15 Replies - 1835 Views - Last Post: 06 June 2012 - 06:46 PM
#1
Passing Values between forms in Visual Basic
Posted 04 June 2012 - 09:41 AM
Replies To: Passing Values between forms in Visual Basic
#2
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 09:54 AM
#3
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 12:10 PM
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
#4
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 01:33 PM
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!
This post has been edited by trevster344: 04 June 2012 - 01:37 PM
#5
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 02:03 PM
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.
#6
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 02:07 PM
#7
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 02:09 PM
#8
Re: Passing Values between forms in Visual Basic
Posted 04 June 2012 - 06:27 PM
#9
Re: Passing Values between forms in Visual Basic
Posted 05 June 2012 - 08:11 AM
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
#10
Re: Passing Values between forms in Visual Basic
Posted 05 June 2012 - 08:52 AM
AdamSpeight2008, on 04 June 2012 - 08:27 PM, said:
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.
#11
Re: Passing Values between forms in Visual Basic
Posted 05 June 2012 - 11:05 AM
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
#12
Re: Passing Values between forms in Visual Basic
Posted 05 June 2012 - 01:59 PM
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
#13
Re: Passing Values between forms in Visual Basic
Posted 05 June 2012 - 02:30 PM
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.
#14
Re: Passing Values between forms in Visual Basic
Posted 06 June 2012 - 07:04 AM
#15
Re: Passing Values between forms in Visual Basic
Posted 06 June 2012 - 08:41 AM

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.
|
|

New Topic/Question
Reply



MultiQuote





|