Solved

VS2010 WindowsForms MouseDown event not firing

Posted on 2013-10-27
7
375 Views
Last Modified: 2013-10-27
I got some help yesterday with a problem I was having getting the name of multiple buttons. This code works fine if I create a new project and 2 new forms but when I place in my project, it doesn't fire at all. I have tried a try/catch statement, but no errors are being reported. How do I go about troubleshooting this. I have tried to rem out code and run after each comment but still the same. Any help would be greatly appreciated.

frmMain

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        For Each ctrl In Me.Controls
            If TypeOf ctrl Is Button Then
                AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
            End If
        Next

        'TODO: This line of code loads data into the 'StorageDataSet1.Customers' table. You can move, or remove it, as needed.
        Me.CustomersTableAdapter.Fill(Me.StorageDataSet1.Customers)
        'TODO: This line of code loads data into the 'StorageDataSet.User' table. You can move, or remove it, as needed.
        Me.UserTableAdapter.Fill(Me.StorageDataSet.User)

        'Dim frmDepartmentsLive As New frmDepartment
        'frmDepartmentsLive.Owner = Me
        'frmDepartmentsLive.ShowDialog()

        lblDate.Text = Now

        Timer1.Start()

        rdoCustomer.Enabled = False
        rdoCustomer.Checked = True
        rdoDepartment.Enabled = False
        rdoDepartment.Checked = False
    End Sub

Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Try
            If (e.Button = MouseButtons.Right) Then
                Dim btn = CType(sender, Button)
                frmRacks.buttonName = btn.Name.Replace("btn", "")
                frmRacks.Show()
            ElseIf (e.Button = MouseButtons.Left) Then
                MessageBox.Show("To be coded")
            End If
                Catch ex1 As exception
            MsgBox("There's an error: " & vbCrLf & ex1.Message)
        End Try

    End Sub

Open in new window


frmRacks

Imports System.Data.OleDb
Public Class frmRacks
    Public buttonName As String
    Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblRacks.Text = buttonName

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
    
End Class

Open in new window

0
Comment
Question by:peter-cooper
  • 4
  • 3
7 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39603798
Hi peter-cooper;

In frmMain I can see that you are using a variable called frmRacks.buttonName = btn.Name.Replace("btn", "") and frmRacks.Show() but I do not see where you have declared frmRocks?
0
 

Author Comment

by:peter-cooper
ID: 39603865
Hi FernandoSoto

Sory, but I do not quite understand your answer. Could you please explain further. Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39603873
Where in frmMain do you define frmRocks?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:peter-cooper
ID: 39603886
There is no frmRocks. It is frmRacks.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39603961
Hi peter-cooper;

I have modified your two forms to work as I think you want them too. I have made comments in the code, please read.

Public Class frmMain

    ' Create a field that holds a reference to frmRacks to be accessed from anywhere in this form
    Private frmRacks As frmRacks = Nothing

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' On form load initalize a frmRack but do not show it yet.
        frmRacks = New frmRacks

        For Each ctrl In Me.Controls
            If TypeOf ctrl Is Button Then
                AddHandler CType(ctrl, Button).MouseDown, AddressOf btn_MouseDown
            End If
        Next


        lblDate.Text = Now

        Timer1.Start()

    End Sub

    Private Sub btn_MouseDown(sender As Object, e As MouseEventArgs)

        Try
            If (e.Button = MouseButtons.Right) Then
                Dim btn = CType(sender, Button)
                ' Now use the reference you created above to modify the frmRacks and show it
                frmRacks.ButtonName = btn.Name.Replace("btn", "")
                frmRacks.Show()
            ElseIf (e.Button = MouseButtons.Left) Then
                MessageBox.Show("To be coded")
            End If
        Catch ex1 As Exception
            MsgBox("There's an error: " & vbCrLf & ex1.Message)
        End Try
    End Sub

End Class


Public Class frmRacks

    ' Create a private field to hold the button name from the main form 
    Private _buttonName As String
    ' Create a public property to access the field _buttonName and set the label
    Public Property ButtonName As String
        Get
            Return _buttonName
        End Get
        Set(ByVal value As String)
            _buttonName = value
            _lblRacks.Text = _buttonName
        End Set
    End Property

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        ' In a form if you Me.Close() it will Dispose of the form. Seeming you may be repeatedly calling this form
        ' you want to hide Or make it invisible so that on the next call from frmMain it will not crash
        Me.Hide()
        'Me.Close()

    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:peter-cooper
ID: 39603982
Thank you very much
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39604017
Not a problem, glad to help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now