pass back data

  • Thread starter Thread starter Hei
  • Start date Start date
H

Hei

Hi All,

i using .showdialog to show a child form for user input some data,
and i wand to pass back these data to the parent form.
how can i achieve this?

thx.
Hei.
 
Hei,

If you consider the form as a class (as it really is) then you could set
create public properties that are accessible from your parent form.

For instance

Dim child as New Form

child.ShowDialog()
' child does it stuff setting public properties and
' then finally call the Me.Hide method from a command button

' Control is back to the parent
Debug.Writeline(child.Property1)
Debug.Writeline(child.Property2)
' etc..

child.Dispose()
child = Nothing

HTH,
Dan
 
i using .showdialog to show a child form for user input some data,
and i wand to pass back these data to the parent form.
how can i achieve this?

Property Procedures:

Let's say we have a Login Form with this stuff on it:

txtUsername
txtPassword
btnOK
btnCancel

You want to set the following relevant properties on the property sheet:

txtPassword.PasswordChar = *
btnOK.DialogResult = OK
btnCancel.DialogResult = Cancel
Login.AcceptButton = btnOK
Login.CancelButton = btnCancel
Login.FormBorderStyle = Dialog

Then inside the form's code, you would write stuff like this:

Public Class Login
Inherits Windows.Forms.Form

'I am leaving out the designer-generated code

'I am assuming that code outside this assembly should
'never be able to get the username or password,
'hence the Friend access modifier

Friend Property Username As String
Get
Return txtUsername.Text
End Get
Set(ByVal username As String)
txtUsername.Text = username
End Set
End Property

Friend Property Password As String
Get
Return txtPassword.Text
End Get
Set(ByVal password As String)
txtPassword = password
End Set
End Property

'You will probably have other code too. This is a cheesy sample.
End Class

Now, in the application that uses this dialog, you can use it similar to the
way that you use the other common dialog components. 1. make a new one, 2.
set default properties, 3. call ShowDialog, 4. get the results:

Dim f As New Login()
Dim username As String
Dim password As String

f.ShowDialog()

username = f.Username
password = f.Password

f = Nothing

Security Note:

For this particular example, I happened to use a login dialog because it's
something familiar and easy to build as a sample. My code here, though, is
focused on the answer to the question, and not security. I don't think that
any custom login should ever pass the password as a string. If this code
were cleaned up for security purposes, the Password.Get block would return a
Hash of the password, not the password itself.

--
Peace & happy computing,

Mike Labosh, MCSD
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 
Back
Top