473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Acces from one form to another form's controls.

Hi

I have frm1. On this form button.Click code for this
button is:

Dim frm2 as New frm2
frm2.show

So after click, frm2 form is shown.

And I want to click on this shown form(frm2) button, and then textbox
on frm1
(this main form ) immediately change. How?? When I add to button on
frm2 this code, it isn't working:

dim frm1 as new frm1
frm1.textbox.text="Hello"
Thx Mrozu

Dec 20 '05 #1
5 2428
Hi, Mrozu,
Form2 doesn't know about Form1. What you're doing in your code below is defining a *new* form1 and trying to set the value on that, which won't work.

Instead (assuming that you're using VS2005), remove all of the code you wrote in form2's button code and instead add the following line:

My.Forms.Form1.TextBox1.Text = "hello"

That will get it done for you.

Hope this helps!
--Matt Gertz--*
VB Compiler Dev Lead

-----Original Message-----
From: Mrozu
Posted At: Tuesday, December 20, 2005 11:31 AM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Acces from one form to another form's controls.
Subject: Acces from one form to another form's controls.
Hi

I have frm1. On this form button.Click code for this
button is:

Dim frm2 as New frm2
frm2.show

So after click, frm2 form is shown.

And I want to click on this shown form(frm2) button, and then textbox
on frm1
(this main form ) immediately change. How?? When I add to button on
frm2 this code, it isn't working:

dim frm1 as new frm1
frm1.textbox.text="Hello"
Thx Mrozu
Dec 20 '05 #2
Sorry but I'm using VB 2003:/ What then?? Your code isn't working:/
Thx Mrozu

Dec 20 '05 #3
Ohhh I think that i found the way.

Code for button on frm2:

frm1.definstance.textbox1.text="hello"

And it is working. But maybe you know better solution;)) If yes, tell
me it.
Thx Mrozu

Dec 20 '05 #4
Oh yes now I see that it isn't very good way.

I get it, when I convert simple VB6 project which can do what i want,
to VB 2003 by converter. And it is working, but converter add very
strange code:

Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form2
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form2
Get
If m_vb6FormDefInstance Is Nothing OrElse
m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form2
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As Form2)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region

to all forms. I think that it isn't very good. Any idea how do that
better??

Thx Mrozu

Dec 20 '05 #5
So, if you're not using VS2005, there are several things you can do:

(1) You can find Form1 (or whatever it's name is) from Application.OpenForms.Item("Form1"), iterate through the resulting form object to find the "TextBox1" control (or whatever you called it), cast it to a TextBox control, and set the text. That's kind of a clumsy way to do it.

(2) You can create a property on Form2 which gets/sets a value of type Form1 (for example, public property FirstForm(ByVal frm as Form1), which uses a field you create, for example Private _FirstForm as Form1). Then, after you create Form2, but before you Show() it, call the property on it to set the reference to Form1 (for example, frm2.FirstForm() = Me). You can then change your button code in Form2 to be FirstForm.TextBox1.Text = "hello". This is probably the way I'd do it -- only the forms which care about this sort of access will be exposed through it.

(3) You could create a global variable of type Form1 which is set by your instance of form1 on its Load event. You can then use this variable in form2 code. This is not an elegant way of doing it, in my opinion, since I feel that global variables should be avoided. This is essentially what the upgrade code you mention below does, though -- it's accounting for the fact that there are no default instances in VB.NET, and so it's creating a way to get at the default instance of a given form.

Etc... there are many other ways of doing this as well, some of which depend on whether or not Form1 is the startup form.

--Matt--*

-----Original Message-----
From: Mrozu
Posted At: Tuesday, December 20, 2005 1:24 PM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Acces from one form to another form's controls.
Subject: Re: Acces from one form to another form's controls.
Oh yes now I see that it isn't very good way.

I get it, when I convert simple VB6 project which can do what i want,
to VB 2003 by converter. And it is working, but converter add very
strange code:

Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form2
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form2
Get
If m_vb6FormDefInstance Is Nothing OrElse
m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form2
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As Form2)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region

to all forms. I think that it isn't very good. Any idea how do that
better??

Thx Mrozu
Dec 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ColinWard | last post by:
Hi. I have a form which has as its recordsource an SQL string. The SQL String is as follows: SELECT * from CONTACTS where false. this ensures that there is no data loaded in the form when the...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
5
by: ortaias | last post by:
I have a form which calls up a second form for purposes of data entry. When closing the data entry form and returning to the main form, things don't work as expected. When I return to the main...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
0
by: Joe Abou Jaoude | last post by:
Hi, I need to acces the controls of an asp.net page (let's say the Default.aspx page) using the IDesignerHost interface. I need some help and some links that shows how to do that. I was only able...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
5
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.