Greets to all!

I have 2 user defined controls.
1 is the main, the other is the control that contains all the radio buttons. One of the criteria is to have a clear button in the main control to select the 0 radio button that clears all the text fields (which the radio buttons are in the other control). My prof told us that the easiest way to accomplish this is to use the InvokeOnClick method...here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokeonclick.aspx

But that completely doesn't help!!! Googling it doesnt have...ANY info on it! Please if anyone understands how to do InvokeOnClick, or has another method that achieves the same task please please please let me know!!

Thanks in advance!

So that is what they teach! Augh!!!!!!!!!!!!!!

If you have a block of code that needs to be called by multiple event handlers, it is best to define that code block as a separate method (subroutine) and call it as needed.

Something like this pattern.

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
   If RadioButton1.Checked Then DoSomething()
End Sub

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

Sub DoSomething()
   'well, I would do something if I knew what to do.
End Sub

To programmaticallly click a button, you could do this:

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

But if you really want to use Control.InvokeOnClick:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
   Me.InvokeOnClick(Button2, New EventArgs)
End Sub

This assumes that "Me" is a class that has the Control class somewhere in it's inheritance tree.

Edited 4 Years Ago by TnTinMN

Thanks for your answer TnTin! Here are some code snippets will help understanding my problem:

This is the main Calculator class:

Private Sub ButClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClr.Click
        courseTab.InvokeOnClick(sender, e)
    End Sub

And the other class would be the CourseTab class:

Private Sub RadioButton0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton0.Click
        Dim button As RadioButton = sender
        If button.Name = "RadioButton0" Then
            clear()
        End If
    End Sub

Public Sub clear()
        For i = 0 To courseArray.Length - 1
            courseArray(i).Enabled = False
            courseArray(i).CourseCodeLabel.Clear()
            courseArray(i).NumGrade.Clear()
            courseArray(i).LetterGrade.SelectedIndex = 0
            courseArray(i).LetterGrade.ClearSelected()
            courseArray(i).FullCourseBox.Checked = False
        Next i
    End Sub

I do have a 3rd control which is called Course, which I have 6 Course controls in a single CourseTab control. So I put all 6 Course controls into an array, hence "courseArray(i)" (looping through the 6 Course controls).

I am trying to get the main class "ButClr_Click" to call the "RadioButton0_Click" in the CourseTab, which inturn should call the clear() method in CourseTab; EXCEPT, it does call, but whatever is in the method...it doesn't do... Please help! =(

Ok, let's walk this through.

If RadioButton0 is clicked, it's "Checked" property will be True. Every time this happens you run the "clear" method. You also want the to execute the "clear" method by clicking ButClr that is parented by the "Calculator" class. It appears that your reference to the "CourseTab" class is named "courseTab". "clear" is defined as a Public method which means it can be called in ButClr_Clicked by adding the following statement to the "ButClr_Clicked" method:

`courseTab.clear`

The only thing that calling the method like this will not accomplish is setting RadioButton0's "Checked" state to True. Since you have not shown that you are handling the RadioButton0.CheckChanged event, there appears to be no reason that setting this in the "clear" method would be a problem. So you could add the follow statement to the "clear" method.

RadioButton0.Checked = True

With that stated, I will not try anymore to steer you way from the programming practice of programmatically clicking buttons.

InvokeOnClick is a protected method, meaning that sans using Reflection, you can only invoke it in the inherited class in which the statement resides. You are trying call it as a public method. It won't work.

You could do something like this though:

Me.InvokeOnClick(courseTab.RadioButton0, New EventArgs)

Take note that this will only work if RadioButton0 is declared Public or if it is delcared Friend and the declaration resides in the same assembly from which it is being accessed.

This is exactly what I'm looking for! Thanks SOOO much TnTin!

I really want to apologized for the fact that I missed out telling you guys the most important piece of information that I didn't know can affect the whole thing.

The control, CourseTab, is placed on different TabPages on a TabControl.
Yes..sorry! You are probably thinking, "Exactly! That makes it COMPLETELY different!"

So to update, I have 2 classes, Calculator and CourseTab classes. The clear button, and TabControl is in Calculator class, and the button is supposed to clear the stuff thats in the TabPages of that TabControl.

Afer reading my marking scheme, I am missing a few things after my previous post. So when I fixed that I did it the following way, instead of doing InvokeOnClick. And also realized that this way is actually easier than what my prof said that InvokeOnClick is easier. Theoretically, .....Continued at the bottom ~~

Here are the relevant codes:
And I apologize for the "courseTab" in the previous code because I had declared it, but I didn't show it in the code snippets.

Calculator Class:

Private Sub ButClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButClr.Click
        CourseTabFOS.clear()
        CourseTabOther.clear()
    End Sub

Yes, so I realized you MUST call the specific TabPages in order for the CourseTab function clear() would work. Indeed this works.
~~ Continued from the top. Now theoretically, if we replace the CourseTabFOS.clear() with CourseTabFOS.InvokeOnClick(RadioButton0, New EventArgs), or something alone the lines of that SHOULD work. I haven't had the time to test it. I will once I get the time next week (Next week is reading week for me Yay!). Then reading TnTin's last post, InvokeOnClick IS a protected method; so my theory up there ^, already won't work. I will post up if I end up getting InvokeOnClick to work with my code!

CourseTab Class:

Private Sub RadioButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Handles RadioButton0.Click, RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click, RadioButton5.Click, RadioButton6.Click
        Dim button As RadioButton = sender
        If button.Name = "RadioButton0" Then
            clear()
        End If
    End Sub
Public Sub clear()
        For i = 0 To courseArray.Length - 1
            courseArray(i).Enabled = False
            courseArray(i).CourseCodeLabel.Clear()
            courseArray(i).NumGrade.Clear()
            courseArray(i).LetterGrade.SelectedIndex = 0
            courseArray(i).LetterGrade.ClearSelected()
            courseArray(i).FullCourseBox.Checked = False
        Next i
    End Sub

This part, is essentially how I had it before; and it works the way it is supposed to! And one of the requirements was to put all 7 RadioButtons in one event handler. Which is why its Handling all 7 RadioButtons.

In the end, we don't necessarily need to use InvokeOnClick(sender, New EventArgs) as we programmers should already understand that there is ALWAYS different ways to accomplish the same functionality. Just cause my prof said that InvokeOnClick(...) is the easiest way, I decided to go into depth searching for answers as a starter.

Thanks TnTinMN
InvokeOnClick(Me, New Eventargs) worked.
"New Eventargs" was the key.

This question has already been answered. Start a new discussion instead.