Results 1 to 8 of 8

Thread: [RESOLVED] Trouble understanding what constitutes a valid argument!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Resolved [RESOLVED] Trouble understanding what constitutes a valid argument!

    I can’t seem to find the right argument to make the high lighted call work. TIA JP

    Code:
      Private Sub FindButton_Click(sender As Object, e As EventArgs) Handles FindButton.Click
    
            ' Check fo a non blank SearchForTextBox then go to RadioButtons_CheckedChange(ByVal
            If SearchForTextBox.Text <> "" Then
                RadioButtons_CheckedChange(???????????)
            Else
                MessageBox.Show("Please enter search argument", "Input Check!", MessageBoxButtons.OK,
                                MessageBoxIcon.Question)
                SearchForTextBox.Focus()
            End If
        End Sub
    
        Private Sub RadioButtons_CheckedChange(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles StateNameRadioButton.CheckedChanged, StdAbrvRadioButton.CheckedChanged, PostalAbrvRadioButton.CheckedChanged,
                        PostalAbrvRadioButton.CheckedChanged
            Dim SelectedRadioButton As RadioButton
            SelectedRadioButton = CType(sender, RadioButton)
    
            Select Case SelectedRadioButton.Name
                Case "StateNameRadioButton"
                    SearchByName()
                Case "StateAbrvRadioButton"
                    SearchbyStdAbrv()
                Case "StateAbrvRadioButton"
                    SearchByPostal()
                Case "StateAbrvRadioButton"
                    SearchByCapitalCity()
            End Select
        End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Trouble understanding what constitutes a valid argument!

    It's solved by not calling an event handler directly, but by putting the code into its own method and calling that.:
    Code:
      Private Sub FindButton_Click(sender As Object, e As EventArgs) Handles FindButton.Click
    
            ' Check fo a non blank SearchForTextBox then go to RadioButtons_CheckedChange(ByVal
            If SearchForTextBox.Text <> "" Then
                DoSearch(SearchForTextBox.Text) ' ?? is that right?... not sure it is
            Else
                MessageBox.Show("Please enter search argument", "Input Check!", MessageBoxButtons.OK,
                                MessageBoxIcon.Question)
                SearchForTextBox.Focus()
            End If
        End Sub
    
        Private Sub RadioButtons_CheckedChange(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles StateNameRadioButton.CheckedChanged, StdAbrvRadioButton.CheckedChanged, PostalAbrvRadioButton.CheckedChanged,
                        PostalAbrvRadioButton.CheckedChanged
            Dim SelectedRadioButton As RadioButton
            SelectedRadioButton = CType(sender, RadioButton)
            DoSearch(SelectedRadioButton.Name)
    
        End Sub
    
    private sub DoSearch(SearchName as string)
            Select Case SearchName
                Case "StateNameRadioButton"
                    SearchByName()
                Case "StateAbrvRadioButton"
                    SearchbyStdAbrv()
                Case "StateAbrvRadioButton"
                    SearchByPostal()
                Case "StateAbrvRadioButton"
                    SearchByCapitalCity()
            End Select
    end sub
    
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trouble understanding what constitutes a valid argument!

    (techgnome was quicker, and basically posted the same code I did.)

    The answer is nothing. You should not call event handlers directly like this. Someone will disagree, and probably show you a way to do it with no explanation. I'd argue they're wrong.

    Event handlers are just plain old subs and sure, if you figure out how to synthesize the arguments you can call them directly. But eventually you write a program where you want the same thing to happen when two different events happen. You can copy/paste the code, but that will cause problems down the road.

    So it's a lot easier to separate "what I want to happen" from "when I want it to happen". The easy case is a "Save" button that you also want to execute when a timer ticks. The setup looks like:
    Code:
    Private Sub Timer1_Tick(...) Handles ...
        Save()
    End Sub
    
    Private Sub SaveButton_Click(...) Handles ...
        Save()
    End Sub
    
    Private Sub Save()
        ' Do things to save the file
    End Sub
    This is easier to maintain, and a month from now you'll find it much more obvious that "code to save the file" is in "Save()" than "It's in Button1_Click()."

    Now, your case is a little more complex, because you do use the 'sender' parameter to affect how you search. One thing you didn't think of when writing this: CheckedChange is raised twice when radio buttons change: once for the one that loses selection, and once for the one that gains selection. Given that you still have to do a Select..Case statement, it's probably better to drop that and just query which button is checked when you want to do a search. Here's how I'd write it:
    Code:
    Private Sub FindButton_Click(...) Handles ...
        If SearchForTextBox.Text <> "" Then
            PerformSearch()
        Else
            ...
        End If
    End Sub
    
    Private Sub RadioButtons_CheckedChange(...) Handles ...
        Dim theButton As RadioButton = CType(sender, RadioButton)
    
        If theButton.Checked Then
            PerformSearch()
        End Sub
    End Sub
    
    Private Sub PerformSearch()
        If StateNameRadioButton.Checked Then
            SearchByName()
        Else If StdAbrvRadioButton.Checked Then
            SearchByStdAbrv()
        Else If SearchByPostalRadioButton.Checked Then
            SearchByPostal()
        Else If CapitalCityRadioButton.Checked 
            SearchByCapitalCity()
        End If
    End Sub
    (I noticed when typing this you have PostalAbrvRadioButton twice in your code, I assume you have a CapitalCityRadioButton variable?)

    This moves most of the logic into a method named PerformSearch(). It does a little work in the CheckedChanged handler to make sure it's only calling PerformSearch once, after the correct radio button is selected.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: Trouble understanding what constitutes a valid argument!

    Thanks all for your response. I used this logic in another class project and for the life of me, I can't figure out how it got executed. I will rip it out and start over. Thanks again! JP

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

    Re: Trouble understanding what constitutes a valid argument!

    To understand where the arguments passed to an event handler come from, I suggest that you follow the Blog link in my signature below and check out my post on Custom Events.

    While I agree with the others who have replied and strongly recommend that you never call an event handler directly, I'll explain how you could, for the sake of your understanding. Any event handler for an event that follows the intended pattern will have two parameters: 'sender' is the object that raised the event and 'e' is the data for the event. If you don't use either parameter in the method then you could actually simply call the method and pass Nothing to each parameter. You could pass values but, if they're never used then it wouldn't matter what they were.

    If you wanted to make the parameters meaningful then you should pass the object that you are simulating the event for to the 'sender' parameter. If the 'e' parameter is type EventArgs then you should pass EventArgs.Empty, which is what events do. That's simply a placeholder for events that have no data. If 'e' is some other type then you need to create an instance of that type and set its properties appropriately. That would mean different things in different situations.
    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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: Trouble understanding what constitutes a valid argument!

    Thanks for the blog connection and your instructive response. In the successful class project I executed the following actions: (1) checked TitleCheckBox , then the display button. (2) The title was displayed and the CountryGroupBox containing 5 country labeled RadioButtons was made visible. (3) I selected a radio button and immediately the program displayed the appropriate national flag.

    Question: What event triggered the picture display? Making the CountryGroupBox visible? Or selecting a radio button or the combination of the two actions? TIA JP

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Trouble understanding what constitutes a valid argument!

    You only posted some of your code. I can't answer questions about code I can't see. But let's think this through.

    If the picture only displayed after (3), it would likely be in response to an event handler triggered by (3). So it's probably a CheckedChanged event handler. But it sounds like that can't happen unless you perform (2), because the radio buttons are not visible until (2) happens. And it sounds like (2) is the result of a button being clicked as part of (1). It's also possible that parts of (1) and (2) set variables that are used by (3).

    So the answer is "both". The picture probably displayed as the direct result of selecting a radio button. But you can't select a radio button until you push a button to make them visible.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: [RESOLVED] Trouble understanding what constitutes a valid argument!

    Thanks for the understanding reply. JP

Tags for this Thread

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