Solved

VS2010 WindowsForms MouseDown event not firing

Posted on 2013-10-27
7
389 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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