【问题标题】:InitializeComponent in C# doesn't exsistC# 中的 InitializeComponent 不存在
【发布时间】:2013-01-07 22:56:54
【问题描述】:

我正在使用 .net 4,但没有看到 InitializeComponent 方法。有吗?

这是我正在使用的类文件

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());
        }
    }

}

【问题讨论】:

  • 您使用的是什么类型的项目(winforms、asp.net、wpf 等)?
  • 它是一个 windows 项目。我为项目添加了所有必要的引用。
  • 您是否在表单中添加了任何控件?
  • 当我尝试输入它时,在自动完成中它没有显示出来。
  • @Steffan,你能给我们看看代码文件吗?

标签: c# .net winforms


【解决方案1】:

InitializeComponent 方法不是由 Control 或其任何子项(如 Form)定义或抽象的;它是由设计师 100% 从头开始​​生成的。它也是私人的;您不能从控制类外部调用它。如果您还没有从设计器开发这个 Control 类,那么您没有 InitializeComponent 方法,除非您自己创建一个。

【讨论】:

  • 另外,设计者现在不也将 InitializeComponent 方法放在partial 类中吗?我已经有一段时间没有写过 winforms 应用程序了,所以我不能 100% 确定。
  • 是的,但是一个部分声明中的成员仍然可以从其他部分访问; partial 关键字只是一个编译器提示,多个源文件创建一个类,因此当该类位于一个文件中时任何代码都可以工作,如果它被拆分为部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2011-10-19
相关资源
最近更新 更多