I have formed buttons
at runtime. I need to access the buttons
from another form
but i can't change the modifier to public at runtime
Could you please help me?
I have formed buttons
at runtime. I need to access the buttons
from another form
but i can't change the modifier to public at runtime
Could you please help me?
You could add a public list of buttons to your form (I assume winforms here):
public List<Button> Buttons { get; private set; }
In the forms constructor:
public MyForm()
{
InitializeComponent();
Buttons = new List<Button>();
}
In the method where you create the buttons at runtime:
var button = new Button();
button.Text = "This is a new Button";
button.Location = ...;
... configure your button here
Buttons.Add(button); // Add button to list.
Controls.Add(button);
In the other form
foreach (Button btn in formWithButtons.Buttons) {
DoSomethingWith(btn);
}
Since the buttons are added dynamically it makes a lot of sense to use a dynamic data structure like a List<T>
.