3 Replies - 4246 Views - Last Post: 12 February 2010 - 10:37 AM Rate Topic: -----

#1 Katelynn7  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 08-February 10

Passing Variables/Arguments between forms

Posted 08 February 2010 - 02:09 PM

I asked this question on a 2 other forums and didn't get an adequate response, I'm hoping you guys can help.

Also, I'm sure I explained myself well enough, but just in case I didn't, I've attached a jpg with form/control examples w/code, and an explanation of my coding problem.

I appreciate any help! Thanks.

PS - Oh yeah I forget to mention, I'm a newbie to .net

Attached image(s)

  • Attached Image

This post has been edited by Katelynn7: 08 February 2010 - 02:10 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Variables/Arguments between forms

#2 snikmotnairb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 70
  • Joined: 02-May 08

Re: Passing Variables/Arguments between forms

Posted 08 February 2010 - 03:13 PM

I believe this is what you were trying to do if I understood your question.

Code for Form1 (has 3 buttons on it, button1, button2 and button3)
Public Class Form1

    Public whatButtonWasPressed As Byte = Nothing

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        whatButtonWasPressed = 1
        Form2.Show()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        whatButtonWasPressed = 2
        Form2.Show()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        whatButtonWasPressed = 3
        Form2.Show()

    End Sub

End Class



And the code for Form2 (just one button on this form, the OK button)

Public Class Form2

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click

        Select Case Form1.whatButtonWasPressed
            Case 1

                MessageBox.Show("Button1 was pressed")
                ' You can put your process code here

            Case 2

                MessageBox.Show("Button2 was pressed")
                ' You can put your process code here

            Case 3

                MessageBox.Show("Button3 was pressed")
                ' You can put your process code here

            Case Else

                'No valid button info was passed 

        End Select

    End Sub

End Class


Was This Post Helpful? 1
  • +
  • -

#3 June7  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 104
  • View blog
  • Posts: 904
  • Joined: 09-December 08

Re: Passing Variables/Arguments between forms

Posted 08 February 2010 - 03:17 PM

In the button click of each button set a variable that will be used in a case structure then call a sub that opens form2 with "form2name.ShowDialog()"
This will open form2 modally with the warning statement. This form should have two buttons, OK and Cancel. If OK clicked then close form and continue code from from1 calling sub which will now use the variable set by the button click to run the correct procedure selected from Case structure, if Cancel clicked then close form and stop code. Have to pass the Cancel response back to calling sub. Here is link on how to pass value between forms: http://www.c-sharpco...6f-28ab1c57e6e1

An alternate to form2 could be MsgBox() with only two possible responses, OK or Cancel. Since the MsgBox() function can set the value of variable in calling code, elminates issue of passing value between forms.

This post has been edited by June7: 08 February 2010 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 Katelynn7  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 21
  • Joined: 08-February 10

Re: Passing Variables/Arguments between forms

Posted 12 February 2010 - 10:37 AM

View Postsnikmotnairb, on 08 February 2010 - 02:13 PM, said:

I believe this is what you were trying to do if I understood your question.

Code for Form1 (has 3 buttons on it, button1, button2 and button3)
Public Class Form1

    Public whatButtonWasPressed As Byte = Nothing

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        whatButtonWasPressed = 1
        Form2.Show()


Thank you so much snikmotnairb!  Works like a charm!  Gosh, that is just so me... making things more complicated than they actually are.  Thanks again!
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        whatButtonWasPressed = 2
        Form2.Show()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        whatButtonWasPressed = 3
        Form2.Show()

    End Sub

End Class



And the code for Form2 (just one button on this form, the OK button)

Public Class Form2

    Private Sub OkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OkButton.Click

        Select Case Form1.whatButtonWasPressed
            Case 1

                MessageBox.Show("Button1 was pressed")
                ' You can put your process code here

            Case 2

                MessageBox.Show("Button2 was pressed")
                ' You can put your process code here

            Case 3

                MessageBox.Show("Button3 was pressed")
                ' You can put your process code here

            Case Else

                'No valid button info was passed 

        End Select

    End Sub

End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1