Solved

VS2010 WindowsForms MouseDown event not firing

Posted on 2013-10-27
7
377 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 53
Runtime Error 2 29
fso.FolderExists("\\server\HiddenFolder$") 4 52
How can my static class become undefined?? 8 21
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

27 Experts available now in Live!

Get 1:1 Help Now