Question Reference Form Using Variable

rgately

New member
Joined
Aug 17, 2010
Messages
4
Programming Experience
1-3
I have an application consisting of 2 forms (Form1 and Form2).

There is a text box on each form and they are both named Text1.

From a sub routine in a module, I want to alter a text box on one of the forms. The sub determines which form to write to and attempts to refer to the form using a variable.

But in the example below, gForm never gets declared when it’s nested within the If statement.

Public Sub DoSomething
If gState = True then
Dim gForm as Form1
Else
Dim gForm as Form2
End if
gForm.Text1.text = ”Hello”
End Sub

This example fails because Form and Form1 (or Form2) are not directly compatible.

Public Sub DoSomething
Dim gForm as Form
If gState = True then
gForm = Form1
Else
gForm = Form2
End if
gForm.Text1.text = ”Hello”
End Sub

The actual application is not so simple and I need to use this or a similar approach throughout the application in order to reduce code redundancy.

To recap, I want to alter a control on a form by replacing the form name with a variable name.

For example, instead of…
Form1.Text1.Text = ”Hello”
I want…
MyVar.Text1.Text = ”Hello”

How can I accomplish this? Thanks!
 
You can CType/DirectCast to the type that have the member, using TypeOf or GetType comparison to determine it.
Using the type of your own base Form class (not the basic Form type defined by .Net) can also be done, where you add the common member to the base class have your more specific forms inherit it.
When you need to handle objects of different types as if they were same type, apart from using base types, you can also define a common Interface type that you implement in the different classes.
 
John, thanks for your feedback. I will take a closer look at CType. I didn't try it before with the TypeOf or GetType.
 
As hard as I tried, I could not come up with a solution based upon your suggestions. The main reason is because I am a VB.Net novice and do not understand the language fully.

However, the following code does what I want it to do except that it cannot find a control when it is contained by another control.

Public Sub DoSomething
Dim gForm as Control
If gState = True then
gForm = Form1
Else
gForm = Form2
End if
gForm.Controls(“Text1”).text = ”Hello”
End Sub

I’m still working on it and am hoping that the solution will turn out to be simple.

I’d be pleased to hear any more ideas. Thanks!
 
I was finally able to kludge something together.

I created 2 functions. One to determine which form to search and the other to search for the control even if it is inside of containers.

Now I can use a single line to make changes to controls on forms without having to copy my code for each form (hard to explain).

Public Function FindControl(ByVal ParentControl As Control, ByVal ControlTobeSearched As String) As Control
Dim FoundControl As New Control
For Each CurrentControl As Control In ParentControl.Controls
Diagnostics.Debug.WriteLine(CurrentControl)
If (CurrentControl.Name = ControlTobeSearched) Then
FoundControl = CurrentControl
Exit For
End If
If (CurrentControl.HasChildren) Then
FoundControl = FindControl(CurrentControl, ControlTobeSearched)
End If
Next
Return FoundControl
End Function

Public Function FindForm() As Control
Dim fform As Control = Nothing
If gInputMode = "Pointer" Or gInputMode = "Touch" Then
fform = frmMain
ElseIf gInputMode = "Switch" Then
fform = frmScan
End If
Return fform
End Function


When needed, I use this line to modify a control...

DirectCast(FindControl(FindForm, "Text1"), Control).Text = “Hello”

Thanks for your help.

I am satisfied with how the code works but if anyone has better, simpler, way of doing it - I'd be happy to hear about it.
 
Back
Top