accessing objects in one form via another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i am creating an app with lots of functions. it is also crowded. i have
decided to move the controls onto lots of separate forms. if a button in a
non form1 form is clicked, could its code be:

form1.textbox.text="";

to clear textbox's text or am i completely wrong?

thanks in advance
 
hi,

i am creating an app with lots of functions. it is also crowded. i have
decided to move the controls onto lots of separate forms. if a button in a
non form1 form is clicked, could its code be:

form1.textbox.text="";

to clear textbox's text or am i completely wrong?

Yes and no...

C# is case sensitive, so it would probably work better with

form1.textbox.Text = "";

....and it also supposes that your other form has a reference variable
referencing your first form, e.g:


class Form2 : Form
{
Form1 form1 = null;
...
public Form2(Form1 f)
{
...
form1 = f;
}
...
}


// Bjorn A
 
Hi ,

make your variables "internal " or "public" and pass the form variable
within constructor . Thats the way i use
 
hi,

do you mean that i should set the textboxes property (i forgot the name but
i'll know when i see it) to internal or public?

or do you mean i should set form1's property to public?
 
message is inline


Alvo von Cossel I said:
hi,
do you mean that i should set the textboxes property (i forgot the name
but
i'll know when i see it) to internal or public?

yes , modifiers = internal | public

or do you mean i should set form1's property to public?

and
let me explain a little bit ;

form1 has a textbox1 and you want to use textbox1 from form2 so
- make textbox1.modifiers = internal | public
- add a variable "form1 form1refvar" to your form2
- modify form2 constructor , that would bu like
public form2 ( form1 ref)
{
form1refvar = ref;

}

- when creating the form2 ,call it like this
form2 frm2 = new form2(this);
 
Back
Top