question

DanielShepherd-8507 avatar image
0 Votes"
DanielShepherd-8507 asked Viorel-1 commented

CS1061 fix

i am trying to make a dialog box with a separate form for the dialog box, but for some reason it gets this error:

 Error    CS1061    'object' does not contain a definition for 'Add' and no accessible extension method 'Add' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

here is my code:

  this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(220, 121);
             object p = this.Controls.Add(this.button2);
             object p3 = this.Controls.Add(this.button1);
             object p2 = this.Controls.add(label1);
             object p1 = this.Controls.Add(this.textBox1);
             this.Name = "name";
             this.Text = "Form1";
             this.ResumeLayout(false);
             this.PerformLayout();

NOTE:I am kind of new to this, so explain in any answers on this question in basic understanding.
How do I fix this!

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DuaneArnold-0443 commented

Hi DanielShepherd-8507,
Based on your code, Controls.Add(Control) is a method which is used to add the specified control to the control collection and it has no return value.
You need to change to the following code:

 this.Controls.Add(this.button2);
 this.Controls.Add(this.button1);
 this.Controls.Add(label1);
 this.Controls.Add(this.textBox1);

Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It still keeps getting the same error when I do this.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 DanielShepherd-8507 ·

Did you enter that lines manually?

0 Votes 0 ·

If you look at the designer code like Payrollview.designer.cs in the Github solution, you'll see how to establish a control and add it to the form.

The Windows form solution shows how to do it, which I had to do all the forms manual becuase Widows form designer in .NET Core and the VS IDE was not available at the time.

https://github.com/darnold924/PubComanyWinCore

0 Votes 0 ·
  1. 92097-thing.png
    2.your project is not letting me open it in VS2019.

  2. don't understand this.

0 Votes 0 ·
thing.png (3.2 KiB)
Show more comments
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DanielShepherd-8507 commented

I guess, why are you writing this code, which is done automatically by the Visual Stuido IDE when dragging a control from the toolbox and dropping it on the form?

Secondly, all objects in .NET derive from the base object System.Object.

https://docs.microsoft.com/en-us/dotnet/api/system.object?view=net-5.0

If you look at the methods of the object class, there is no Add() method.

object p3 = this.Controls.Add(this.button1);

The statement is wrong where p3, an object, is to be a this.controls.Add() with an Add() derived from the Controls collection which is an array. p3 can be a Controls, but p3 can never be a Controls.Add() as you have it.

However, Object can be a container object holding the Controls collection object as a whole, which is for another discussion.

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.controlcollection?view=net-5.0

Anytime you see a plural naming convection used on an object, it means holds more than one item..

I suggest that you start over and drag a control to a form from the toolbox, right-click the form, select view code, select the InitializeComponent() method, right-click the method, select 'go to definition' and view the control code that was generated in the InitializeComponent() method when the control is dragged and dropped on the form, because that's how the coding is done, if you get to making form control code on a form manually yourself.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This does not make any sense to me. I had literally said that I was a beginner.

0 Votes 0 ·
DanielShepherd-8507 avatar image
0 Votes"
DanielShepherd-8507 answered Viorel-1 commented

Here is my code that has been updated. It still gets the same error.

  this.button2.Location = new System.Drawing.Point(26, 86);
             this.button2.Name = "button2";
             this.button2.Size = new System.Drawing.Size(75, 23);
             this.button2.TabIndex = 3;
             this.button2.Text = "cancel";
             this.button2.UseVisualStyleBackColor = true;
             // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(220, 121);
             this.Controls.Add(this.button2);
             this.Controls.Add(this.button1);
             this.Controls.Add(this.label1);
             this.Controls.Add(this.textBox1);
             this.Name = "name";
             this.Text = "Form1";
             this.ResumeLayout(false);
             this.PerformLayout();
    
         }


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ALL of my code just got like 70 errors. not kidding. i have NO idea why. PLEASE HELP!

0 Votes 0 ·

One error can cascade into many errors.

And to me, you are in an area of coding that you should not been in, becuase you don't have the expertise to code it.

You should be letting the Visual Stuido IDE auto generate the code with you doing a drag and drop operation of a control from the toolbox onto the form that just hooks it all up for you code-wise. The VS IDE auto-generates the code behind the scenes. What you are doing you shouldn't be concerned about as a beginner.

https://www.youtube.com/watch?v=4BYK7HhxPPk

continued......

0 Votes 0 ·

You should have gone through a general tutorial about C# and using the VS IDE. You should have purchased a book for beginners that would have taken you through the VS IDE in this situation about using user controls, but also, it would have give you some basic Object Oriented Programming concepts as you went through the book.

https://www.amazon.com/C-Dummies-Stephen-R-Davis/dp/0764508148

How did you get to the point you're now at without having a basic understanding of the power of the VS IDE
and a basic knowledge of OOP?

I suggest that you abandon what you are doing, and do the things a beginner should be doing at this time.

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 DanielShepherd-8507 ·

If the error messages are the same, maybe you have a manually added member of your class called “Controls”, such as:

private object Controls;

Do you have such variable in your class? Then rename it.

Otherwise, show the first error messages.

0 Votes 0 ·