new c# frustrations

S

S Moran

im starting to think everything became A LOT more complicated with .net as
opposed to vs6.

how can i access a textbox on a form from another form?

for example i have a button on form1 and when i click it i want to add text
to a textbox on form2.

sure was easy in vs6
 
P

Paul Hadfield

Looking through your previous posts, it may be an idea to spend some time
learning the differences between VB6 and VB.NET - they are small but
important. You've already asked how to access a button one form from
another - to which you've got an answer, now you're asking how to access a
text box in one form from another (note: they both inherit from control (and
therefore object), so same concepts apply). I could give you the answer
here, but I fear that I can see your next post now:

----------------
im starting to think everything became A LOT more complicated with .net as
opposed to vs6.
how can i access a listbox on a form from another form?
for example i have a button on form1 and when i click it i want to add an
item
to a listbox on form2.
sure was easy in vs6
 
I

Israel

im starting to think everything became A LOT more complicated with .net as
opposed to vs6.

how can i access a textbox on a form from another form?

for example i have a button on form1 and when i click it i want to add text
to a textbox on form2.

sure was easy in vs6

I think that having two forms directly communicate with each other is
generally a bad a model. I'm not sure I ever thought about doing this
with VS6 but I probably did it through VB6 just as a quick and dirty
hack.
I would highly recommend changing your model so if you click on a
button it talks to some other object which then fires an event which
the other form responds to. Then when you create both of those forms
they use that object as their conduit of communication. It really
shouldn't be a lot of code.
 
S

S Moran

hhmm... apparently its not that simple? if textbox1 exists on form2, then
you would think, from button1 i could access the textbox via
"form2.textbox1.text", but this isnt working.

so howzabout a pointer? or is that not why we're here?
 
A

AlexS

You can if textbox1 is visible in form1. Designer usually makes control
fields private, so no visibility. You can change it to public.

But this is bad practice. Probably you need to familiarize yourself with
access modifiers and read about properties and fields. It's basic OO stuff,
which can help you to sort out issues like this one.

http://msdn2.microsoft.com/en-us/library/zztsbwsx(VS.80).aspx
 
C

Chris Mullins [MVP]

By default, controls are defined as "private" to a form.

In "form 1" (the one that has the text box), change the modifier on the text
box from "private" to "public". This will allow it to be visible.

The other problem you're going to run into is that VB6 used default form
instances, and .Net doesn't. This means when you create a "new" form 1 and a
"new" form 2, you'll need to keep the references around. This way form2 can
say:
myform1.UserNameTextBox.Text = "bla bla bla";
 
S

S Moran

thank you very much.
interesting, form2 has a textbox whose modifier is set to public...
in form1's class i have "Form secondform = new Form2();"
then, a button on form1 says "secondform.textBox1.Text = "some text here";
but this doesnt work. i get:

"Error 1 'System.Windows.Forms.Form' does not contain a definition for
'textBox1'
 
S

S Moran

ok... so changing "Form secondform = new Form2();"
to "Form2 secondform = new Form2();" fixed that problem.
trying to get a good understanding of why
 
G

Guest

This is inheritence. Form2 inherits from Form, so your original declaration
only opened the access to the base class methods.
 
J

Jon Skeet [C# MVP]

S Moran said:
thank you very much.
interesting, form2 has a textbox whose modifier is set to public...
in form1's class i have "Form secondform = new Form2();"
then, a button on form1 says "secondform.textBox1.Text = "some text here";
but this doesnt work. i get:

"Error 1 'System.Windows.Forms.Form' does not contain a definition for
'textBox1'

And indeed it doesn't. You've declared the type of secondform to be
Form - you should have declared it to be Form2. (Or preferrably, you
should have given Form2 a more sensible and meaningful name in the
first place.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top