Solved

Right Clicking on a button from a Form problem

Posted on 2003-10-25
2
243 Views
Last Modified: 2010-05-01
VB.NET 2003

So why in the world you cant start with the regular:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Code here

End Sub

and replace 'Code here with something to detect if it was a right click?  Further why can you do something exactly like above but replace "Handels Button1.Click" with something like "Handels Button1.RightClick"?

I did figure out that I needed to add my own event handler:

AddHandler Button1.MouseDown, AddressOf myEvents.myMouseDown1

Then added a Class:

Public Class myMouseEventClass
     Public Sub myMouseDown1(ByVal sender As Object, ByVal ex As MouseEventArgs)
        If ex.Button = MouseButtons.Right Then
          'Code here
        End If
    End Sub
End Class

Works fine but here is my problem, from within this class I cant touch any variables unless they are globally declared as Shared. Further I can call any other subroutines unless they are shared as well.

My main problem is that I would like to update part of the main form of my program, namely a Label1.Text with something from within the 'Code here section in the event handler.  I also need to peform another button click from within the clase as well, namely Button2.PeformClick() but the same problem.

Trying to do either gives me:

Reference to a non-shared member requires an object reference

from the IDE and a subsequent:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

From the Build Errors.

How can I get aroud this and do one simple thing, update a text box when I right click on a button.

Thanks for any insight anyone has.

Craig

0
Comment
Question by:Craig_Sparks
2 Comments
 
LVL 4

Accepted Solution

by:
brother7 earned 250 total points
ID: 9618987
I got the answer... create an event handler for MouseDown, not Click.

--- Code Start ---

   Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
      If e.Button = MouseButtons.Right Then
         lblOutput.Text = "Right button was clicked."
      End If

      If e.Button = MouseButtons.Left Then
         lblOutput.Text = "Left button was clicked."
      End If
   End Sub

--- Code End ---

Tested and it works.
0
 

Author Comment

by:Craig_Sparks
ID: 9619808
Thanks!

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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