Passing Value to Text Box from Routine in Class

InterOrion

New member
Joined
Sep 18, 2009
Messages
2
Programming Experience
3-5
Hey guys,

I'm having a little difficulty Setting the .text value of a Textbox in an MDIChild from a Subby in a standalone class.

I'm at this point:

On Event, this sub is run in an deattached class:

VB.NET:
Public Sub FillFields(ByVal LeadId As Integer)

        MsgBox(da.SelectCommand.CommandText.ToString)
        MsgBox(LeadId)

        da.SelectCommand.Parameters("@LeadId").Value = LeadId

        da.Fill(ds)

        MsgBox("table count = " & ds.Tables.Count)
        MsgBox("row count = " & ds.Tables(0).Rows.Count)
        MsgBox("first item = " & ds.Tables(0).Rows(0).Item("NameLast").ToString())



        LeadsMod2.cboNameTitle.Text = ds.Tables(0).Rows(0).Item("NameTitle").ToString()
        LeadsMod2.txFirstName.Text = ds.Tables(0).Rows(0).Item("NameFirst").ToString()
        LeadsMod2.txLastName.Text = ds.Tables(0).Rows(0).Item("NameLast").ToString()

        MsgBox("Loaded")

    End Sub


I know that the data exists because it displays in the Message Box ( MsgBox("first item = " & ds.Tables(0).Rows(0).Item("NameLast").ToString())) and the executes fine, but for some reason, I can't get the text boxes in LeadsMod2 (Open as MDIChild) to update.


Any ideas?

Thanks!
 
How are you using LeadsMod2? You'll ahve to call it like this if this should work:
VB.NET:
                LeadsMod2.MDIParent = yourParent
                LeadsMod2.Show()

VB.NET:
        If Not LeadsMod2.Visible Then
                LeadsMod2.MDIParent = yourParent
                LeadsMod2.Show()
        End If
        LeadsMod2.cboNameTitle.Text = ds.Tables(0).Rows(0).Item("NameTitle").ToString()
        LeadsMod2.txFirstName.Text = ds.Tables(0).Rows(0).Item("NameFirst").ToString()
        LeadsMod2.txLastName.Text = ds.Tables(0).Rows(0).Item("NameLast").ToString()

Bobby
 
Thanks, Robert.

I'm calling it exactly like that.

LeadsMod2 is always visible.

Infact, the sub is actually called from LeadsMod2 on Button.Click

I thought that maybe I need to the MDIChild somehow, because there is a possibility of multiple instances being opened.
 
because there is a possibility of multiple instances being opened.

Then pass the form instance to FillFields ByReference :-

VB.NET:
Public Sub FillFields(ByRef whatForm as LeadsMod2, ByVal LeadId As Integer)

        MsgBox(da.SelectCommand.CommandText.ToString)
        MsgBox(LeadId)

        da.SelectCommand.Parameters("@LeadId").Value = LeadId

        da.Fill(ds)

        MsgBox("table count = " & ds.Tables.Count)
        MsgBox("row count = " & ds.Tables(0).Rows.Count)
        MsgBox("first item = " & ds.Tables(0).Rows(0).Item("NameLast").ToString())



        whatForm.cboNameTitle.Text = ds.Tables(0).Rows(0).Item("NameTitle").ToString()
        whatForm.txFirstName.Text = ds.Tables(0).Rows(0).Item("NameFirst").ToString()
        whatForm.txLastName.Text = ds.Tables(0).Rows(0).Item("NameLast").ToString()

        MsgBox("Loaded")

    End Sub
 
Inertia, using ByRef only makes sense when you want to send a value type back through the method parameter, or when you want the method to be able to reassign the class reference pointed to by the caller of the method. A class is a reference type so when you want give a method access to an instance you just use the default ByVal parameter, the method can then call methods and get/set properties for this object, but not dereference or swap the object with a new instance.
Passing Arguments by Value and by Reference
Differences Between Passing an Argument By Value and By Reference

A code safety feature even exist in the language to prevent methods with ByRef parameters to change the instance:
How to: Force an Argument to Be Passed by Value
 
Noted and understood - thanks John :)
 
Back
Top