1

Why is the code time and again returning me the following error:

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

Code:

   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;

     }

   }
 }

EDIT : Taken from 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();

    }

I was having the same issue so i created a new project to get resolve the issue ( previously was matching in Form1.Designer.cs) .

How to get around this?I saw this but wasn't conclusive

Community
  • 1
  • 1
Khan
  • 185
  • 1
  • 4
  • 15
  • You need to provide a short but complete code reproducing the problem to get the answer. – Sriram Sakthivel Dec 20 '14 at 06:52
  • 1
    what is in your other `partial class Form1`(s)? – bansi Dec 20 '14 at 06:55
  • if you are using VisualStudio, there is a button `Show All files` on the top of `Solution Explorer` box. Click that and you can then see the additional files in each form. check if you have any other files there. – bansi Dec 20 '14 at 07:06
  • looks like you just remove the `private void InitializeComponent()` method from the first code – bansi Dec 20 '14 at 07:08
  • Might be compiler gets ambigiuos from above designer code in the last para. –  Khan Dec 20 '14 at 07:08
  • To be clear,i was having this sample from a site and added .cs of it to new project and thereby creating the same form on `Form1` in project.When i got to know later that i was having it pre-defined in taken project i deleted the other and kept mine created same form.Looks like a mess .... –  Khan Dec 20 '14 at 07:12
  • 1
    if you don't want all other controls in the Designer.cs, i would suggest you use the designer to remove those controls. otherwise you can replace/remove the `InitializeComponent()` method in the Designer.cs. Also don't forget to remove the decelerations for those components too. – bansi Dec 20 '14 at 07:16
  • There is another option: you can have erroneous references in the project. Try to find something unusial, like self-reference, cyclic references – omikad Dec 20 '14 at 07:34

2 Answers2

3

You cannot have TWO InitializeComponent() methods

InitializeComponent is a method automatically written for you by the Form Designer when you create/change your forms.

So you cannot write a method having the name InitializeComponent()and call it so compiler won't understand "what method" to select

What you can do

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

}

And when you want to Do the thing you want call Re_InitializeComponent() in that function. As follows

 public void YourCalling(){
   Re_InitializeComponent();
 }

p.s- I tested this in on one of my projects. it gives a minimized version of same layout and I guess that's what you are expecting

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
  • If he have two methods, he'll get different error. *Type 'NameSpace.YourTyeName' already defines a member called 'InitializeComponent' with the same parameter types* – Sriram Sakthivel Dec 20 '14 at 06:52
3

Your Form1 class is partial, another part of the class is in Form1.Designer.cs file. You have InitializeComponent() in your Form1 and there is another in Form1.Designer.cs. Try to remove one in Form1 and put all of his content to Form1.Designer.cs

omikad
  • 979
  • 8
  • 10