469,647 Members | 1,803 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Getting values between forms

Hello,

I'm have defined a combobox on form2 and I'm trying to use that value
in form1. I have defined a global value so that form1 can retrieved
the modified value as:

Public Shared TimerMinutes As ComboBox

In form2, this value is modifed with ComboBox1_SelectedValueChanged()
and ComboBox1_SelectedIndexChanged() methods. Once the value has
changed I set the TimerMinutes to ComboBox1:

TimerMinutes = ComboBox1

After this global value has been modified, I try to retrieve the value
in form2 by

Dim F3 As New frmOptions
Dim temp As Integer
temp = F3.TimerMinutes.Text

The value that in TimerMinutes is always the default value. It never
has the modified value. Any help is appreciated.

Dec 30 '06 #1
3 1049
Lakepeir,

if you create a new form than it is a *new* form, that has nothing old on
it, there are many methods to retrieve your problem.

However probably is the most easiest method is the owner method

dim frm as new form2
frm.owner = me
frm.show (or showdialog)

Now you can do in form 2

myvalue = me.WhateverPublicPropertyFromForm1

I hope this helps,

Cor

<la******@yahoo.comschreef in bericht
news:11**********************@48g2000cwx.googlegro ups.com...
Hello,

I'm have defined a combobox on form2 and I'm trying to use that value
in form1. I have defined a global value so that form1 can retrieved
the modified value as:

Public Shared TimerMinutes As ComboBox

In form2, this value is modifed with ComboBox1_SelectedValueChanged()
and ComboBox1_SelectedIndexChanged() methods. Once the value has
changed I set the TimerMinutes to ComboBox1:

TimerMinutes = ComboBox1

After this global value has been modified, I try to retrieve the value
in form2 by

Dim F3 As New frmOptions
Dim temp As Integer
temp = F3.TimerMinutes.Text

The value that in TimerMinutes is always the default value. It never
has the modified value. Any help is appreciated.

Dec 30 '06 #2

la******@yahoo.com wrote:
Hello,

I'm have defined a combobox on form2 and I'm trying to use that value
in form1. I have defined a global value so that form1 can retrieved
the modified value as:

Public Shared TimerMinutes As ComboBox

In form2, this value is modifed with ComboBox1_SelectedValueChanged()
and ComboBox1_SelectedIndexChanged() methods. Once the value has
changed I set the TimerMinutes to ComboBox1:

TimerMinutes = ComboBox1

After this global value has been modified, I try to retrieve the value
in form2 by

Dim F3 As New frmOptions
Dim temp As Integer
temp = F3.TimerMinutes.Text

The value that in TimerMinutes is always the default value. It never
has the modified value. Any help is appreciated.
There are some architectural issues with this code. One issue is the
global reference to the combobox. It is not a very good idea. You
should think of forms as any other class - which means that you should
encapsulate implementation details as much as possilbe. Because a form
is a class - you can create/add properties and events to your form
classes that can be accessed by any client holding a reference (or in
2005, they can use the My.Forms to get the reference). Oh, by the way
- I don't think you have option strict on in your code - or the
following code:

Dim temp As Integer
temp = F3.TimerMinutes.Text

would not compile. You should get into the habbit of always turning on
Option Explicit and Option Strict. There are only a occasional of
scenarios, for example late binding with COM objects, where you might
turn Option Strict off. The good news is that in 2005 you can set this
option to be the default in the IDE (tools -options -projects and
solutions -vb defaults.

In your case - I would probably create a property on your form that
exposed the timer minutes - something like:

Public ReadOnly Property TimerMinutes() As Integer
Get
Return Convert.ToInt32(Me.timerMinutesComboBox.Text)
End Get
End Property

Then, your client could access the value like:

Dim i as integer = My.Forms.Form3.TimerMinutes

You could also, of course keep a private reference to the form in
form2. If you wanted to make this automatic - then you can also create
an event to notify any clients who cared about the change and then
raise the event from the combobox.selectedindexchanged event..

--
Tom Shelton

Dec 30 '06 #3
I'm a little confused by your references. I assume that "frmOptions" is actually
"form2" from your initial discussion. I also assume that "TimerMinutes" is
a member of this same "form2/frmOptions." Using the Shared keyword says,
"No matter how many instances of form2/frmOptions I create, just use a single
version of TimerMinutes among them all. Because of this, it's not possible
to reference shared members using an instance variable. You should instead
reference them using the generic class name.

form2.TimerMinutes

But this is probably not what you want. If you have multiple form2/frmOptions
open, you probably want each one to expose its own value. I would remove
the "Shared" keyword. Then you can reference "F3.TimerMinutes" directly.
If you really want a global variable, use a "Module..End Module" block and
add the global variable inside of it.

Module SomeGlobals
Public TimerMinutes As ComboBox
End Module

Personally, since I only care about the value exposed by the ComboBox, I
would create TimerMinutes as a string, or integer, or whatever, but not a
full ComboBox. Form1 doesn't care that the value came from a ComboBox; it
only cares about the value. Part of encapsulating things in classes is to
hide the inner workings. Why should Form1 care how TimerMinutes came about
within Form2? It should only care about the value itself. Therefore, I would
expose a simple string or integer (whatever meets your needs) and have Form1
access this member instead.

Class Form2
Public TimerMinutes As Integer = 0

Private Sub ComboBox1_SelectedIndexChanged(sender, e)
If (ComboBox1.SelectedIndex <-1) Then
Me.TimerMinutes = CInt(ComboBox1.SelectedItem.ToString)
End If
End Sub

End Class

Class Form1
Private Sub SomeRoutine()
Dim F3 As New Form2
...
MsgBox("Timer Minutes = " & F3.TimerMinutes)
...
End Sub
End Class

As another poster indicated, you should enable Option Strict and Option Explicit.
Letting VB decide how to convert ComboBox.Text to Integer is just asking
for trouble. You should be explicit and use CInt() or one of the other conversion
functions.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005
Hello,

I'm have defined a combobox on form2 and I'm trying to use that value
in form1. I have defined a global value so that form1 can retrieved
the modified value as:

Public Shared TimerMinutes As ComboBox

In form2, this value is modifed with ComboBox1_SelectedValueChanged()
and ComboBox1_SelectedIndexChanged() methods. Once the value has
changed I set the TimerMinutes to ComboBox1:

TimerMinutes = ComboBox1

After this global value has been modified, I try to retrieve the value
in form2 by

Dim F3 As New frmOptions
Dim temp As Integer
temp = F3.TimerMinutes.Text
The value that in TimerMinutes is always the default value. It never
has the modified value. Any help is appreciated.

Dec 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by thomas goodwin | last post: by
5 posts views Thread by Erol | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.