?
Solved

VS2010 WindowsForms MouseDown event not firing

Posted on 2013-10-27
7
Medium Priority
?
398 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 64

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 64

Expert Comment

by:Fernando Soto
ID: 39603873
Where in frmMain do you define frmRocks?
0
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 

Author Comment

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

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 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