Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am new to vb.net.

I have three form.The first one is Main form (Mainform) which wil created multiple form of (Mpfform). Each Mpfform can create only one (Dmfform). Dmfform contain one "Start" button. The problem is when i click "Start" button in one of Dmfform, all the buttons event in other Dmfform will be trigger as well. What is the problem here? I only want the button clicked in that particular form to trigger.The code below is the button event in Dmfform .

VB
Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
        
        If Me.NewModbusSetting.DGV_AddVariable.RowCount <> Nothing Then
        
            Me.mytimer3.Interval = 1000
            Me.mytimer3.Start()
        Else
            MsgBox("Definition of the modbus properties is currently not available." & vbNewLine & "Please define the properties in the Measurement window : Communication >> Modbus Properties")
        End If
       
    End Sub


and the code below for how i created Dmfform window.

VB
Public Newdisplaymode As New Dmfform

    Private Sub DataDisplayModeMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataDisplayModeMenuItem.Click

        Newdisplaymode.Text = "Display Measurement: " & Me.Text
        Newdisplaymode.Show()
        
    End Sub


and the code below is when i created Mpfform

VB
Public mpf(30) As Mpfform()
Public num As Integer = 0

Private Sub NewMenuItem_File_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewMenuItem_File.Click
 
        num += 1
        mpf(num) = New Mpfform()

        mpf(num).Text = "ModbusPoll:" & CStr(num)
        mbv.Text = "Modbuspoll Values:" & CStr(num)
        mpf(num).Name = "ModbusPollFormFull" & num

             
        mpf(num).TopLevel = False
        mpf(num).BringToFront()
        mpf(num).Show()
        mpf(num).Focus()

End Sub


Thanks in advance
Posted
Updated 17-Jan-13 2:08am
v3
Comments
Sergey Alexandrovich Kryukov 11-Jan-13 13:24pm    
Not clear. What do you mean by "X" Forms and "X" Windows. (I believe it's not X Windows :-)
—SA
Sergey Alexandrovich Kryukov 11-Jan-13 13:27pm    
When you say "will be trigger as well", you only assume what you say, you don't observe it directly (I can explain it). You need to tell us what happens in the terms of something really observable. First of all, use the debugger — you will see everything.

—SA

1 solution

Please see my comments to the question.

Sorry for giving you somewhat incomplete answer, but I hope it' be enough for you to address and resolve your problem.

Your observation itself is not quite correct, and not because you could not see what's going on, but by a more basic reason: there is no such concept as "trigger a button". Here is what happens in fact: the button class has an event instance Click. What you do is: you add your handler to the event invocation list of the event instance of one and only one instance of your button. Unlike "regular" delegate instanced, the event instance is very strictly limited in use; and the only purpose of the separate concept of event is the set of limitation. One important limitation is this: nothing can invoke the event instance except the code of the declaring class, that is, the class where this event is declared. And again, nothing can invoke the event instance from the code of some other object. In the case of Click, only a physical click on the button can invoke your event instance, and nothing else, and of course, never a click on some other button or other control.

Now, you might disagree and ask, how come you still can have the mess you have? The answer is very simple: all of the above does not mean that nothing can call the particular event handle you used to add to some event instance invocation list. Are you getting the idea? This handler is just some method, and some other code could call it, especially if this is a named method (that is one of the reasons that I most usually advise in favor of using anonymous methods; in VB.NET, it can be used via AddHandle). Potentially, you could, accidentally or my conceptual confusion, add the same method to the invocation lists of different event instances. I just listed two possible reasons for your problem.

And here is the third possible reason: it could be just the side effect of the code of the event handler. For example, if you call MsgBox from different event handles (not really a good idea), it may look the way you don't really see where it was actually called from.

You can really easily find the ends if you simply execute it all under the debugger. When you execution comes into the body of some handler method, don't forget such a powerful tool as "Call Stack" — it will immediately show you where the call comes from.

I really hope you will do the rest by yourself.

Good luck,
—SA
 
Share this answer
 
v3
Comments
juita 16-Jan-13 5:12am    
thank you for your answer..ill try to solve it from here...
Sergey Alexandrovich Kryukov 16-Jan-13 13:31pm    
Great. And when and if you see it makes sense, please don't forget to accept it formally (green button).
If not, or if you still have issues or further questions, you are welcome to ask, of course.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900