Newbie Question

A

Abet

Hi,


Im new to VB.NET programming, Im currently starting my study.

I have a simple question. I have two forms in my project and

I was wondering how could I access the value of a Text box

from one form in the other and vice versa. What is the best way

to display the 2nd form and be able to access the objects from

the main form. Right now im using the command

Dim frmPrint As New Form2
frmPrint.Show()


Thanks,
Abet
 
S

Scott M.

The code you have below is just fine. Now, let's assume this code is inside
of Form1, so you could access a textbox that is in Form2 from Form1 with
this code:


Public Class Form1

Sub ShowOtherForm()
Dim frmPrint As New Form2
frmPrint.Show()
Dim otherTextBoxValue As String = frmPrint.TextBox1.Text
End Sub

End Class
 
A

Abet

Hi Scott,

Thanks for the reply. Im really new to VB.NET
When I close Form2 or frmPrint, how do I get
the value of frmPrint.TextBox1.Text? I tried
checking for the value of otherTextBoxValue but
the variable it is not recognized in other objects

Thanks,
Abet
 
C

Cor Ligthert [MVP]

Abet,

I did read your message a little bit else than Scott, than it is stealing
his code
(this is refering to another message from Scott)
:)

\\\
Public Class Form1
Sub ShowOtherForm()
Dim frmPrint As New Form2
frmPrint.Owner = Me
frmPrint.Show()
Dim otherTextBoxValue As String = frmPrint.TextBox1.Text
End Sub
///

Now you can use in your second form
\\\
WhatINeed = me.owner.Textbox1.Text
///
I hope this helps,

Cor
 
S

Scott M.

Let's take this from the top...

Module1
'Global variable to hold a value as long as the program is running
'This should be the Project's Startup Object

Public OtherFormTBValue As String

Sub Main
Dim Form1 As New Form1
Form1.Show()
End Sub

End Module

Public Class Form1

Private frmPrint As New Form2

Sub ShowOtherForm
frm2.Show()
End Sub

Function GetOtherFormsTextBoxValue() As String
'Here's how you can get the value of a textbox on another form
'just by calling this method anytime Form1 is open
Return frm2.tb.Text
End Sub

Function GetOtherFormsTextBoxValue2() As String
'Here's another way to get the other form's textbox value
'When the 2nd form is closed, the global variable will be populated
Return OtherFormTBValue
End Sub

End Class

Public Class Form2
Dim tb As New TextBox

Sub Form2_Close() Handles Form2.Close
OtherFormTBValue = tb.text
End Sub

End Class
 

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

Similar Threads


Top