473,405 Members | 2,334 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,405 software developers and data experts.

Accessing controls on another form.

Hi

I posted this to the .Net general newsgroup and got a reply from "Morten"
who has given me an answer but in C# a language I have never worked with.
VB.Net is still very new to me, I was hoping someone here in the VB.net
group could help me a little. I have copied the posts to here.

Morten has given me a code snippet in C# but where do I use it and what is
the VB.Net equivalent.

Thanks DaveG

''''''''' Orig Post ''''''''''''''
Hi

Using VB.net 2003

I know how to access controls on a 2nd form after declaring the form
in the first.

But how do I access the controls on the original form from the 2nd
form.

(in frmMain)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim form2 As New Form2
form2.TextBox1.Text = TextBox1.Text
form2.Show()
End Sub

(in Form2)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim form1 As New Form1
form1.TextBox1.Text = TextBox1.Text
form1.Update()
Me.Close()

End Sub
Form2 textbox updates ok
The textbox in form1 does not update and still holds the original text
before opening Form2

This is just a simple example I cannot get working, once I can get
this working the I should not have problems with the actual code I
need to use.

There must be a simple answer to this....???

Thanks in advance for any help given

DaveG
Hi Dave,

You need to pass a reference to your 1st form when declaring the 2nd,
typically

Form2 f2 = new Form2(this); // Me in VB

Overload the constructor of the 2nd form to accept a 1st form
parameter. Store the parameter for later use

On Sat, 30 Apr 2005 13:29:05 +0200, DaveG <no**********@daveg.co.uk>
wrote:

Thanks Morten

Now the problem is I have used the overload in normal funtions and subs
but never with the constructors, so now I'm a little lost, I understand
the reasons for the overload..... so the New form2(Me) will be excepted
bur how to implement it is where I am stuck......

Dave, overloading a constructor is done the exact same way, although I'm
not sure how the VB syntax is. Store the Form1 reference for later use.

private Form1 myParent;

public Form2(Form1 f)
{
myParent = f;
}

then simply call

myParent.MethodOrSimilarInForm1()

whenever you need.

Nov 21 '05 #1
3 3229
Dave,

There are many roads that goes to Rome

Your mainform
\\\
Dim frm As New Form2
frm.Owner = Me
frm.Show()
///
You form where you want to use it.
\\\
MessageBox.Show(DirectCast(Me.Owner, Form1).TextBox1.Text)
///
Assuming that you have a textbox1 on form1

I hope this helps,

Cor
Nov 21 '05 #2
"DaveG" <no**********@daveg.co.uk> schrieb:
I posted this to the .Net general newsgroup and got a reply from "Morten"
who has given me an answer but in C# a language I have never worked with.
VB.Net is still very new to me, I was hoping someone here in the VB.net
group could help me a little. I have copied the posts to here.


Depending on the situation direct access to the parent form isn't required
in order to update the parent form upon closing the other form:

<URL:http://groups.google.de/groups?selm=%237sTDoqHFHA.3208%40TK2MSFTNGP10.phx. gbl>

In MDI environments:

\\\
DirectCast(Me.MdiParent, MainForm).TextBox1.Text = ...
///

Providing a reference to an application's main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainform&lang=en>

In VB 2005 you can access form instances more easily with 'My.Forms.*'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
Thanks guys for your quick responses.

I think I am crawling along one of these roads to Rome. now I can see how
I am supposed to implement a basic setup I'm sure I can play a little, if
not "I shall be back".

DaveG

On Sun, 01 May 2005 09:54:54 +0200, DaveG wrote:
Hi

I posted this to the .Net general newsgroup and got a reply from
"Morten" who has given me an answer but in C# a language I have never
worked with. VB.Net is still very new to me, I was hoping someone here
in the VB.net group could help me a little. I have copied the posts to
here.

Morten has given me a code snippet in C# but where do I use it and what
is the VB.Net equivalent.

Thanks DaveG

''''''''' Orig Post ''''''''''''''
Hi

Using VB.net 2003

I know how to access controls on a 2nd form after declaring the form
in the first.

But how do I access the controls on the original form from the 2nd
form.

(in frmMain)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim form2 As New Form2
form2.TextBox1.Text = TextBox1.Text
form2.Show()
End Sub

(in Form2)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim form1 As New Form1
form1.TextBox1.Text = TextBox1.Text
form1.Update()
Me.Close()

End Sub
Form2 textbox updates ok
The textbox in form1 does not update and still holds the original
text before opening Form2

This is just a simple example I cannot get working, once I can get
this working the I should not have problems with the actual code I
need to use.

There must be a simple answer to this....???

Thanks in advance for any help given

DaveG Hi Dave,

You need to pass a reference to your 1st form when declaring the 2nd,
typically

Form2 f2 = new Form2(this); // Me in VB

Overload the constructor of the 2nd form to accept a 1st form
parameter. Store the parameter for later use

On Sat, 30 Apr 2005 13:29:05 +0200, DaveG <no**********@daveg.co.uk>
wrote:

Thanks Morten

Now the problem is I have used the overload in normal funtions and subs
but never with the constructors, so now I'm a little lost, I understand
the reasons for the overload..... so the New form2(Me) will be excepted
bur how to implement it is where I am stuck......

Dave, overloading a constructor is done the exact same way, although I'm
not sure how the VB syntax is. Store the Form1 reference for later use.

private Form1 myParent;

public Form2(Form1 f)
{
myParent = f;
}

then simply call

myParent.MethodOrSimilarInForm1()

whenever you need.

Nov 21 '05 #4

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

Similar topics

3
by: Tom Meuzelaar | last post by:
Hello: I'm using VB6 in VS enterprise. I'd like to place an HTML form inside a VB container, have a user fill out the form information, click a submit button, and then have the program capture...
4
by: Jim Heavey | last post by:
Hello, I have created a form which I intend to use a a dialog box. It has a few controls on that form and I changed the properties of those controls to "public" with the idea that I could access...
4
by: Andrew Diabo | last post by:
I have 2 forms (Form1 and Form2) in my C# project. I created the second form from the main form like so: Fom2 aForm = new Form2(); aForm.ShowDialog(); How do I access the properties of a...
3
by: Tim Fitzgerald | last post by:
Hello all, I have no problem accessing another form in my app.. however, when I try to access a ListView on a different form, I come up empty... Basically, Dim pForm As New frmMain Dim iCount...
0
by: Geraldine Hobley | last post by:
Hello I have a problem whereby I have a treeview control on one form called projecttree and I wish to clea the nodes from this control in another form The form that contains the treeview is...
5
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
8
by: colmkav | last post by:
Hi, could someone tell me how I can check whether a database is open by name eg something like db("mydbname")
3
by: M K | last post by:
I have 2 classes. One where the form resides and I created another one for all the database stuff. after i get data from the db i want to be able to update the form. I have the namespace of...
2
by: San24 | last post by:
Guys, Let me explain the application I have - Form > Main Tab Control > Main Tab Page > User Control > Sub Tab Control > Sub Tab Page > User Control > Contols/Text Box. Form - The main...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.