473,569 Members | 2,590 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(o bject 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 1716
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(o bject 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(o bject sender, EventArgs e)

{

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

{

System.Windows. Forms.Button(se nder).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(obj ect 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.ToStrin g();
}
}

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(Sh aredClick);
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******** **********@TK2M SFTNGP12.phx.gb l...
Hi Jon,

but how does this check and the cast work?

private void button1_Click(o bject sender, EventArgs e)

{

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

{

System.Windows. Forms.Button(se nder).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
6105
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 runtime). Does it indicate a problem with References for example? Thanks Owen PS. replies cc'd by email appreciated....
0
1390
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 clas As String If stev = 0 Then clas = "" End If If cl = 0 Then createUrl = "<table border=1><tr><td><a href='" & link & "'>" &
0
1272
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 database under commitment control. Function #1 then calls a series of other functions (sub-functions) where I would also like to write transactions...
3
7180
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 ( Remote Desktop) . When sending the report by email to the client the firm wants the salers email in the from-property and not the servers email.
8
1490
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 HELP ME.
5
2272
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 suggestions on a better way to do it. GalenS
4
1380
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 WebPage.aspx, but it can not access the WebPage.aspx functions. If I add a button to the WebPage.aspx, I can access those functions no problem. Is...
2
1382
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 Email has arrived and if matches with the Email Domain, outlook should store it in a local folder. Thanks and Regards Ash
11
2118
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 online I found an article which suggested I might place some of the code into web user controls. I created two new files (general_info.ascx and...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.