0

I had this problem, I can't add a control inside a class. More specifically the Controls.Add(Button);. Is this possible or do i missing something.

    MyFunctions mf = new MyFunctions();
    class MyFunctions
    {
        public int ButtonWidth(int number)
        {
            string a = "";
            int ButtonWidth=0;

            Button x = new Button();
            x.Size = new Size(10, 40);//Initial Size
            x.AutoSize = true;
            x.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            Controls.Add(x);//Why i can't this one?

            for(int i=1;i<=number;i++)
            {
                a += "X";
                x.Text = a;
                ButtonWidth = x.Width;

                MessageBox.Show(i + "-" + a + "-" + ButtonWidth);
            }
            return ButtonWidth;
        }
  }`

I got an error message.

can't access a non-static member of outer type.

I made a trial to see if the button width is changing when button.text changes its length. but the button.width is held constant.

2 Answers 2

1

Your MyFunctions is a separate class from your Form class. And therefore, it does not have Controls property (the Controls property belongs to your Form - or its derived - class)

If you want to add a Button in your Form class, you should make your code block a Method of your Form instead of a new, separated MyFunctions class:

public class MyForm : Form {  
    public int ButtonWidth(int number) //this is OK
    {
        string a = "";
        int ButtonWidth=0;

        Button x = new Button();
        x.Size = new Size(10, 40);//Initial Size
        x.AutoSize = true;
        x.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        Controls.Add(x);//Why i can't this one?

        for(int i=1;i<=number;i++)
        {
            a += "X";
            x.Text = a;
            ButtonWidth = x.Width;

            MessageBox.Show(i + "-" + a + "-" + ButtonWidth);
        }
        return ButtonWidth;
    }
}

That being said, since your method name is to ButtonWidth, it is not advisable (not a good design) to add Button in a method which is meant to get ButtonWidth. Consider separation of Methods: CreateButton to create your Button, GetButtonWidth to get the ButtonWidth (though it may not be necessarily made a method) and then you simply add your created Button to your Form's Controls.

Sign up to request clarification or add additional context in comments.

1 Comment

@Vincent great. ;) please note my comment on your code design too... I strongly encourage you to consider separation of Methods to make your code becomes much clearer.
0

You're trying to access an instance of a class, from an instance of another class.

Try making MyFunctions static, or include the functions on a base class that the first class derives from.

Comments

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.