How to pass value from one form to another?

songoku107723

Member
Joined
Apr 18, 2008
Messages
12
Programming Experience
Beginner
hello guys.... i'm ricky and i'm newbie in vb .net...

i want ask how to pass value from one form into another form...???

for example if i have 1 mdiform and 2 form (form1, and form2)

for load form1 i'm using system.reflection.assembly

here is the code for load form1 :

Imports System.Reflection

Private Asm As Assembly
Private FrmNama As Form

Public Function GetForm(ByVal FormName As String) As Form

Try

FrmNama = New Form()
Asm = Assembly.GetExecutingAssembly
FrmNama = CType(asm.CreateInstance(asm.GetName.Name & "." & FormName, True), Windows.Forms.Form)

Asm = Nothing

Return FrmNama

Catch ex As Exception

Throw New Exception("GetForm Function Error : " & vbCrLf & ex.Message.ToString)

End Try

End Function

in mdiform i just call the function

here is the code in mdiform :

F_Nama = New Form()
F_Nama = GetForm(CStr(strName).Trim)
F_Nama.MdiParent = Me
F_Nama.Show()

after form1 load i call form2 from form1 and from form2 i want pass value, here is the code from form2 to pass value into form1 :

form1.textbox1.text = textform2.text

why value from form2 can't fill into textbox1 in form1??

please help me... thank's before... :) and sorry if my english langguage bad... :D
 
Last edited:
try this one:
VB.NET:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       textbox1.text = form1.textbox1.text
    End Sub
 
thank's to trialer... :D

i was try it but i still can't get the value from form2... i don't know why.. but i think the problem because i was load the form1 using system.reflection.assembly...

there's another way to get the value from form2 ?

thank's a lot... :D
 
You should probably setup properties on your second form to recieve the information, so you can just set it in the first form.

On form2

VB.NET:
Private _int1 As Integer

Public Property int1() As Integer
    Get
        Return _int1
    End Get
    Set(ByVal value As Integer)
        _int1 = value
    End Set
End Property


Sorry if my syntax isnt correct, been programming in C# and java too long, lol.

Then, when you call your form from the first one, do this

form2.int1 = value (where value is your variable you want to pass)
 
Last edited by a moderator:
You could also try setting the modifier on the textbox on form1 to public, so that it can be accessed outside of its own instance. Though this is not recomended for security reasons. I would just set a property on form1 to recieve it, then on form2 just access form1's property that you made, and set the value.
 
thank's compguru910... :D

hmmm... i was try it but still i can't get or set the value from form2 into form1... i think it because i was load the form using system.reflection

if i load the form using system.reflection i think my form1 it's not like the form1 that i build in my solution... is that true???

sorry for my bad english langguage... hehehe... :p
 
yupz... hehehe... i want to load my form dynamically from database... so i use reflection... to get my form name and load the form...
in my databse i juzt set the form name...

there is another way to load form dynamically without using reflection???
 
In VB.NET we can receive and handle events in 2 ways. The first one is using WithEvents and Handles keywords. The second way is to use the AddHandler method and dynamically add event handlers through our code. We can also use RemoveHandler to dynamically remove them. WithEvents and Handles clause we have to declare the object variable and the event handler as we write our code so linkage is created upon compilation. On the other hand with AddHandler and RemoveHandler linkage is created and removed at runtime, which is more flexible. Let’s assume that we want to load several MDI child forms, allowing each of them to be loaded only once and of course to know when one of the child forms is closed. Since we have several forms to load we would like to use the AddHandler and RemoveHandler keywords so we can be flexible and write the minimal code we can.
 
thank's trialer...

like u said... i want to load several mdi child forms from mdi parent... but i use toolstrip menu to add the menu and sub menu...

here is my code :
VB.NET:
[FONT="Arial"]    Public Sub LoadMenuList()
        Try

            'Isi Data View Header

            Try

                strSql = String.Format("DECLARE @Kode_Role CHAR(4); " & _
                                       "SELECT " & _
                                       "@Kode_Role = Kode_Role_User " & _
                                       "FROM " & _
                                       "MasterUserName " & _
                                       "WHERE " & _
                                       "Kode_User = '{0}'; " & _
                                       "SELECT " & _
                                       "A.Kode_MenuHeader, " & _
                                       "B.Kode_Role_Permission, " & _
                                       "A.Nama_MenuHeader " & _
                                       "FROM " & _
                                       "DaftarMenuProgramHeader A " & _
                                       "INNER JOIN " & _
                                       "DaftarPermission B " & _
                                       "ON A.Kode_MenuHeader = B.Kode_MenuHeader_Permission " & _
                                       "WHERE " & _
                                       "B.Kode_Role_Permission = @Kode_Role " & _
                                       "AND " & _
                                       "B.Kode_User_Permission = '{1}' " & _
                                       "AND " & _
                                       "B.Status_Permission = 1 " & _
                                       "GROUP BY " & _
                                       "A.Kode_MenuHeader, " & _
                                       "B.Kode_Role_Permission, " & _
                                       "A.Nama_MenuHeader", _
                                       CStr(strKd_User).Trim, _
                                       CStr(strKd_User).Trim)

                objDv = New DataView(GetQueryData(strSql).Tables(0))

            Catch ex As Exception

                Throw New Exception("Load Header Function Error : " _
                                    & vbCrLf & ex.Message.ToString)

            End Try

            If objDv.Count > 0 Then

                For i As Integer = 0 To objDv.Count - 1

                    tsmChild = MsListMenu.Items.Add(CStr(objDv.Item(i).Item("Nama_MenuHeader")).Trim, Nothing)

                    'Isi Data View Detail

                    Try

                        If objDv.Count > 0 Then

                            strSql = String.Empty

                            strSql = String.Format("SELECT " & _
                                                   "A.Kode_Role_Permission, " & _
                                                   "C.Nama_MenuDetail, " & _
                                                   "C.Link_MenuDetail " & _
                                                   "FROM " & _
                                                   "DaftarPermission A " & _
                                                   "INNER JOIN " & _
                                                   "MasterRole B " & _
                                                   "ON B.Kode_Role = A.Kode_Role_Permission " & _
                                                   "INNER JOIN " & _
                                                   "DaftarMenuProgramDetail C " & _
                                                   "ON A.Kode_MenuDetail_Permission = C.Kode_MenuDetail " & _
                                                   "AND " & _
                                                   "C.Kode_MenuHeaderDetail = '{0}' " & _
                                                   "WHERE " & _
                                                   "A.Kode_Role_Permission = '{1}' " & _
                                                   "AND " & _
                                                   "A.Kode_MenuHeader_Permission = '{2}' " & _
                                                   "AND " & _
                                                   "A.Status_Permission = 1 ", _
                                                   CStr(objDv.Item(i).Item("Kode_MenuHeader")).Trim, _
                                                   CStr(objDv.Item(i).Item("Kode_Role_Permission")).Trim, _
                                                   CStr(objDv.Item(i).Item("Kode_MenuHeader")).Trim)

                            objDvChild = New DataView(GetQueryData(strSql).Tables(0))

                            If objDvChild.Count > 0 Then

                                For j As Integer = 0 To objDvChild.Count - 1

                                    tsmChild.DropDownItems.Add(CStr(objDvChild.Item(j).Item("Nama_MenuDetail")).Trim, _
                                                               Nothing, _
                                                               New EventHandler(AddressOf MsListMenu_OnClick))

                                    tsmChild.DropDownItems(j).Name = CStr(objDvChild.Item(j).Item("Link_MenuDetail")).Trim

                                Next

                            End If

                        End If

                    Catch ex As Exception

                        Throw New Exception("Load Detail Function Error : " _
                                            & vbCrLf & ex.Message.ToString)

                    End Try

                Next

                tsmChild = MsListMenu.Items.Add("Keluar", Nothing)
                tsmChild.DropDownItems.Add("Logout", Nothing, New EventHandler(AddressOf MsListMenu_OnClick))
                tsmChild.DropDownItems(0).Name = "Logout"
                tsmChild = MsListMenu.Items.Add("Window", Nothing)
                tsmChild = MsListMenu.Items.Add("Lbl2", Nothing)
                tsmChild.Name = "Lbl2"

            End If

        Catch ex As Exception

            Throw New Exception("LoadMenuList Function Error : " _
                                & vbCrLf & ex.Message.ToString)

        Finally

            strSql = String.Empty

        End Try

    End Sub

    Private Sub MsListMenu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)

        Try

            If sender.GetType() Is GetType(ToolStripMenuItem) Then

                Dim strName As String = ((CType(sender, ToolStripItem)).Name.Replace(" ", ""))

                Select Case strName

                    Case "Logout"

                        Me.Close()
                        strKd_User = String.Empty
                        strNama_User = String.Empty
                        strPassword_User = String.Empty
                        strKd_Role = String.Empty

                        F_Login.TxtUserName.Clear()
                        F_Login.TxtPassword.Clear()

                        F_Login.Show()
                        F_Login.TxtUserName.Focus()

                    Case Else

                        If isOpened(CStr(strName).Trim) Then

                            F_Nama = New Form()
                            F_Nama = GetForm(CStr(strName).Trim)
                            F_Nama.MdiParent = Me
                            F_Nama.Show()

                        End If

                End Select

            End If

        Catch ex As Exception

            Throw New Exception("MsListMenu_OnClick Error : " _
                                & vbCrLf & ex.Message.ToString)

        End Try

    End Sub

    Private Function isOpened(ByVal strFormName As String) As Boolean

        Try

            For Each F_Check As Form In Application.OpenForms

                If F_Check.Name.Trim = strFormName.Trim Then

                    Return False

                End If

            Next

        Catch ex As Exception

            Throw New Exception("isOpened Function Error : " _
                                & vbCrLf & ex.Message.ToString)

        End Try

        Return True

    End Function

    Public Function GetForm(ByVal FormName As String) As Form

        Try

            FrmNama = New Form()
            Asm = Assembly.GetExecutingAssembly
            FrmNama = CType(Asm.CreateInstance(Asm.GetName.Name & "." & FormName, True), Windows.Forms.Form)

            Asm = Nothing
            CalledForm = FrmNama

            Return FrmNama

        Catch ex As Exception

            Throw New Exception("GetForm Function Error : " & vbCrLf & ex.Message.ToString)

        End Try

    End Function[/FONT]

i load my list form name from my database... after that i set the name into the toolstripmenu and create new event handler to set the toolstripmenu event...

in this toolstripmenu i just send the toolstripmenu name into the event handler name.... but if i want to load the form only with the toolstripmenu name it can't... because it type like a variable not the object form... is that true?? so i use reflection to check form in my solution with my toolstripmenu name as the parameter name.. if there is the same name so i will load this form...

can u give me a little example for load this menu without checking the form name in my solution using reflection ? i'm still newbie in vb .net... i'm still confuse with this problem... >.<

thank's a lot trialer... :D
 
Last edited:
Im not to familiar with system reflection, but couldnt you write code in your new form to gether the information you need from the database when its called. And when you call the new form, you could create a new instance of a prebuilt form, somethig like this

form = new Form2()
form.showDialogue()
form.property1 = blah


Like I said, its been a while since I have used VB, so my syntax is probably off, but thats how I generally did it. I passed an id or something in a property to the new form, then on the Form_Load() event, I just made it gather from the database the information it needed and populated textboxes and labels that way. Or am I just way off with understanding what your trying to do?
 
thank's compguru...

no, us not like u say i must load or gathering data and populated textboxes and labels in the form from sql when my form loaded...
the problem is when i load the form1 which i generate dynamically using reflection... i must call the form2 which i call the form2 from form1 button

so in form1 i have the button for load the form2... after i click the button form2 was shown.. and there is a textbox in form2 and textbox in form2 was fill by the value from sql and this values was call when the form2 load... in form2 i have one button to send each value from textbox in form2 into the texboxt in form1 and i close the form2...

but i can't send the value in each textbox form2 into texbox in form1... i don't know why.. >.<

by the way... thank u verry much compguru... :)
 
Ok, what I would do if I were you, is set the modifier of the textbox on form1 to public , and then you should be able to access form1's textbox from form two. Or, make a public property on form1 that automatically sets the value of the textbox. So, something like this

private string _value

public string value
set (_value = value
textbox.text = value
)

Something like that, just reverse of what I told before. So instead of putting propertys on form2, put them on form1, then in form2, just set the property of form1 like this

form1.value = textbox.text

That should work. Again, I apologize if my syntax isnt correct.

After I get off work, ill make a sample project to show you how this works if that doesnt hlep
 
i was try like what u said... but i still can't get the value from form2.... >.<

can u give some examples how to obtain value from form B and sent to form A where A is loaded dynamically ? either in C# or VB .net is fine... :)

thank's compguru910... :D sorry for my bad english... :D
 
Ok, I wrote something up that should help. Its very simple. It just uses the showdialogue, and with the returned value it places it in the labels textfield. Now, the first form isnt dynamic, but, I dont see how that should make a difference. When you run the program, it automatically brings up Form2, if you type something into the box, and hit ok, then it goes back to form1 with the value in the label. My email is compguru910@gmail.com, send me an email and ill email the project I made to you. It wont let me post it here
 

Latest posts

Back
Top