0

I'm trying to add a button to the form using code and iv'e looked it up on the internet but nothing works.

public void addSnake()
{
    Button btn = new Button();
    btn.Location = new Point(360, 390);
    btn.Size = new Size(10, 10);
    btn.Text = "";
    btn.Name = num + "";
    btn.Tag = this.oneD;
    btn.IsAccessible = false;
    Controls.Add(btn);
}

public Point getPoint()
{
    Button btn = (Button)Controls.Find(num + ""); 
    return this.pos; //temporary
}

it says "The name 'Controls' does not exist in the current context". (for both functions)

Note: the functions addSnake and getPoint are inside a Class I made

Full code here: deleted

4
  • Show the entire code Commented Sep 13, 2015 at 15:05
  • Is the class you made inheriting from System.Windows.Forms.Form? Else there is no such property if you have not declared one by yourself. Commented Sep 13, 2015 at 15:09
  • added full code. How do I make it so? Commented Sep 13, 2015 at 15:10
  • just try this.controls.add(btn) instead controls.add(btn). If you are trying this in the code behind file of the form. Commented Sep 13, 2015 at 15:19

2 Answers 2

1

Your class is not inheriting from System.Windows.Forms.Form. So there is no such property called Controls.

What you can do is pass a reference of the form to the constructor of SnakeB:

public class SnakeB
{
    private System.Windows.Forms.Form parentForm;

    public SnakeB(System.Windows.Forms.Form parent)
    {
        parentForm = parent;
    }
}

and use it in your methods like this:

public Point getPoint()
{
    Button b = parentForm.Controls.Find(num + "") as Button;
    return b.Location;
}

public void addSnake(bool isFirst)
{
    Button b = new Button();
    // ...
    parentForm.Controls.Add(b);
}

Usage:

public Form1()
{
    InitializeComponent();
    SnakeB snake = new SnakeB(this);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to have instance of the form in your class and add the control to that instance like

public class SnakeB
{
    int num;
    int oneD;
    Point pos = new Point();
    Form1 frm1 = new Form1();



 frm1.Controls.Add(btn);

How will you have the instance of the form? Like below

    public Form1()
    {
        InitializeComponent();
        SnakeB snake = new SnakeB(this);
    }

In your class have a similar consructor

public class SnakeB
{
    int num;
    int oneD;
    Point pos = new Point();
    Form1 frm1;

    public SnakeB()
    {
        this.num = 0;
        this.oneD = 2;
        //here construct the maain head button and 2 reguler ones
        addSnake(true);
        addSnake(false);
        addSnake(false);
    }

    public SnakeB(Form1 frm) : this()
    {
        this.frm1 = frm;
    }

1 Comment

It creates a loop Loading 'Form1' over and over again which causes a stackoverflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.