Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 October 13th, 2006, 07:04 PM
Registered User
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Programatically generating a "TextChanged" handler

Hi there,
I'm creating a number of tabs in a form, each of which contains a text box. As I don't know at design time how many tabs a user may configure in the app I create these programatically at run time.

The code for this is as follows:

            for (int i = 0; i < TabsR; i++)
                TabPage myTabPage = new TabPage(TabTitles[i]);
                tabControl.SelectedTab = myTabPage;
                myTabPage.Name = TabTitles[i].ToString();
                RichTextBox MyRTF = new System.Windows.Forms.RichTextBox();
                MyRTF.Dock = DockStyle.Fill;
                MyRTF.Name = "RTF" + i.ToString();
                MyRTF.Font = new Font(EncDec.NoteFont, 10, FontStyle.Regular);
                myTabPage.Controls.AddRange(new Control[] { MyRTF });

My problem is that I have no way of tracking when the text in any of these text boxes changes; my question being how can I programatically create a "TextBox_TextChanged(object sender, EventArgs e)" event handler for when the change occurs?

Any help is greatly appreciated!

Kind regards,
Old October 13th, 2006, 07:19 PM
Wrox Author
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons

You are trying to create a Method on the fly or just wire up the textboxes?

To wire up the textboxes you would do:
txt.TextChanged += New System.EventHandler(this.method);

To write a custom method on the fly, I don't think that can be done. You would be better off just writing a method that would handle all of your textboxes and just wire them all up to that.


I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
Old October 14th, 2006, 05:36 AM
Registered User
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts

Yes! that worked a treat and does exactly what I need.
The code I've got working now based on your suggestion is as follows:

  MyRTF.TextChanged += new System.EventHandler(MyTextChanged);

And then in a seperate method:

  private void MyTextChanged(object sender, System.EventArgs e)
       RichTextBox MyRTF;
       MyRTF = (RichTextBox)sender;
       int a = Convert.ToInt16(MyRTF.Name.Substring(3,1));
       if (a == 0)
           UserTab1Dirty = True; // can use this flag elsewhere to prompt save b4 quit.

Many thanks - it all works perfectly!

Kind regards,

Similar Threads
Thread Thread Starter Forum Replies Last Post
textchanged event Komila ASP.NET 2.0 Basics 5 August 5th, 2007 01:09 PM
Accessing Programatically xlr8 BOOK: Beginning C# 2005 Databases 1 April 30th, 2007 08:56 AM
MaxDataAxisValue programatically ganans Crystal Reports 0 May 6th, 2006 06:30 PM
Using data programatically jasongorringe ASP.NET 2.0 Basics 1 February 10th, 2006 02:42 PM
problem with textchanged event alyeng2000 ASP.NET 1.0 and 1.1 Basics 0 February 15th, 2004 05:24 AM

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