473,322 Members | 1,540 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,322 software developers and data experts.

get control of senders functions

Hi there, in my calculator app the number buttons have (of course) almost
the same functionality. Therefore, I would like to store their value in the
tag number and read that tag number when any of those buttons is pressed.
But how does this work??

private void button3_Click(object sender, EventArgs e)

{

if sender.GetType() is button

{

read out its tag value and add it to calcWindow;

}

}

Thanks,

Pascal
Dec 7 '05 #1
4 1705
Pascal Schmidt-Volkmar wrote:
Hi there, in my calculator app the number buttons have (of course) almost
the same functionality. Therefore, I would like to store their value in the
tag number and read that tag number when any of those buttons is pressed.
But how does this work??

private void button3_Click(object sender, EventArgs e)

{

if sender.GetType() is button

{

read out its tag value and add it to calcWindow;

}

}


Well, I would make sure that you only subscribe to the event with that
delegate when the sender *will* be a button - that way you can just
cast straight to button. After that, just use any of the button's
properties as normal (eg Text, Tag etc).

Jon

Dec 7 '05 #2
Hi Jon,

but how does this check and the cast work?

private void button1_Click(object sender, EventArgs e)

{

if ( sender.GetType() = System.Windows.Forms.Button )

{

System.Windows.Forms.Button(sender).text = edErgebnis.Text + "1";

}

}

Pascal
Dec 7 '05 #3
Well I'm pretty sure that it doesn't! You have an assignment operator in a
test, and are comparing directly to a class definition... and use of a
non-existant property...

I think this should be:

private void SharedClick(object sender, EventArgs e) {
Button btn = sender as Button;
if (btn != null && btn.Tag != null) {
// do something with btn.Tag, which will be cast as an object,
so we may need to recast it or call .ToString(), e.g.
myTextBox.Text += btn.Tag.ToString();
}
}

When any button is clicked, it fires the event sending itself (this) as the
sender; when we catch the event, we test it so see if it is a Button using
the "as" keyword (which returns null if you have incorrectly hooked the
event into another type of control). After this, btn is an instance of a
Button, so we can access btn.Tag (incidentally, you could actually replace
Button with Control in the above and it would work just fine, since Tag is
defined against a Control, not the actual Button class).

You'd also need to hook all of the Click events to use this *same* event
handler, which can be done either in the IDE (by selecting them in the
drop-down), or manually using something like (in .Net 2):

myButton1.Click += SharedClick;
myButton2.Click += SharedClick;
myButton3.Click += SharedClick;
....

In .Net 1 you would have to use the longer syntax, so I would probably go
for:

EventHandler sharedEvent = new EventHandler(SharedClick);
myButton1.Click += sharedEvent;
myButton2.Click += sharedEvent;
myButton3.Click += sharedEvent;
....

Does that help any?

Marc

"Pascal Schmidt-Volkmar" <pa***********@tauth.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Hi Jon,

but how does this check and the cast work?

private void button1_Click(object sender, EventArgs e)

{

if ( sender.GetType() = System.Windows.Forms.Button )

{

System.Windows.Forms.Button(sender).text = edErgebnis.Text + "1";

}

}

Pascal

Dec 7 '05 #4
Marc,

that was exactly what I was looking for! Thanks a lot!

Pascal
Dec 7 '05 #5

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

Similar topics

6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
0
by: Simon | last post by:
I have user control Functions with this function: Function createUrl(ByVal link As String, ByVal name As String, Optional ByVal stev As Int16 = 0, Optional ByVal cl As Int16 = 0) As String Dim...
0
by: Matt Clepper | last post by:
Ok - debated on posting this on SQL group but here goes... I have a piece of ASP code (function #1) that creates a new SQLTransaction (BeginTransaction) to and begins to write records to the...
3
by: Filips Benoit | last post by:
Dear All, How can i email from access and hide or change the senders email-address? Why: Quotations for a house are made by several salers with an Access-interface on the firms server (...
8
by: Prince Mathew | last post by:
Hi All, I have a requirement. I am throwing an exception from my user control I want to catch this in my container page. Is this possible? I don't want any event to be raised. PLEASE...
5
by: Galen Somerville | last post by:
Just looking for thoughts on converting VB6 Control arrays. Here's the way I did it. The control happens to be a Usercontrol, CircVol, but could be any control. I would appreciate any...
4
by: SAL | last post by:
Hello, I created a WebPage.aspx that has some functions in it, and I created a User Control that has a File Field Control and a Botton control on the .ascx. I added myUserControl.ascx to the...
2
by: ashutosh11 | last post by:
Hi all, can anyone please help I am a new to VBA and need help here. I am trying to store EMAILs depending on the senders Email Domain. I should write a VBA script that will run everytime a new...
11
by: Brad Baker | last post by:
I'm building a small web application - I started out placing all my code in one file (config.aspx). As I continue to add code though it was becoming very unwieldy. After doing some searching...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.