Results 1 to 4 of 4

Thread: [02/03] What Button on Another Form Did I click?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    7

    Question [02/03] What Button on Another Form Did I click?

    Hi,

    I have done this in VB6 but can't find the syntax in VS 2003. I have a form (choice form) that has two buttons that both open another form (a search form). One button is supposed to open a basic search and the other is supposed to open a flash search.

    I can't find a simple way to determine which button I've pressed on the other form. I am guessing it's hiding somewhere in the sender event but I just can't get to it.

    My other option is to use a hidden label on the "choice" form that has text saying which is clicked but I can't figure out how to access the value of the hidden label once I am on the second "search" form.

    Can anyone help me with these tasks?

    Thanks,
    Andrew
    Andrew - Probably Using VB.NET 2003 and Most Likely

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [02/03] What Button on Another Form Did I click?

    If you show the searchForm by ShowDialog mode then you can assign the seachForm dialog result to OK or what ever you want and after the form is closed you can check which button was clicked.
    VB Code:
    1. 'Form 1
    2. Public Class Form1
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         Dim fr2 As New Form2
    6.         If fr2.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             'The OK button was clicked
    8.         End If
    9.  
    10.     End Sub
    11. End Class
    and
    VB Code:
    1. 'Form 2
    2. Public Class Form2
    3.     Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
    4.         Me.DialogResult = Windows.Forms.DialogResult.OK
    5.         Me.Close()
    6.     End Sub
    7. End Class

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [02/03] What Button on Another Form Did I click?

    If you need to know from a different form, some fact about the first form. Well, another way to put that would be that you want to know a property of the first form. So why not use a ReadOnly property added to the first form that exposes a boolean or integer that informs you which button was pressed. Offhand, I can't think of a way to do this other than to have a member variable on the first class that holds this. You would set the variable in the button click, and expose the variable through the property.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] What Button on Another Form Did I click?

    Sorry, are you saying that in form1 you have two buttons that both open form2, but form2 is supposed to behave differently depending on which button you pressed? If so then you need to think of this slightly differently. form2 doesn't actually care what button is pressed. It only cares what it is told to do. You might want to actiavet the same functionality from a menu item or something else completely unrelated to either button. You need a way to pass an instrcution to this form. The most logical way to do that is through the constrcutor. You write one or more constructors that take parameters that control how the form behaves. If you only have two choices then you might have one argument of type Boolean:
    VB Code:
    1. Public Sub New(ByVal flash As Boolean)
    2.     ' This call is required by the Windows Form Designer.
    3.     InitializeComponent()
    4.  
    5.     ' Add any initialization after the InitializeComponent() call.
    6.  
    7.     If flash Then
    8.         'Perform a flash search.
    9.     Else
    10.         'Perform a basic search.
    11.     End If
    12. End Sub
    You then pass a Boolean value to the form when you create it. The basic button would pass False to the argument and the flash button would pass True.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width