C# 中的 InitializeComponent 不存在 [英] InitializeComponent in C# doesn't exsist
问题描述
我正在使用 .net 4,但没有看到 InitializeComponent 方法.有吗?
I am using .net 4 and I don't see the InitializeComponent method. Is it there?
这是我正在使用的类文件
Here is my class file that i am using
using System;
using System.Drawing; //must add reference
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms; //must add reference
using System.Threading;
using System.Net.Sockets;
using System.IO;
public class Client : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox inputTextBox;
private System.Windows.Forms.TextBox displayTextBox;
private NetworkStream output;
private BinaryWriter writer;
private BinaryReader reader;
private string message = "";
private Thread readThread;
private System.ComponentModel.Container components = null;
//default constructor
public Client()
{
InitializeComponent();
readThread = new Thread(new ThreadStart(RunClient));
readThread.Start();
}
[STAThread]
static void Main()
{
Application.Run(new Client());
}
protected void Client_Closing(object sender, CancelEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
//sends text the user typed to server
protected void inputText_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
writer.Write("CLIENT>>>> " + inputTextBox.Text);
displayTextBox.Text += "\r\nCLIENT>> " + inputTextBox.Text;
inputTextBox.Clear();
}
}
catch
{
displayTextBox.Text += "\nError writing object";
}
} //end method inputTextBox_KeyDown
//connects to server and display server-generated text
public void RunClient()
{
TcpClient client;
//Instantiate TcpClient for sending data to server
try
{
displayTextBox.Text += "Attempting connection...\r\n";
//Step 1: create TcpClient and connect to server
client = new TcpClient();
client.Connect("localhost", 5000);
//Step 2: Get NetworkStream associated with TcpClient
output = client.GetStream();
//creates objects for writing and reading across streams
writer = new BinaryWriter(output);
reader = new BinaryReader(output);
displayTextBox.Text += "\r\nGot I/O stream\r\n";
inputTextBox.ReadOnly = false;
//loop until server signals termination
do
{
//Step 3: processing phase
try
{
//read message from the server
message = reader.ReadString();
displayTextBox.Text += "\r\n" + message;
}
//handle exception if error in reading server data
catch (Exception)
{
System.Environment.Exit(System.Environment.ExitCode);
}
} while (message != "SERVER>>> TERMINATE");
displayTextBox.Text += "\r\nClosing connection.\r\n";
//Step 4: close connection
writer.Close();
reader.Close();
output.Close();
client.Close();
Application.Exit();
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
}
推荐答案
InitializeComponent 方法未由 Control 或其任何子项(如 Form)定义或抽象;它是由设计师 100% 从头开始生成的.它也是私人的;你不能从控制类之外调用它.如果您没有从设计者那里开发这个 Control 类,那么您就没有 InitializeComponent 方法,除非您自己创建.
The method InitializeComponent is not defined or abstracted by Control or any of its children (like Form); it is generated, 100% from scratch, by the designer. It is also private; you cannot call it from outside the control class. If you have not developed this Control class from the designer, you do not have an InitializeComponent method unless you create one yourself.
这篇关于C# 中的 InitializeComponent 不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!