Passing Values between forms in Visual Basic

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 9503 Views - Last Post: 06 June 2012 - 06:46 PM Rate Topic: -----

#1 reconnetworks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-May 12

Passing Values between forms in Visual Basic

Posted 04 June 2012 - 09:41 AM

Can anyone give me a solid rundown of how this process works using custom events & EventArgs, Im really having trouble wrapping my head around it. Any help would be appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: Passing Values between forms in Visual Basic

#2 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 288
  • Joined: 22-August 11

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 09:54 AM

you should give us an example of what you're doing.
but after a short google session i found
this
and this
Was This Post Helpful? 0
  • +
  • -

#3 reconnetworks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-May 12

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 12:10 PM

Okay, I did review something similar to those, just having trouble understanding how they work, and what is the best option to proceed, in a previous thread I had been talking to a well respected poster on this forum, and he gave me some examples, I just was having difficulty following them. However here is what I am trying to do.

I have a "new project" section in my software, I have a button to select a new client, the user clicks on the button and a dialog window pops up asking the user to select a client from the database.

Here is the code for client selection;
  Private Sub CustName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustName.TextChanged
        CustName.Text = "Select Your Customer"
        CustName.Enabled = False
    End Sub

    Private Sub CustSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustSelect.Click
        ProjectChooseCustomer.Show()
    End Sub

Here is a snippet from the client selection code:
   Private Sub CustSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustSelect.Click
        If Not LV.SelectedItems.Count = 0 Then
            With LV.SelectedItems.Item(0)
                AddProject.CustName.Text = .SubItems(1).Text
            End With
            Me.Close()
        End If
    End Sub

It has already been explained to me that showdialog() is not a good method for passing the values at all, and I am of the firm understanding as to why now. So passing using a custom event is the approriate method, however I am just so stuck on how this all works, even in the simplest form it confuses me, so I am worried that I wont understand the more complicated methods like passing 10 arguments or values. Any help teaching a total newbie would be greatly appreciated :P
Was This Post Helpful? 0
  • +
  • -

#4 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 01:33 PM

You can always add a module with a public variable which can be accessed from all forms as long as it's public.

Module Module1
Public MyName as String 'Public Variable In Module
End Module

Public Class Form1

Public Sub LoadMe() 'Just a Sub
MyName = "Trev" 'Set the value of the string
End Sub

End Class

Public Class Form2
Public Sub LoadMeInstead() 'Another sub
MyName = "Trev2" 'Setting it otherwise
End Sub
End Class



You can design your form or create it dynamically, and under it's new sub routine allow it arguments to be passed when it is instantiated. Then send whatever data you need to it from there.

Public Class Form1

Private TheNameIGot as string 'Variable enclosed in the object Form1

Public Sub New(Byval YourName as string)
TheNameIGot = YourName 'Setting the value of the variable to the argument passed as YourName
End Sub

Public Function SayHi() As String 'Function that says hi!
Return "Hello " & ThenameIGot
End Function

End Class

Public Class FirstForm 

Public Sub PokeForm1 'Sub that instantiates the Form1 object under the variable named NewForm
Dim NewForm as New Form1("Trev")
MessageBox.Show(NewForm.SayHi)'Messagebox shows the string which is returned by the Function that is called

End Sub

End Class



There are tons of ways and it should not be difficult(otherwise you're doing it wrong! :)) What I think is freaking you out so to speak is that you do not understand the fundamentals of OOP.

This post has been edited by trevster344: 04 June 2012 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 02:03 PM

Events would determine WHEN the value is passed between forms, not HOW. There are plenty of ways to do this, but the solid way is to use a property to hold the value. To pass a value between form x and form y, create a property in y, then in form x set the value of the property in form y.

I do NOT generally recommend adding a module with a public variable. You should only do this if you can't do it any other way.
Was This Post Helpful? 1
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 02:07 PM

I have to agree with you BobRodes, I don't usually use the Module method unless it is the only way. With Properties and passing the arguments I haven't used the module method since my noobie days.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 02:09 PM

Well, let's see if we can get the OP to learn how to use properties then, so he doesn't become a menace. :lol:
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2255
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Passing Values between forms in Visual Basic

Posted 04 June 2012 - 06:27 PM

You can pass arguments via the event, when you raise it. You have create a class the derives from EventArgs and a the properties you require.
Was This Post Helpful? 0
  • +
  • -

#9 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Passing Values between forms in Visual Basic

Posted 05 June 2012 - 08:11 AM

Let's say you have Form1(frmMain) and Form2(frmSettings). On Form1(frmMain) you have two integer variables:
XScale and YScale.
If you are on Form2(frmSettings) and have there two textboxes txtX and txtY and a button btnOK, you can access and change the values on frmMain like this:

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        frmMain.XScale = val(Trim(txtX.Text))
        frmMain.YScale = val(Trim(txtY.Text))
    End Sub


Was This Post Helpful? 0
  • +
  • -

#10 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing Values between forms in Visual Basic

Posted 05 June 2012 - 08:52 AM

View PostAdamSpeight2008, on 04 June 2012 - 08:27 PM, said:

You can pass arguments via the event, when you raise it. You have create a class the derives from EventArgs and a the properties you require.

There's that, too, and it's a more stateless way (and so probably the best way) to handle the requirement--assuming we clearly understand the requirement. However, given the OP's understanding of "events", I'm not sure he knows what an "argument" is yet, let alone "derives", and I'm not entirely sure he knows what a "class" is, either. Properties seemed like the best solution from a pedagogical standpoint.
Was This Post Helpful? 0
  • +
  • -

#11 reconnetworks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-May 12

Re: Passing Values between forms in Visual Basic

Posted 05 June 2012 - 11:05 AM

I guess you could say that the major problem I'm having is the actually ability to wrap my head around it, I would prefer not to have someone just hand me the code (although appreciated), I would like to be able to fully understand it, as I will be using it more and more as time goes on.

So when you say you can pass arguments via the even, when you raise it. I really have no idea what your talking about, but would love to know :)
Was This Post Helpful? 0
  • +
  • -

#12 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Passing Values between forms in Visual Basic

Posted 05 June 2012 - 01:59 PM

Events are something happening.
Raising an event is like clicking a button, or resizing a form or moving the mouse on top of a label, or changing a color or a font. These are events.
The way the program respond to them is controlled by the handlers. If you have a button click, you could have a subroutine that responds to it; the connection is made by the clause "Handles Button1.Click" or "Handles Me.SizeChanged", etc, so the program knows what to do when that button is clicked or that change had occurred. The subroutine could have arguments that provide some extra useful information, but the response to the event is what exists between the line:

Private Sub ...



and the line
End Sub


This post has been edited by ricardosms: 05 June 2012 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#13 reconnetworks  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 30-May 12

Re: Passing Values between forms in Visual Basic

Posted 05 June 2012 - 02:30 PM

Okay so the arguments are like eventargs and object?

so if I wanted to send a value from form a to form b, I need to include in my 'arguments' list what is being passed? I guess thats the part im stuck on now.
Was This Post Helpful? 0
  • +
  • -

#14 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Passing Values between forms in Visual Basic

Posted 06 June 2012 - 07:04 AM

Reconnetworks, suppose you first learn how to pass values from one property to the other. It's easier and you need to know how to do that too. Then, we'll talk about what "arguments" are, in a context where it separated from the concept of events. Then, we'll talk about what events are. That should help you untangle all the concepts. Also, if you would, go to my #10, and let me know if I've assessed your understanding of these ideas correctly.
Was This Post Helpful? 0
  • +
  • -

#15 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Re: Passing Values between forms in Visual Basic

Posted 06 June 2012 - 08:41 AM

There are all kinds of events connected to the controls on your form. Check the attached image. On the left is a combobox where Button1 is selected. On the right is a dropped down combobox that shows a partial list of the events asocciated with button1. All of them can be used by you. BTW this is the development environment from VB.Net express 2010.

Attached Image


If you click the left one you will have a list of all the controls on the form, including the form, also a list of variables, they are objects. If you select one of them, then click on the right combobox, there you will see the events associated with it.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2