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

How to select an arbitrary area on a form using sender / what EventHandler?

I have a form, Form6, that has a bunch of buttons overlaid on it. I
want to be able to click on any arbitrary area of the form, and if
that area of the form is overlaid by a button, I want to change the
color of the button to Coral, and show a MessageBox with the button
name.

So far, I've tried the below event handlers, and the only one that has
worked is of course the named button event click, for a particular
named button (button3 below), but, I don't want to set up such click
events for each button, since the form Form6 has an arbitrary number
of buttons and/or a large number of buttons on it.

Any way to do this in C#?

Thanks

RL

//////////

private void Form6_DoubleClick(object sender, EventArgs e)
{
//double clicking on form will cause this event to
trigger, which checks to see if a particular button clicked
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button doubleclicked is: " +
currentButton.Text);
}
}

private void Form6_MouseDoubleClick(object sender,
MouseEventArgs e)
{
//double clicking on form will cause this event to
trigger, which checks to see if a particular button clicked
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button doubleclicked is: " +
currentButton.Text);
}
}

private void Form6_MouseMove(object sender, MouseEventArgs e)
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button MouseMove: " +
currentButton.Text);
}

}

private void button3_Click(object sender, EventArgs e) //only
this event handler works
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button #3!: " + currentButton.Text);
}

}

private void Form6_MouseDown(object sender, MouseEventArgs e)
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button MouseDown: " +
currentButton.Text);
}
}

Jul 25 '08 #1
5 1667
This code in your form should do what you want, if I understand correctly.

public Form1()
{
InitializeComponent();

foreach (Control c in Controls)
{
if (c is Button)
{
Button b = (Button)c;
b.Click += new EventHandler(b_Click);
}
}
}

void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Color old = b.ForeColor;
b.ForeColor = Color.Coral;
MessageBox.Show(b.Name);
b.ForeColor = old;
}

"raylopez99" <ra********@yahoo.comwrote in message
news:af**********************************@a1g2000h sb.googlegroups.com...
>I have a form, Form6, that has a bunch of buttons overlaid on it. I
want to be able to click on any arbitrary area of the form, and if
that area of the form is overlaid by a button, I want to change the
color of the button to Coral, and show a MessageBox with the button
name.

So far, I've tried the below event handlers, and the only one that has
worked is of course the named button event click, for a particular
named button (button3 below), but, I don't want to set up such click
events for each button, since the form Form6 has an arbitrary number
of buttons and/or a large number of buttons on it.

Any way to do this in C#?

Thanks

RL

//////////

private void Form6_DoubleClick(object sender, EventArgs e)
{
//double clicking on form will cause this event to
trigger, which checks to see if a particular button clicked
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button doubleclicked is: " +
currentButton.Text);
}
}

private void Form6_MouseDoubleClick(object sender,
MouseEventArgs e)
{
//double clicking on form will cause this event to
trigger, which checks to see if a particular button clicked
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button doubleclicked is: " +
currentButton.Text);
}
}

private void Form6_MouseMove(object sender, MouseEventArgs e)
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button MouseMove: " +
currentButton.Text);
}

}

private void button3_Click(object sender, EventArgs e) //only
this event handler works
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button #3!: " + currentButton.Text);
}

}

private void Form6_MouseDown(object sender, MouseEventArgs e)
{
if (sender.GetType() ==
typeof(System.Windows.Forms.Button))
{
Button currentButton = (Button)sender;
currentButton.BackColor = Color.Coral;
MessageBox.Show("Button MouseDown: " +
currentButton.Text);
}
}
Jul 26 '08 #2
On Jul 25, 8:01*pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
This code in your form should do what you want, if I understand correctly..
Yes, thanks FTM! It worked fine. Just to complicate things and as a
teaching exercise to show for me how delegates work, I added a helper
function that is based on a user defined delegate. The helper
function shows a MessageBox with the button's name, when the button is
clicked. I used two different delegate/event based variables, one
being static, the other being non-static, to fire this helper
function.

It's interesting that you cannot define easily name a delegate Click
since there is a Click delegate (namely, the standard button click
event) already named in .NET, so it's forbidden to do so (compiler
error) unless you want to overload Click, which I don't want to touch,
so I used another delegate name. The location of the 'firing' of the
non-static and static-versions of the event is the same, but the
'registration' of the event is different for non-static (must be
declared once) versus static (must be declared inside of the _Click
function).

Below is the code.

RL

// 7/26/2008
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MDIForms
{

public partial class Form6 : Form
{
public delegate void ShowNameButEventHandler(object source,
EventArgs e);

//instance class version
public class ButtonHelper
{
public event ShowNameButEventHandler ShowNameBut;
public void OnShowNameBut(Button be, EventArgs e)
{
if (ShowNameBut != null) ShowNameBut(be,
EventArgs.Empty);
}

}

//static class version
public static class staticButtonHelper
{
public static event ShowNameButEventHandler
staticShowNameBut;
public static void staticOnShowNameBut(Button be,
EventArgs e)
{
if (staticShowNameBut != null) staticShowNameBut(be,
EventArgs.Empty);
}
}

Button[] buttons; //Button must be declared here to have
proper scope

/*
// *&* public event EventHandler Click; //not needed since
system has one predefined nongeneric EventHandler delegate called
“Click” already (button click), hence get compiler warning:
'MDIForms.Form6' already contains a definition for 'b_Click'
*/

public Form6()
{
InitializeComponent();
buttons = new Button[] { this.button1,
this.button2,this.button3,this.button4,this.button 5,this.button6,this.button7,this.button8,this.butt on9 };
//buttons 1-9 created using designer wizard prior to runtime

ButtonHelper buttonHelper = new ButtonHelper();

staticButtonHelper.staticShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut);

/*
//registration (old-style format); placing registration here is
mandiatory for static version // to prevent more than once
instantiation (compare with non-static version)
*/

foreach (Control c in Controls) //note convention
{
if (c is Button)
{
Button b = (Button)c;
//note convention "Click" is Delegate for signature function "_Click"

b.Click += b_Click; //new style registration;
// b.Click += new EventHandler(b_Click); //oldstyle format also
works

/*
// staticButtonHelper.staticShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //register,(modern
style) but don't register here--get nine instances!
// buttonHelper.ShowNameBut +=new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //works but should
not be registered here...you get nine popup Message boxes when form
constructed, not what you want
*/

// buttonHelper.OnShowNameBut(b,EventArgs.Empty);
//works but should not be fired here…get up to nine instances of
MessageBox

}
}

}
void b_Click(object sender, EventArgs e) {
Button b = (Button)sender;
Color old = b.BackColor;
b.BackColor = Color.Coral;

// NON-STATIC VERSION OF EVENT (works fine)
// ButtonHelper buttonHelper1 = new ButtonHelper(); //
create instances of ButtonHelper for non-static version of event
delegate
// buttonHelper1.ShowNameBut += new
ShowNameButEventHandler(buttonHelper_ShowNameBut); //register the non-
static public event
// buttonHelper1.OnShowNameBut(b, EventArgs.Empty); //fire
here, non-static instance version (works)

//STATIC VERSION OF EVENT (works fine)
// staticButtonHelper.staticShowNameBut += buttonHelper_ShowNameBut;
//WRONG PLACE TO register static version (gives up to nine
instantiations, everytime the loop is traversed another added)
staticButtonHelper.staticOnShowNameBut(b, EventArgs.Empty);
//fire here, static version;

/*
// both static and non-satic versions of ButtonHelper fired from
inside b_Click, which is automatically fired when buttons are clicked
(depends on button click, see *&* above)
*/

}

//static member function used by delegate/events (same for both static
and non-static delegate/events
static void buttonHelper_ShowNameBut(object sender, EventArgs
e)
{
Button b = (Button)sender;
MessageBox.Show(b.Name + "!");
}

} //end bracket, form 6
} //end bracket, namespace
Jul 26 '08 #3
On Jul 26, 3:37*am, raylopez99 <raylope...@yahoo.comwrote:
On Jul 25, 8:01*pm, "Family Tree Mike"

<FamilyTreeM...@ThisOldHouse.comwrote:
This code in your form should do what you want, if I understand correctly.

Yes, thanks FTM! *It worked fine. *Just to complicate things and as a
teaching exercise to show for me how delegates work, I added a helper
function that is based on a user defined delegate. *The helper
function shows a MessageBox with the button's name, when the button is
clicked. *I used two different delegate/event based variables, one
being static, the other being non-static, to fire this helper
function.

UPDATE: the non-static version works much better. The static version
has a 'bug' that results from the fact the 'static' class is not
garbage collected when the window closes (this happens to be a sub-
form or child window). Then, when you reopen the child window again,
the non-static class has delegate instances that necessitate in
multiple firings. Everytime you close and reopen the window,more of
these delegates accumulate, to the point where if you open and close
the window 10 times, you'll have to click through 10 MessageBoxes for
each time you click on a button.

But the non-static version doesn't have this problem, since the
instance class is garbage collected when the window is closed.

RL

Jul 26 '08 #4
On Jul 26, 4:03*am, raylopez99 <raylope...@yahoo.comwrote:
But the non-static version doesn't have this problem, since the
instance class is garbage collected when the window is closed.

RL
The static version apparently also has to be garbage collected, and is
not being done so properly, refer to some other threads here,
".The object isn't static, it's only the reference to the object that
is static. The object is allocated on the heap just like any other
object, and if you remove the reference from the static variable (e.g.
setting it to null), the object can be garbage collected."

RL
Jul 27 '08 #5
On Jul 27, 5:03*am, raylopez99 <raylope...@yahoo.comwrote:
On Jul 26, 4:03*am, raylopez99 <raylope...@yahoo.comwrote:
But the non-static version doesn't have this problem, since the
instance class is garbage collected when the window is closed.
RL

The static version apparently also has to be garbage collected, and is
not being done so properly, refer to some other threads here,
".The object isn't static, it's only the reference to the object that
is static. The object is allocated on the heap just like any other
object, and if you remove the reference from the static variable (e.g.
setting it to null), the object can be garbage collected."

RL
Here is how I fixed the static version bug, without having to resort
to Dispose, which I hate to fool with.

Simply 'unsubscribe' the event when the subform / child form, Form 6,
closes, like so:

private void Form6_FormClosing(object sender,
FormClosingEventArgs e)
{
staticButtonHelper.staticShowNameBut -=
buttonHelper_ShowNameBut; //dispose of this (to prevent multiple
instances for static version) //works!

}

Now, when you reopen the form, you will only have one static Event/
Event hander/ delegate, and the program will work properly.

This is done automatically in the non-static version, when the
instantiated object, variable buttonHelper1, goes out of scope, but
for the static version you have to manually get rid of the variable as
per the above.

RL
Jul 27 '08 #6

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

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
3
by: Chris | last post by:
Hi, I'm trying to append text from another class to a generic richTextBox that I've added to a Windows form. I can't seem to figure out how to expose the richTextBox to append text to it. ...
6
by: Claus Holm | last post by:
I'm trying to enable a menuitem in the parent form from a mdichild. Rather than making the menuitems public, I'd go for a public method in the parent form to do the change, but when I call the...
8
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.