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

Programmatically disabling Windows Control Events

Hi

I wanted to programmatically disable events like SelectedIndexChanged without
using a boolean flag, which indeed I find a quick and dirty solution. So, I
found a
piece of code that do this more elegant:

* How to disable event in C#?
Link: http://tinyurl.com/alsge
Group: microsoft.public.dotnet.framework.compactframework

But I saw it is limited to ComboBoxes and to the SelectedIndexChanged Event
and
I want to generalize it to an object of type control and to every possible
event. To
give you an idea, I will post some of my ugly code:

public static EventHandler DisableSelectedIndexChanged(Control control)
{
FieldInfo fieldInfo;
object fieldObject;
EventHandler handler = null;

fieldInfo = control.GetType().GetField("SelectedIndexChanged",
BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo != null)
{
fieldObject = fieldInfo.GetValue(control);
if (fieldObject is EventHandler)
{
handler = (EventHandler)fieldObject;

//This is the part I want to avoid
if (control is ComboBox)
((ComboBox)control).SelectedIndexChanged -= handler;
else if (control is ListBox)
((ListBox)control).SelectedIndexChanged -= handler;
else if (control is ListView)
((ListView)control).SelectedIndexChanged -= handler;
else if (control is TabControl)
((TabControl)control).SelectedIndexChanged -= handler;
}
}
return handler;
}

Is it possible to avoid the casting and get a pointer to the
SelectedIndexChanged
property, then apply the -= operation over it? How? As I could understand,
FieldInfo.GetValue(control) only returns the handler, but not the
SelectedIndexChanged property.

Thanks in advanced,
Josef
Nov 17 '05 #1
0 1480

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

Similar topics

9
by: Marek Mand | last post by:
How to use generated keyboard events? What I am trying here to do is in onkeyup event handler http://www.hot.ee/idaliiga/braggart/createEventTest.htm generate a (shift)TAB keydown so the...
13
by: aundro | last post by:
Hello, I've been looking on the web for a solution to this problem: I create a set of checkboxes, and 2 buttons: - one is labeled "All" - the other is labeled "None" Clicking "All" is...
5
by: Lyn | last post by:
Hi, I hope someone can help. I have a main form which mostly fills the Access window. In the bottom half of this form I have a tab control to display various types of data related to the main...
6
by: Roger Uribe | last post by:
There are references to using the wndproc form sub to trap and swallow the mousewheeel events - but they don't get there when a combobox (or many other controls) has the focus.. Yet the MSDN does...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
4
by: Bob | last post by:
Hi, I'm working with VWD and i defined programmatically a button (in code-behind) with an ID. So I expect to see beside "Page Events" and "Form1" my button "b1" in order to use the Click event....
4
by: Tomasz | last post by:
Hello Developers, Here is interesting problem I just came across: how do I wire a GridView control programmatically? Here is my sample code using Object Data Source: protected void...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
5
by: jehugaleahsa | last post by:
Hello: I am sure this question comes up a lot. I need to disable the controls on my Windows forms so that when the BindingSource is empty some the controls bound to it will be disabled. This...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
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
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,...
0
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...

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.