Reputation: 115
I'm new in objective programing, just started learning so probably my question will be stupid for most of you but, I really don't know how to achieve what i need.
I have Form1 with control - combobox_kraje
, user choose country and click button
, here is the button code
Here Messagebox.Show(combobox_kraje.text)
show me correct value of this control.
Now i want to access combobox_kraje in my class
Here Messagebox.Show(form.combobox_kraje.text)
show me incorrect value (empty MessageBox).
Can you please explain me how i can access that form control in my class?
Upvotes: 0
Views: 57
Reputation: 9365
You are using form
(which is initialized by new Form1()
), but it is not the instance that is shown.
You should pass the instance that is shown to the class and assign it to form
instead:
public MyClass(Form1 form1)
{
this.form = form1;
}
Also as @HimBromBeere stated, you should add a property to expose the text it self, instead of using a public control.
Upvotes: 0
Reputation: 37000
Setting something to public to be able to access it outside isn´t allways a good idea and at least here there´s a better approach to do this. Just add a property to your form that returns the combonboxes text-value:
public class MyForm : Form
{
public string TheMessageText { return this.combobox_kraje.Text; }
}
Now access it via form.TheMessageText
. This way you can leave the actual control private
and only provide those parts to the outside that are relevant, not the entire control.
Upvotes: 1