Solved

VS2010 WindowsForms MouseDown event not firing

Posted on 2013-10-27
7
384 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 63

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 63

Expert Comment

by:Fernando Soto
ID: 39603873
Where in frmMain do you define frmRocks?
0
How our DevOps Teams Maximize Uptime

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

 

Author Comment

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

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 63

Expert Comment

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

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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