PASSING CUSTOM

Hi All,

I have below handler :

    Private Sub rdbJenisGudang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
                rdbGudangLokal.Click, _
                rdbGudangCustomerKonsinyasi.Click, _
                rdbGudangCustomer.Click, _
                rdbGudangKonsinyasi.Click

    End Sub

I want to call that handler like below :

rdbJenisGudang_Click(rdbGudangLokal, xxxxxx)

How could I do it ? What I suppose to pass xxxx ?

Thank you.
LVL 1
emi_sastraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
EventArgs class has no properties to pass any data in. So what you can do is either derive from it and create your own class, or use a variable to pass the information and then just do a PerformClick call on any of the buttons.
emi_sastraAuthor Commented:
Hi CodeCruiser,

-So what you can do is either derive from it and create your own clas
Would you please provide simple sample ?

Thank you.
käµfm³d 👽Commented:
Why are you calling an event handler to begin with? If you want to share the logic you have in the event handler, why not create a separate function that does that logic. Then you can call that function in both places.

Private Sub SeparateFunction()
    ' Do some stuff
End Function

Private Sub rdbJenisGudang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    rdbGudangLokal.Click, _
    rdbGudangCustomerKonsinyasi.Click, _
    rdbGudangCustomer.Click, _
    rdbGudangKonsinyasi.Click

    SeparateFunction()
End Sub

Private Sub FunctionWhereYouWantedToCallHandler
    SomeFunction()
End Sub

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

emi_sastraAuthor Commented:
Please see below code :

Dim rdb As RadioButton = DirectCast(sender, RadioButton)

        cboGudang.DropDownStyle = ComboBoxStyle.DropDownList

        Select Case True

            Case rdbGudangLokal.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Perusahaan(cboGudang, dsTable, False, JenisGudang.glbGudangBarangJadi)

            Case rdbGudangCustomerKonsinyasi.Checked
                cboGudang.DropDownStyle = ComboBoxStyle.DropDown
                mdlFillInvCombo.Fill_Combo_Gudang_Perusahaan(cboGudang, dsTable, True, JenisGudang.glbGudangBarangJadi)

            Case rdbGudangCustomer.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Customer(cboGudang, dsTable, "", JenisGudang.glbGudangBarangJadi, True)

            Case rdbGudangKonsinyasi.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Supplier(cboGudang, dsTable, "", JenisGudang.glbGudangBarangJadi, True)

        End Select


Thank you.
CodeCruiserCommented:
Not sure about your code sample. As Kaufmed pointed, you can refactor the code into a separate function and call it from your event handler.
emi_sastraAuthor Commented:
Private Sub rdbJenisGudang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
                rdbGudangLokal.Click, _
                rdbGudangCustomerKonsinyasi.Click, _
                rdbGudangCustomer.Click, _
                rdbGudangKonsinyasi.Click

        Dim rdb As RadioButton = DirectCast(sender, RadioButton)

        cboGudang.DropDownStyle = ComboBoxStyle.DropDownList

        Select Case True

            Case rdbGudangLokal.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Perusahaan(cboGudang, dsTable, False, JenisGudang.glbGudangBarangJadi)

            Case rdbGudangCustomerKonsinyasi.Checked
                cboGudang.DropDownStyle = ComboBoxStyle.DropDown
                mdlFillInvCombo.Fill_Combo_Gudang_Perusahaan(cboGudang, dsTable, True, JenisGudang.glbGudangBarangJadi)

            Case rdbGudangCustomer.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Customer(cboGudang, dsTable, "", JenisGudang.glbGudangBarangJadi, True)

            Case rdbGudangKonsinyasi.Checked
                mdlFillInvCombo.Fill_Combo_Gudang_Supplier(cboGudang, dsTable, "", JenisGudang.glbGudangBarangJadi, True)

        End Select

    End Sub
CodeCruiserCommented:
Can you describe what this code is doing?
CodeCruiserCommented:
And what you want to pass to it?
emi_sastraAuthor Commented:
I could use rdbGudangLokal.PerformClick()

Just want to know any other way to do it.

Thank you.
CodeCruiserCommented:
Again, can you describe what the code inside click event is doing and what custom information you want to pass to it?
emi_sastraAuthor Commented:
Its task is filling combo based on which rb is clicked.

Clear enough ?

Thank you.
käµfm³d 👽Commented:
What is the reason for this line:

Dim rdb As RadioButton = DirectCast(sender, RadioButton)

Open in new window


I don't see where you are actually using it anywhere.
CodeCruiserCommented:
>Its task is filling combo based on which rb is clicked.

Then why do you want to call it manually? To fill a combo? You already have a method which actually fills the combos. Why not call that?
emi_sastraAuthor Commented:
Dim rdb As RadioButton = DirectCast(sender, RadioButton)
Yes, its a habit, I usually wanted to check its name, thus rdb.Name

Select case rdb.Name

    Case "rdbGudangLokal"
    case ...

End Select
emi_sastraAuthor Commented:
- Then why do you want to call it manually? To fill a combo? You already have a method which actually fills the combos. Why not call that?
Yes, I could use rdb.PerformClick or call the fill combo directly.
As I comment before if we could pass the event args, may be useful for some other function.

Thank you.
CodeCruiserCommented:
Public Class MyEventArgs
   Inherits EventArgs

...
End Class
emi_sastraAuthor Commented:
Sorry, do not get your point.

Thank you.
CodeCruiserCommented:
>As I comment before if we could pass the event args, may be useful for some other function.

I showed you how to derive from EventArgs class (which is passed into button click handler) and add your custom logic to it.
emi_sastraAuthor Commented:
I do not know how to use it.

Would you please provide simple sample ?

Thank you.
CodeCruiserCommented:
Public Class MyEventArgs
   Inherits EventArgs

...
Public MyProperty As String
End Class


Private Sub rdbJenisGudang_Click(ByVal sender As System.Object, ByVal e As MyEventArgs) Handles _
                rdbGudangLokal.Click, _
                rdbGudangCustomerKonsinyasi.Click, _
                rdbGudangCustomer.Click, _
                rdbGudangKonsinyasi.Click

msgbox (e.MyProperty)
emi_sastraAuthor Commented:
Great.

How could I set the MyProperty  at Form Load or in any procedure ?

Thank you.
CodeCruiserCommented:
When you want to call it directly

Dim mea As New MyEventArgs
mea.MyProperty = MyValue

rdbJenisGudang_Click(nothing, mea)
CodeCruiserCommented:
If you are thinking of setting my property so that its supplied when event is fired via the controls, I don't think that's possible without inheriting the controls as well.
emi_sastraAuthor Commented:
Show error after message box appeared.

Unable to cast object of type 'System.EventArgs' to type 'WindowsApplication1.MyEventArgs'.

Thank you.
emi_sastraAuthor Commented:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim mea As New MyEventArgs
        mea.MyProperty = "A"

        RadioButton1_Click(RadioButton1, mea)
    End Sub

    Private Sub RadioButton1_Click(ByVal sender As Object, ByVal e As MyEventArgs) Handles RadioButton1.Click
        MsgBox(e.MyProperty)
    End Sub
End Class


Public Class MyEventArgs
    Inherits System.EventArgs

    Public MyProperty As String
End Class

CodeCruiserCommented:
This approach works with custom events

http://www.java2s.com/Tutorial/VB/0220__Event/SubclassEventArgstocreateyourowneventarguments.htm

But I don't think it would work in your scenario.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emi_sastraAuthor Commented:
Ok. I will learn it later.

Thank you very much for your help.
CodeCruiserCommented:
Glad to help :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.