Get name of control at a certain mousedown point

B

Brad

I have several labels and textboxes that are drawn to a tab page based on
certain criteria so these labels and textboxes can be drawn at different
points on the tab page. I set the label and textbox names in the drawing
procedure. Now what I need to do is to get the control's information on a
mouse down.

Here is what I have:

////
Private Sub tcGuard_MouseDown(ByVal sender as Object, ByVal e as
System.Windows.Forms.MouseEventArgs) Handles tcGuard.Mousedown 'tcGuard is
a tab control
Dim mouseLocale as new point(e.X, e.Y)
dim tName as String
dim cntrl as New Control
cntrl = tcGuard.GetChildAtPoint(mouseLocale)
tname = cntrl.Name
End Sub
\\\\

Here is the error message I get: Object reference not set to an instance of
an object.

All I need to do at this juncture is get the name of the label or textbox
that the mouse clicked on.

If I am doing this wrong, please let me know and thanks for any help you can
give.

Brad
 
B

Bob Powell [MVP]

The mouse event's will be handled by the individual controls so you'll need
to wire all the controls click events up to a handler in the tab page or tab
control.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
B

Brad

Bob,

Thanks for that information. Being fairly new to .Net, I do understand when
you talk about the handler event in the tab page or tab control. But if I
do not know what labels or textboxes will be drawn on the tabpage, how would
I add a handler? I guess I am not understanding that part and that is why I
am trying to get the control's name based on the point of the MouseDown
event.

Brad
 
C

Cor Ligthert

Brad,

In addition to Bob.

However when you want to know what type the object is and know it derives
from control than you can do something as

\\\
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If TypeOf sender Is Button Then
MessageBox.Show("I am a Button")
MessageBox.Show _
("My name is " & DirectCast(sender, Control).Name)
End If
End Sub
///
To show your 2 solutions in one sample.

I hope this helps,

Cor
 
C

Cor Ligthert

Brad,

I found it as well a little bit confusing what Bob wrote in that part,
however I think that he means to create one event sub and add to that using
the clasic VB method in
\\\\
Private Sub MyClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click, Label1.Click, etc
///
See the end of above
or
\\\
AddHandler Button1.Click, AddressOf MyClick
///
The last you can do as well in a loop for all the controls which should have
when you see the sample I sent as well in this thread.

You can think about
\\\
For each ctr as control in tabpage.controls
AddHandler ctr.Click, AddressOf MyClick
next
///

All controls have a click event and more in common
http://msdn.microsoft.com/library/d...rlrfsystemwindowsformscontrolmemberstopic.asp

I hope this helps?

Cor
 
A

AllcompPC

Cor,

I am still confused. I have a tab control with 8 pages that are set.
Within those 8 pages are 8 more tab controls that are drawn on. Within
those drawn tab pages is where the labels and textboxes are drawn. All of
this is driven based on criteria of data that is pulled from a SQL database.

I can easily reference the original 8 tap pages from the main tab control,
but if one wants to click on a sub page and then click on one of the labels
or text boxes in this page, this is where I need an event (a drag and drop)
to occur.

I certainly cannot build "Private Sub....Handles Label1.Click because Label1
may or may not be there.

My thinking was on the mouse down event of the tab page, get the XY
coordinates and then based on the tab page, get the control at that
location.

Complicated, I know. But learning the language myself, I have had to tackle
these issues and once I do, I remember from there on.

Brad
 
B

Bob Powell [MVP]

It looks as if you should programmatically step through the Control lists of
the nested items and use AddHandler to grab the mousemove or click events of
the various nested components.

The trouble is that if the mouse is inside say a textbox control the tabpage
will never see the mousedown because it will be handled by the textbox. This
is why you should ensure that a particular handler converts the current
mouse position to fit the context of whichever control you treat as the
parent.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
A

AllcompPC

Cor and Bob,

Thank you for your help. It is now working to what I want it to do. I used
the AddHandler statements - one for DragEnter and one for DragDrop in the
same routine that draws the label to the tab page.

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top