【问题标题】:Call is ambigious between the following methods以下方法之间的调用不明确
【发布时间】:2014-12-20 06:41:44
【问题描述】:

为什么代码一次又一次地给我返回以下错误:

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,但不是结论性的

【问题讨论】:

  • 您需要提供一个简短但完整的代码来重现问题以获得答案。
  • 您的其他partial class Form1(s) 中有什么内容?
  • 如果您使用 VisualStudio,Solution Explorer 框顶部有一个按钮 Show All files。单击它,然后您可以看到每个表单中的其他文件。检查那里是否还有其他文件。
  • 看起来你只是从第一个代码中删除了private void InitializeComponent() 方法
  • 如果您不想要 Designer.cs 中的所有其他控件,我建议您使用设计器删除这些控件。否则,您可以替换/删除 Designer.cs 中的 InitializeComponent() 方法。也不要忘记删除这些组件的减速。

标签: c# winforms sockets


【解决方案1】:

你不能有两个 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- 我在我的一个项目中对此进行了测试。它提供了相同布局的最小化版本,我想这就是你所期望的

【讨论】:

  • 如果他有两种方法,他会得到不同的错误。 Type 'NameSpace.YourTyeName' 已经定义了一个名为 'InitializeComponent' 的成员,具有相同的参数类型
【解决方案2】:

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

【讨论】:

  • 如果他有两种方法,他会得到不同的错误。 Type 'NameSpace.YourTyeName' 已经定义了一个名为 'InitializeComponent' 的成员,具有相同的参数类型
  • 好的,然后尝试在Form1中重命名InitializeComponent()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 2013-12-23
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多