newbie wanna ask... please help

let say i have a form, in this form i make a menu, if i click it, it will show a form, mini form (sub form), in this form i put a textboxt and a button, the textboxt contain some value and changeable, if i push the button, this mini form will close and the value in it will use in the main form, how can i do this?

thanks for the help...

Dani AI

Generated

A simple, maintainable pattern is to treat the mini form as a modal dialog that exposes a public property (for the textbox value) and a public OK flag (or DialogResult). This keeps the main form and subform loosely coupled, avoids global state, and makes it easy to pre-fill the subform and read a final value when it closes. ’s direct control-to-control copy works but couples forms; ’s module variable works but introduces global state.

Example (VB6-style): Form2 exposes a property and sets an OK flag when its button is clicked.

' In Form2 (subform)
Public OKPressed As Boolean

Public Property Get InputValue() As String
  InputValue = txtBox.Text
End Property

Public Property Let InputValue(ByVal v As String)
  txtBox.Text = v
End Property

Private Sub cmdOK_Click()
  OKPressed = True
  Me.Hide
End Sub
' In Form1 (main form)
Dim dlg As New Form2
dlg.InputValue = txtMain.Text    ' pre-fill subform
dlg.Show vbModal
If dlg.OKPressed Then
  txtMain.Text = dlg.InputValue ' read back only if OK was used
End If
Unload dlg

For VB.NET the same idea uses ShowDialog and DialogResult; expose a public property on the dialog form and set DialogResult = DialogResult.OK from the OK button, then read the property after ShowDialog returns.

Quick troubleshooting and best practices:

  • Set the initial value via the property before showing the dialog (otherwise Form_Load in the dialog may overwrite it).
  • Hide the dialog from inside it and let the caller unload/Dispose it; this gives the caller control.
  • Prefer properties over touching another form’s controls (keeps UI encapsulated).
  • Avoid module-level globals and default-instance coupling when multiple instances or unit-testing might be needed.

This pattern fits the original question — pre-fill the subform, allow editing, then return the edited value to the main form when the subform’s OK button is used — while keeping the code clearer and safer than cross-form control assignments or global variables.

Recommended Answers

All 3 Replies

Form1.Text1.Text = Form2.Text1.Text

Unload Me

''Form1 is your main form, form2 is the form where you get the data from. After you have the data, close the form...

thanks for reply,

if i want the previous value appear in textboxt (in form2) after i click the menu from form 1 to show that form (form 2), and if the button clicked in form 2, the data automatically send to form 1 and form 2 will closing, how i can do that?

try this
declare public type string variable at module

Public oldstr As String

now On Sub Form (Form2)

Private Sub Command1_Click()
oldstr = Form2.Text1.Text
Form1.Text1.Text = oldstr
Unload Form2
End Sub


Private Sub Form_Load()
If oldstr <> "" Then
Form2.Text1.Text = oldstr
End If
End Sub

hope this helps you . . .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.