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

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 1158
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: thomas goodwin | last post by:
Hi, This is a bit long explanation to what is probably a very easy problem: I have query 1: "Select col1 from table 1" I have form1 with: - a textbox for col1 - recordsource for form1 =...
5
by: Erol | last post by:
How do I get a type from a string? I'm retrieving a string value from my database so that I can set my property values dynamically. In the event "Form1_Load", you will see that I'm trying to set...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
6
by: MLH | last post by:
I have a query (SQL below) that operates on values entered by users into an unbound form to append a record to tblAdmin. I do not under- stand the basis for the error. There are some 17 or so data...
15
by: rleepac | last post by:
This is a little complicated but I'll do my best to explain. In my db I have a table called L_AgeCorrection which has the following fields: Age, Sex, Frequency, AgeValue This is a table used to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.