473,378 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Extracting needed info from "sender as system.object"

I want to attach a context menu to multiple textboxes but I don't know
how the extract the name of the textbox that is the subject of the
event.

I can't seem to do anything with "sender as system.object" other than
get its type.

Could someone tell me how to extract this info? I don't like the untidy,
inefficient and anti-OO coding of using a seperate context menu for each
textbox.

--
Alan LeHun
Nov 21 '05 #1
3 1896
Alan LeHun wrote:
I want to attach a context menu to multiple textboxes but I don't know
how the extract the name of the textbox that is the subject of the
event.

I can't seem to do anything with "sender as system.object" other than
get its type.

Could someone tell me how to extract this info? I don't like the untidy,
inefficient and anti-OO coding of using a seperate context menu for each
textbox.


I think this is what you want to do:

Sub XYZ(sender as object, ....)

Dim txtBox as textbox = directcast(sender, textbox)
messagebox.show(txtBox.Name)

end sub
Nov 21 '05 #2
In article <ue**************@TK2MSFTNGP11.phx.gbl>, no@spam.com says...
Alan LeHun wrote:
I want to attach a context menu to multiple textboxes but I don't know
how the extract the name of the textbox that is the subject of the
event.
[]
I think this is what you want to do:

Sub XYZ(sender as object, ....)

Dim txtBox as textbox = directcast(sender, textbox)
messagebox.show(txtBox.Name)

end sub


Not quite chris, but thanks for the DirectCast heads-up.

sub XYZ(sender as object, ....)
Dim sendingTxtBox As String = DirectCast(sender,
MenuItem).Parent.GetContextMenu.SourceControl.Name
messagebox.show(sendingTxtBox)
end sub

or
sub XYZ(sender as object, ....)
Dim cMenuItem As MenuItem = DirectCast(sender, MenuItem)
Dim activeMenu As Menu = cMenuItem.Parent
Dim activeCMenu As ContextMenu = activeMenu.GetContextMenu
Dim sendingObject As Control = activeCMenu.SourceControl
Dim m As String = sendingObject.Name
messagebox.show(sendingTxtBox)
end sub

gives me what I want. I think I'm finally beginning to understand this
DirectCast thing. :)

--
Alan LeHun
Nov 21 '05 #3
Alan LeHun wrote:
In article <ue**************@TK2MSFTNGP11.phx.gbl>, no@spam.com says...
Alan LeHun wrote:
I want to attach a context menu to multiple textboxes but I don't know
how the extract the name of the textbox that is the subject of the
event.


[]

I think this is what you want to do:

Sub XYZ(sender as object, ....)

Dim txtBox as textbox = directcast(sender, textbox)
messagebox.show(txtBox.Name)

end sub

Not quite chris, but thanks for the DirectCast heads-up.

sub XYZ(sender as object, ....)
Dim sendingTxtBox As String = DirectCast(sender,
MenuItem).Parent.GetContextMenu.SourceControl.Name
messagebox.show(sendingTxtBox)
end sub

or
sub XYZ(sender as object, ....)
Dim cMenuItem As MenuItem = DirectCast(sender, MenuItem)
Dim activeMenu As Menu = cMenuItem.Parent
Dim activeCMenu As ContextMenu = activeMenu.GetContextMenu
Dim sendingObject As Control = activeCMenu.SourceControl
Dim m As String = sendingObject.Name
messagebox.show(sendingTxtBox)
end sub

gives me what I want. I think I'm finally beginning to understand this
DirectCast thing. :)


"I don't know how the extract the name of the textbox"
You asked how to get the name of the textbox ;)

There is also CType which does the same job as DirectCast, but direct
cast is more efficent in a case that you know the type.

Glad it helped...
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave C. | last post by:
Hi, I have a few things on my databases which seem to be neither true system objects or user objects - notably a table called 'dtproperties' (created by Enterprise manager as I understand,...
22
by: Dr Duck | last post by:
GDay all, Something seems odd to me.... I wrote a simple C# function public void bind(ref object a, ref object b, bool atob) { if(atob) b = a; else
0
by: Sin | last post by:
Hello, I'm currently toying around with using a VB.NET interface in a VC.NET program and I've come across somewhat of a problem. I found a workaround but I figure I must be a bit off cause it...
3
by: Maurice Walmsley | last post by:
I'll avoid tell you how lame I am and get straight to the question... I need a good expanation to the "ByVal sender As Object, ByVal e As System.EventArgs" part of; Public Sub...
5
by: Christian Hvid | last post by:
What is the easiest way to get the "row object" or "item object" when a datagrid is clicked? I have web form with a datagrid. And I have an array of something called BlogEntry that I bind to the...
4
by: lars.uffmann | last post by:
Hey everyone! I am (still) working on a project that I took over from former students, so don't blame me for the criminal approach on coding *g* The problem I have is fairly easy and while I...
4
by: Spectre1337 | last post by:
Hello, to my understanding the ASP.NET process works as follows: 1) Page_Init(): At this moment the user should allocate his controls, set their IDs and connect them to the page...
4
by: Rob | last post by:
I see that MsgBox(sender.ToString()) will return a string that includes the Text value of a button... Is there a way to return only the Text value (i.e., some additional property designate) ?
3
by: =?Utf-8?B?UGFvbG8=?= | last post by:
I'm trying to populate a rich text box with the results of a query, thus: private void button1_Click(object sender, EventArgs e) { var query from trans in dataSet.Transaction select new {...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.