Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2007, 12:57 PM
Authorized User
 
Join Date: Dec 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Events - Adding Dynamically

Hi !!

Is it possible to create & add an event dynamically to an existing control ?

Suppose there is a textbox. I want to declare & raise an event "event1" when the entered value is outside a range. I dont want a custom control. all this needs to be done during the execution.

Is it possible ?

dev(1);
__________________
dev(1);
 
Old December 5th, 2007, 01:01 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You cannot add new event types to a control.

You would have to create a custom control that inherited from TextBox control and add the new event functionality to it.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 5th, 2007, 02:05 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to create a custom control only if you want this event to exist for any textbox instance you use throughout your application (i.e. create reusable control).

If you just want to do this test in your form, set up a handler to the textbox TextChanged event, and test the value. Then you can raise an event on your form or do whatever else you need.

Alternatively, if you want a reusable "range tester" (for lack of a better term) for this particular value condition, you could create a class based on the Observer pattern. The class constructor would take an instance of a textbox (to Observe) and a method delegate (to call when condition is met). To user this class on a form you'd have code that looked something like this:
Code:
MyRangeObserver observer1 = 
   new MyRangeObserver(myTextBox, new OutOfRangeEventDelegate(someMethod));

private void someMethod(...){
   //do something here when input is out of range.
}
With the right implementation, you could even make the observer configurable for different range values so it could be reused with different test values. Toss in some generics and anonymous delegates and you could do all sorts of things with a single class implementation (i.e. an anonymous delegate in the observer constructor could provide the test predicate to allow any kind of value test).

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically adding year in dropdown abhishekkashyap27 C# 2005 1 June 9th, 2008 07:44 AM
Adding Controls Dynamically andyj00 C# 0 March 19th, 2007 08:54 AM
dynamically adding objects shawnvb VB How-To 3 February 14th, 2007 03:12 PM
Events for multiple dynamically created controls Puck312 Pro VB 6 5 August 29th, 2005 03:05 PM
Dynamically adding page breaks Krimboss BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 June 6th, 2005 10:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.