Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combine two strings to produce form field name

Status
Not open for further replies.

eseabrook2008

Technical User
Jan 9, 2008
74
CA
Background:
I have a main form with a combo box. Each item in the combo box opens a subform where the users selects certain info and clicks submit which assigns values to public variables and closes the subform. This works as designed. Since I will have multiple subforms, I want to create a function to assign the value of the public variable to a variable on the main form rather than re-typing the code for each item in the combo box.

rather than doing this:
Code:
CASE 1
...
FINALbldgCode = subform1.bldgCode
FINALMultOcc = subform1.MutlOcc
FINALContsChge = subform1.ContsChge
FINALEXTRA = subform1.EXTRA

Case 2
...
FINALbldgCode = subform2.bldgCode
FINALMultOcc = subform2.MutlOcc
FINALContsChge = subform2.ContsChge
FINALEXTRA = subform2.EXTRA

I want to do something like this:
Code:
CASE1
....
formname = "subform1"
FINAL()

Case2
....
formname = "subform2"
FINAL()

Public Sub FINAL()
FINALBldgCodeNo = FORMNAME & ".bldgCodeNo"
FINALMultOcc = FORMNAME & ".MultOcc"
FINALOccChge = FORMNAME & ".OccChge"
FINALContsChge = FORMNAME & ".ContsChge"
FINALEXTRA = FORMNAME & ".EXTRA"

But this doesn't work because it's assigning the string, not the object.
 
Hopefully this will point you in the right direction.

Form1 is the main form, Form2 and Form3 are subforms.

All three forms have TextBox1 and TextBox2 and the objective is to copy the data from the selected subform to the main form. This demo does not ensure that only one of the subforms will exist at any one point in time - presumably your code already handles that.

Two methids are shown:

Code:
Public Class Form1

	Private SubForm As Form = Nothing

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		SubForm = New Form2
		SubForm.Show()

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

		SubForm = New Form3
		SubForm.Show()

	End Sub

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

		'Either .....
		'For Each c As Control In SubForm.Controls
		'	Select Case c.Name
		'		Case "TextBox1"
		'			Me.TextBox1.Text = CType(c, TextBox).Text
		'		Case "TextBox2"
		'			Me.TextBox2.Text = CType(c, TextBox).Text
		'	End Select
		'Next

		'Or ....
		Me.TextBox1.Text = CType(SubForm.Controls("TextBox1"), TextBox).Text
		Me.TextBox2.Text = CType(SubForm.Controls("TextBox2"), TextBox).Text

	End Sub
End Class
 
Sorry, I misread your question. I think this will be a better solution.

Note the WithEvents statement in the SubForm variables declaration. This allows the main form to capture the FormCloising event of the subform.

Code:
Public Class Form1

	Private WithEvents SubForm As Form = Nothing

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

		If SubForm Is Nothing Then
			Select Case ComboBox1.Items(ComboBox1.SelectedIndex).ToString
				Case "Form2"
					SubForm = New Form2
				Case "Form3"
					SubForm = New Form3
			End Select
		End If
		If Not SubForm Is Nothing Then SubForm.Show()

	End Sub

	Private Sub SubForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles SubForm.FormClosing

		'Either .....
		'For Each c As Control In SubForm.Controls
		'	Select Case c.Name
		'		Case "TextBox1"
		'			Me.TextBox1.Text = CType(c, TextBox).Text
		'		Case "TextBox2"
		'			Me.TextBox2.Text = CType(c, TextBox).Text
		'	End Select
		'Next

		'Or ....
		Me.TextBox1.Text = CType(SubForm.Controls("TextBox1"), TextBox).Text
		Me.TextBox2.Text = CType(SubForm.Controls("TextBox2"), TextBox).Text

		SubForm = Nothing

	End Sub
End Class
 
How would you replace the textboxes with variables?

Code:
Me.TextBox1.Text = CType(SubForm.Controls("TextBox1"), TextBox).Text
Me.TextBox2.Text = CType(SubForm.Controls("TextBox2"), TextBox).Text

Code:
VariableMainForm = CType(SubForm.Controls("VariableSubForm"), VARIABLE).???
 
This assumes that the Global variables are in Module (they could be anywhere that is convenient and accessible):

Code:
Module Module1

	Public TextBox1Variable As String
	Public TextBox2Variable As Integer

End Module

The revised Form1 code ...
Code:
Public Class Form1

	Private WithEvents SubForm As Form = Nothing

	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

		If SubForm Is Nothing Then
			Select Case ComboBox1.Items(ComboBox1.SelectedIndex).ToString
				Case "Form2"
					SubForm = New Form2
				Case "Form3"
					SubForm = New Form3
			End Select
		End If
		If Not SubForm Is Nothing Then SubForm.Show()

	End Sub

	Private Sub SubForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles SubForm.FormClosing


		Me.TextBox1.Text = CType(SubForm.Controls("TextBox1"), TextBox).Text
		Me.TextBox2.Text = CType(SubForm.Controls("TextBox2"), TextBox).Text

		TextBox1Variable = CType(SubForm.Controls("TextBox1"), TextBox).Text	'string variable
		Try
			TextBox2Variable = Integer.Parse(CType(SubForm.Controls("TextBox2"), TextBox).Text)	'integer variable
		Catch ex As Exception
			MessageBox.Show("Invalid value in " + SubForm.Name + " : TextBox2")
		End Try

		SubForm = Nothing

	End Sub


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		'To test the global variables
		MessageBox.Show(TextBox1Variable)
		MessageBox.Show(TextBox2Variable.ToString)

	End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top