473,379 Members | 1,544 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,379 software developers and data experts.

Text from form to form

I'm working in Visual Studio 2008 in vb.net and I'm trying to make Form1 pass text in my labels to Form2. Can anyone help me? I can't figure it out.
Aug 27 '08 #1
5 878
If the forms are used in the same application, you can pass a reference to the second form when you create it, then use the reference to move the information from form to form.
Aug 27 '08 #2
Curtis Rutland
3,256 Expert 2GB
There are three ways to do this. All of them work similarly.
  • Expose a public property. Add a public string to your form in the code and set it before you show the form.
  • Expose a public method. Add a public Sub that accepts a string as a parameter and set a private member's value with it.
  • Modify the form's constructor to accept a string. If you don't know what a constructor is, look up VB.NET Constructor. It's the Public Sub New() that you may or may not have. Add a string as a parameter, and change the way you instantiate the form.

Any one will work, I prefer to use the constructor, because of encapsulation.
Aug 27 '08 #3
balabaster
797 Expert 512MB
There are three ways to do this. All of them work similarly.
  • Expose a public property. Add a public string to your form in the code and set it before you show the form.
  • Expose a public method. Add a public Sub that accepts a string as a parameter and set a private member's value with it.
  • Modify the form's constructor to accept a string. If you don't know what a constructor is, look up VB.NET Constructor. It's the Public Sub New() that you may or may not have. Add a string as a parameter, and change the way you instantiate the form.

Any one will work, I prefer to use the constructor, because of encapsulation.
A demonstration:
Expand|Select|Wrap|Line Numbers
  1. Public Partial Class Form2
  2.   Inherits Form
  3.  
  4.   Public Property Item1() As String
  5.     Get
  6.       Return TextBox1.Text
  7.     End Get
  8.     Set (ByVal Value As String)
  9.       TextBox1.Text = Value
  10.     End Set
  11.   End Public Property
  12.  
  13.   ''Or
  14.   ''Only allow outside objects to read the property
  15.   ''after it's been set using the SetValue method
  16.   ''or passed in the constructor.
  17.   'Public ReadOnly Property Item1() As String
  18.   '  Get
  19.   '    Return Textbox1.Text
  20.   '  End Get
  21.   'End Public Property
  22.  
  23.   'Doesn't allow you to GET the value out of the textbox though,
  24.   'just put a value in...
  25.   Public Sub SetValue(ByVal Value As String)
  26.     TextBox1.Text = Value
  27.   End Sub
  28.  
  29.   'Doesn't allow you to GET the value out of the textbox though,
  30.   'just put a value in...
  31.   Public Sub New (ByVal Value As String)
  32.     TextBox1.Text = Value
  33.   End Sub
  34.  
  35. End Class
  36.  
  37. Public Partial Class Form1
  38.   Inherits Form
  39.  
  40.   Public Sub SomeMethodInMyForm()
  41.  
  42.     'If you didn't use the constructor in Form2...
  43.     Dim oFrm2 As New Form2
  44.     oFrm2.Item1 = "My New Value"
  45.  
  46.     'Else if you did use the constructor in Form2....
  47.     Dim oFrm2 As New Form2("My New Value")
  48.  
  49.     'If we're just opening the form to display...
  50.     Form2.Show()
  51.  
  52.     'Or if we want to use the form like a dialog box...
  53.     'requires buttons defined on Form2 to pass back the
  54.     'dialog results like vbOK and vbCancel etc...
  55.     If Form2.ShowDialog() = vbOK Then
  56.  
  57.       MsgBox(Form2.Item1)
  58.  
  59.     End If
  60.  
  61.     'Do stuff...that relates to the data in Form2
  62.  
  63.     'Clean up Form2...
  64.     Form2.Dispose()
  65.  
  66.   End Sub
  67.  
  68. End Sub
  69.  
If you add this kind of idea into the form you wish to put data into and pull data out of the you can read and write to any of the objects on your form... the only way to put data in and get data out
Aug 27 '08 #4
Curtis Rutland
3,256 Expert 2GB
Thanks, I was too lazy to write up a VB.NET sample.
Aug 27 '08 #5
balabaster
797 Expert 512MB
Thanks, I was too lazy to write up a VB.NET sample.
's OK, I'm busy procrastinating on another portion of a project I'm trying to come up with a solution for, so I thought I'd exercise my mental muscles answering everyone else's questions... ;)
Aug 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
12
by: Randell D. | last post by:
Folks, I have a form called "ourTestForm". Its a test form - nothing special - it contains five input tags - they are named one, two, three, four and five. The input tags are of type...
6
by: Lance Geeck | last post by:
I have a simple form where I am using a dataset called Client. On the data entry screen, there are name, address, city state and zip. I have the fields bound to the dataset field. (Properties...
16
by: Adda | last post by:
If I cycle through the MdiChildActivate event of the parent form I can read text in a textbox on the child mdiform -- console.writeline(Me.ActiveMdiChild.Controls(1).Text) But if I have a sub...
7
by: Andrew McKendrick | last post by:
Hi, I've noticed a bug in VB.NET (latest .NET Framework)... - I have a TabControl on a form with several tabs. - Each tab contains text boxes that are bound to fields in a data source...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.