以下方法之间的调用不明确

时间:2014-12-20 06:41:45

标签: c# winforms sockets

为什么代码时间又一次又回复了以下错误:

Error 1 The call is ambiguous between the following methods or properties: 'reClientOnly_winforms.Form1.InitializeComponent()' and 'reClientOnly_winforms.Form1.InitializeComponent()'  

代码:

   namespace reClientOnly_winforms
   {
   public partial class Form1 : Form
   {
    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

    public Form1()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(284, 261);
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load_1);
        this.ResumeLayout(false);

    }


    private void Form1_Load_1(object sender, EventArgs e)
    {

    }
    private void Form1_Load(object sender, EventArgs e)
    {

        msg("Client Started");

        clientSocket.Connect("127.0.0.1", 8888);

        label1.Text = "Client Socket Program - Server Connected ...";

    }
    private void button1_Click(object sender, EventArgs e)
    {

        NetworkStream serverStream = clientSocket.GetStream();

        byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");

        serverStream.Write(outStream, 0, outStream.Length);

        serverStream.Flush();



        byte[] inStream = new byte[10025];

        serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);

        string returndata = System.Text.Encoding.ASCII.GetString(inStream);

        msg(returndata);

        textBox2.Text = "";

        textBox2.Focus();

    }

    public void msg(string mesg)
    {

        textBox1.Text = textBox1.Text + Environment.NewLine + " >> " + mesg;

     }

   }
 }

编辑:取自Form1.Designer.cs

      private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.textBox3 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(85, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(88, 68);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(297, 20);
        this.textBox1.TabIndex = 1;
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(88, 197);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(297, 20);
        this.textBox2.TabIndex = 2;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(85, 154);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(35, 13);
        this.label2.TabIndex = 3;
        this.label2.Text = "label2";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Location = new System.Drawing.Point(96, 42);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(35, 13);
        this.label3.TabIndex = 4;
        this.label3.Text = "label3";
        // 
        // textBox3
        // 
        this.textBox3.Location = new System.Drawing.Point(273, 272);
        this.textBox3.Name = "textBox3";
        this.textBox3.Size = new System.Drawing.Size(100, 20);
        this.textBox3.TabIndex = 5;
        // 
        // Form1
        // 
        this.ClientSize = new System.Drawing.Size(432, 316);
        this.Controls.Add(this.textBox3);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.label1);
        this.Name = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load_1);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

我遇到了同样的问题所以我创建了一个新项目来解决问题(以前在Form1.Designer.cs中匹配)。

如何解决这个问题?我看到了this,但没有定论

2 个答案:

答案 0 :(得分:3)

您不能拥有两个InitializeComponent()方法

  

InitializeComponent是在您创建/更改表单时由表单设计器自动为您编写的方法。

所以你不能写一个名为InitializeComponent()的方法并调用它,所以编译器不会理解“选择什么方法”

你能做什么

public Form1()
{
    InitializeComponent();
}
private void Re_InitializeComponent()
{
    InitializeComponent();
    this.SuspendLayout();
    // 
    // Form1
    // 
    this.ClientSize = new System.Drawing.Size(284, 261);
    this.Name = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load_1);
    this.ResumeLayout(false);

}

当你想要做你想要的事情时,请在该功能中调用Re_InitializeComponent()。如下

 public void YourCalling(){
   Re_InitializeComponent();
 }

p.s-我在我的一个项目中对此进行了测试。它给出了相同布局的最小化版本,我想这就是你所期待的

答案 1 :(得分:3)

您的Form1类是部分的,该类的另一部分位于Form1.Designer.cs文件中。 Form1中有InitializeComponent(),Form1.Designer.cs中有另一个。尝试删除Form1中的一个并将其所有内容放入Form1.Designer.cs

相关问题