473,698 Members | 2,598 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 1720
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
6114
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. owen.southwood@mantix.com
0
1394
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
1278
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 under the same commitment control. After I start writing records, if anything fails anywhere in any...
3
7191
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
1495
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
2279
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
1385
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 not adding a User Control the samething? If not, what must I do in myUserControl.ascx file so I...
2
1397
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
2124
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 store_front.ascx) and placed two chunks of code in each. I then registered the controls at the top of...
0
8680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9169
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.