Using Controls

I l @ ve RuBoard

So far, we have seen the Button controls and the NumericUpDown in action. Let's take a closer look at some of the more commonly used form controls.

Check Boxes and Radio Buttons

Check box and radio button behavior is essentially the same as that of their MFC counterparts. However, the Windows Forms versions do not have any of the nice DDX traits we are all so used to, so reading them and reacting to them is a little more time consuming.

Check boxes can be placed on the form or within other panels or group boxes but, because they don't generally rely on the state of other check boxes around them, their setup is relatively simple.

Radio buttons that are associated with one another should be children of another object, such as a panel or group box, for their automatic radio-button selection to work.

Retrieving the state of these controls is simply a matter of interrogating them directly or using their click methods to keep a record of the user 's choices.

Listing 3.2.9 shows a dialog with both check boxes and radio buttons. The code shows several methods for reading the button states or trapping events.

Listing 3.2.9 dialogtest.cs : Using Check Boxes and Radio Buttons
 1: using System;   2: using System.ComponentModel;   3: using System.Drawing;   4: using System.Windows.Forms;   5: using System.IO;   6: using System.Text;   7:   8: namespace Sams {   9:  10: class DialogTest : System.Windows.Forms.Form  11: {  12:  13:     private Button okButton;  14:     private Button cancelButton;  15:     private CheckBox checkbox;  16:     private GroupBox radiogroup;  17:     private RadioButton radio1,radio2,radio3;  18:  19:     public int Radio;  20:  21:     public bool Check {  22:         get {  return checkbox.Checked; }  23:         set {  checkbox.Checked = value;}  24:         }  25:  26:     void OnRadio(Object sender,EventArgs e)  27:     {  28:         int n=0;  29:         foreach(Object o in radiogroup.Controls)  30:         {  31:             if(o is RadioButton)  32:             {  33:                 RadioButton r=(RadioButton)o;  34:                 if(r.Checked)  35:                     Radio=n;  36:                 n++;  37:             }  38:         }  39:     }  40:  41:     public DialogTest()  42:     {  43:  44:         Size = new Size(400,300);  45:         BorderStyle = FormBorderStyle.FixedDialog;  46:         Text = "Dialog test";  47:  48:         //place the buttons on the form  49:         okButton = new Button();  50:         okButton.DialogResult = DialogResult.OK;  51:         okButton.Location = new Point(20,230);  52:         okButton.Size = new Size(80,25);  53:         okButton.Text = "OK";  54:         Controls.Add(okButton);  55:  56:         cancelButton = new Button();  57:         cancelButton.Location = new Point(300,230);  58:         cancelButton.Size = new Size(80,25);  59:         cancelButton.Text = "Cancel";  60:         cancelButton.DialogResult = DialogResult.Cancel;  61:         Controls.Add(cancelButton);  62:  63:         //place the check box  64:         checkbox = new CheckBox();  65:         checkbox.Location = new Point(20,30);  66:         checkbox.Size = new Size(300,25);  67:         checkbox.Text = "CheckBox";  68:         Controls.Add(checkbox);  69:  70:         //place the radiobuttons  71:         //they need to go into a a group box or a panel...  72:         radiogroup = new GroupBox();  73:         radiogroup.Text = "Radio Buttons";  74:         radiogroup.Location = new Point(10,60);  75:         radiogroup.Size = new Size(380,110);  76:         Controls.Add(radiogroup);  77:  78:         radio1 = new RadioButton();  79:         radio1.Location = new Point(10,15);  80:         // remember this is relative to the group box...  81:         radio1.Size = new Size(360,25);  82:         radio1.Click += new EventHandler(OnRadio);  83:         radio1.Text = "Radio Button #1";  84:         radiogroup.Controls.Add(radio1);  85:  86:  87:         radio2 = new RadioButton();  88:         radio2.Location = new Point(10,40);  89:         // remember this is relative to the group box...  90:         radio2.Size = new Size(360,25);  91:         radio2.Click += new EventHandler(OnRadio);  92:         radio2.Text = "Radio Button #2";  93:         radiogroup.Controls.Add(radio2);  94:  95:  96:         radio3 = new RadioButton();  97:         radio3.Location = new Point(10,70);  98:         // remember this is relative to the group box...  99:         radio3.Size = new Size(360,25); 100:         radio3.Click += new EventHandler(OnRadio); 101:         radio3.Text = "Radio Button #3"; 102:         radiogroup.Controls.Add(radio3); 103: 104:     } 105: 106: } 107: 108: class dtApp : System.Windows.Forms.Form 109: { 110: 111:     void OnExit(Object sender, EventArgs e) 112:     { 113:         Application.Exit(); 114:     } 115: 116:     void OnDialogTest(Object sender, EventArgs e) 117:     { 118:         DialogTest dlg = new DialogTest(); 119: 120:         DialogResult r=dlg.ShowDialog(); 121: 122: 123:         StringWriter sw=new StringWriter(new StringBuilder()); 124: 125:         sw.WriteLine("Dialog return value = { 0} "+ 126:                      "\ nRadio Buttons = { 1} \ nCheck box = { 2} ", 127:                      r,dlg.Radio,dlg.Check); 128: 129:         MessageBox.Show(sw.ToString()); 130: 131:     } 132: 133:     public dtApp() 134:     { 135:         MainMenu mm=new MainMenu(); 136:         mm.MenuItems.Add(new MenuItem("&Dialog")); 137:         mm.MenuItems[0].MenuItems.Add(new MenuItem("&Test", 138:             new EventHandler(OnDialogTest))); 139:         mm.MenuItems[0].MenuItems.Add(new MenuItem("-")); 140:         mm.MenuItems[0].MenuItems.Add(new MenuItem("E&xit", 141:             new EventHandler(OnExit))); 142:         Menu = mm; 143:     } 144: 145:     static void Main() 146:     { 147:         Application.Run(new dtApp()); 148:     } 149: } 150: 151: } 

Looking more closely at the code in Listing 3.2.9, you can see that the radio buttons are, in fact, children of the group box control (lines 70 “103), and that they all use the same click handler (lines 26 “39). This radiobutton handler reads through the child controls of the group box one at a time. When it finds a radio button, it looks to see if it's checked. If it is, the corresponding value is set in the forms member data. This is one of many techniques you can use to read radio buttons. You could just as easily define one handler per button and set a specific value for each of them.

In the case of radio buttons, the dialog data is stored in a public data member. This can be read directly by external objects. The check box data is read by a public property that accesses the actual check box control.

Simple Edit Controls

There is a simple text box that handles multiline editing and password display for entering text into your forms. The TextBox class can also allow the user to use the Tab key and press Enter so multiline editing is more flexible.

Text edit boxes are illustrated later in the chapter in Listing 3.2.10.

List Boxes

Windows Forms list boxes can be used to display information in vertical or horizontal lists.

List box usage is illustrated in Listing 3.2.10, shown later in this chapter.

Tree Views

The tree view has become a standard for displaying the hierarchical structures of disk directories and other information, such as XML data. Naturally, Windows Forms has a very good tree view class that has great capabilities.

Tree views hold tree nodes. Each node can have a text label associated with it and have other properties such as images that denote the state of the node. Nodes themselves have a Nodes collection in which they store their child nodes and so on.

Think of structure of a TreeView class as being similar to that of the menu and menu-item relationship.

Tree views support many events for detecting changes in the nodes. The Before xxxxx events will allow you to trap a change before it is effected and cancel it. The After xxxxx events inform you of the changes that have been made.

Tree views support three event types aside from the ones associated with RichControl from which it derives. These event types are TreeViewEventHandler , TreeViewCancelEventHandler , and NodeLabelEditEventHandler . There are also EventArg derivatives to go along with these event types

The TreeView class, some node operations, and the events associated with them are illustrated shortly in Listing 3.2.10.

Tab Controls

Tab controls can be used to create forms with tabbed pages. They consist of a tab bar that can be selected by the user and a collection of TabPage objects that can host other form controls. Listing 3.2.10, later in this chapter, shows tab controls and a host of other controls.

Dynamic Control Management

As you have seen from all of the previous demonstrations in this chapter, the addition, placement, and programming of controls is all performed at runtime, with no reference to layout resources. This implies that Windows Forms controls are easier to use when it comes to creating forms and dialogs that morph and reshape themselves in reaction to the user's commands. Probably everyone who is familiar with MFC will have tried, at one time or another, to create a dynamically changing dialog and have been frustrated by the difficulty of adding and removing controls and handling the messages they generate. Listing 3.2.10, later in this chapter, shows an application with all of the elements shown previously plus some dynamic control features.

About the MungoTabApp

The application laid out in Listing 3.2.10 is rather large but illustrates many of the important principles of Windows Forms development. The code listing is numbered but is interspersed with comments in the form of notes in addition to the code comments we have added. For your own sanity , do not try typing this application. It's supplied with this book or available for download from the Sams Publishing Web site. (See the Appendixes in this book for information.)

NOTE

At this point, it is important to note that the application shown in Listing 3.2.10 has nothing like the structure of an application built with VisualStudio.NET. To use such an application for educational purposes is an almost impossible task. The form layout engine puts code into a routine called InitializeComponent and does not make any effort to order it in a logical manner. The following application has been laid out as logically as possible for your benefit.


Listing 3.2.10 MungoTabApp.cs : Tabbed Windows and Controls
 1: namespace Sams    2: {    3:     using System;    4:     using System.ComponentModel;    5:     using System.Drawing;    6:     using System.Windows.Forms;    7:     using System.Text;    8:    9:   10:     /// <summary>   11:     ///    The MungoTabApp is to show off many of the capabilities and   12:     ///    ease of use of the Windows Forms system. The application   13:     ///    is contructed as a form with a tab control along the bottom.   14:     ///    Each page in the tab control shows off a different aspect of   15:     ///    Windows Forms usage   16:     /// </summary>   17:     public class MungoTabApp : Form   18:     { 

NOTE

The variables and members in the application are all private. This is good practice for encapsulation. First, all the components are declared.


 19:   20:         // basic parts   21:         private Timer timer1;   22:         private MainMenu mainMenu1;   23:         private TabControl MainTabControl;   24:         private TabPage WelcomeTabPage;   25:         private TabPage SimpleTabPage;   26:         private TabPage DynamicTabPage;   27:         private TabPage ListBoxTabPage;   28:         private TabPage MouseTabPage;   29:         private TabPage TreeTabPage;   30:   31:   32:         //Welcome page.   33:         private RichTextBox WelcomeTextBox;   34:   35:         //Controls for the Simple Controls page   36:         private Label label1;   37:         private Label label2;   38:         private LinkLabel linkLabel1;   39:         private TextBox ClearTextBox;   40:         private TextBox PasswordTextBox;   41:         private GroupBox groupBox1;   42:         private RadioButton radioButton1;   43:         private RadioButton radioButton2;   44:         private Panel panel1;   45:         private RadioButton radioButton3;   46:         private RadioButton radioButton4;   47:         private Button button1;   48:         private CheckBox checkBox1;   49:         private CheckBox checkBox2;   50:   51:         // the listbox page   52:         private ListBox listBox1;   53:         private CheckedListBox checkedListBox1;   54:         private Label label3;   55:         private Label label4;   56:         private Label PickAWord;   57:         private ComboBox comboBox1;   58:         private ListView listView1;   59:         private DateTimePicker dateTimePicker1;   60:         private Label label6;   61:         private Label label7;   62:         private MonthCalendar monthCalendar1;   63:         private Label label10;   64:         private TrackBar trackBar1;   65:         private ProgressBar progressBar1;   66:         private Label label8;   67:         private DomainUpDown domainUpDown1;   68:         private NumericUpDown numericUpDown1;   69:         private Label label9;   70:         private Label label11;   71:   72:   73:         //Mouse movement and dynamic placement   74:         private Button ClickMeButton;   75:   76:         // Dynamic controls   77:         private CheckBox ShowDynamic;   78:         private CheckBox UseAlternates;   79:         private CheckBox HideChecks;   80:         private GroupBox DynGroup;   81:         private RadioButton DynRadioButtn1;   82:         private RadioButton DynRadioButtn2;   83:         private RadioButton DynRadioButtn3;   84:         private RadioButton DynRadioButtn4;   85:         private ListBox EventList1;   86:         private ListBox EventList2;   87:         private Button ClearEvents1;   88:         private Button ClearEvents2;   89:         //TreeView tab   90:         private TreeView treeView1;   91:         private ListBox tvlistBox;   92:         private Button button4;   93:         private Button button5;   94:   95:   96:         private bool ShowingRadioGroup;   97: 

NOTE

The next section initializes the Welcome page. Sections initializing each of the Tab control pages follow.


 98:         private void InitWelcome()   99:         {  100:             WelcomeTextBox=new RichTextBox();  101:             WelcomeTabPage = new TabPage();  102:             WelcomeTabPage.Text = "Welcome";  103:             WelcomeTabPage.Size = new System.Drawing.Size(576, 422);  104:             WelcomeTabPage.TabIndex = 0;  105:             WelcomeTextBox.Text = "Welcome to the Mungo Tab App.\ n"+  106:                 "This Windows Forms demonstration" +  107:                 " application accompanies the Sams C# and the .NET framework"+  108:                 " book by Bob Powell and Richard Weeks.\ n\ nThis tab hosts a"+  109:                 " RichTextBox. You can edit this text if you wish."+  110:                 "\ n\ nThe tabs in this form will show you"+  111:                 " some of the more complex controls that  you can use in your "+  112:                 "Windows Forms application.\ n\ nPlease examine the source code"+  113:                 " for this application carefully.\ n\ nBob Powell.\ n";  114:             WelcomeTextBox.Size = new System.Drawing.Size(576, 424);  115:             WelcomeTextBox.TabIndex = 0;  116:             WelcomeTextBox.Anchor = AnchorStyles.Top   117:                 AnchorStyles.Left   118:                 AnchorStyles.Right   119:                 AnchorStyles.Bottom;  120:             WelcomeTextBox.Visible = true;  121:             WelcomeTabPage.Controls.Add(WelcomeTextBox);  122:             MainTabControl.Controls.Add(WelcomeTabPage);  123:         }  124: 

NOTE

The handlers dedicated to the simple page are in the next section.


 125:         // Handlers for the simple page  126:  127:         private void OnClickedSimple1(Object sender, EventArgs e)  128:         {  129:             // This is one of two handlers that may be attached  to the button  130:             string message = "You clicked the big button";  131:             if(this.checkBox1.Checked)  132:             {  133:                 message = "And the password is.... "+this.PasswordTextBox.Text;  134:             }  135:  136:             MessageBox.Show(message);  137:         }  138:  139:         private void OnCheckColorEdit(Object sender,EventArgs e)  140:         {  141:             // this handler add or removes a second click handler to the button.  142:  143:             if(this.checkBox2.Checked)  144:             {  145:                 this.button1.Click += new EventHandler(OnColorEdit);  146:             }  147:             else  148:             {  149:                 this.button1.Click -= new EventHandler(OnColorEdit);  150:             }  151:         }  152:  153:         private void OnColorEdit(Object sender, EventArgs e)  154:         {  155:             // This second handler is added to the click event of the button  156:             // when the check box is checked.  157:             ColorDialog dlg = new ColorDialog();  158:             if(dlg.ShowDialog() == DialogResult.OK)  159:             {  160:                 this.panel1.BackColor=dlg.Color;  161:             }  162:         }  163:  164:         //This handler is invoked by clicking the link label  165:         //it will take you to a web site.  166:         private void LinkClick(Object sender, EventArgs e)  167:         {  168:             this.linkLabel1.LinkVisited=true;  169:             if(this.ClearTextBox.Text=="This is an editable text box")  170:             {  171:                 System.Diagnostics.Process.Start("IExplore.exe ",  172:                     "http://www.bobpowell.net/");  173:             }  174:             else  175:             {  176:                 try  177:                 {  178:                     System.Diagnostics.Process.Start(ClearTextBox.Text);  179:                 }  180:                 catch(Exception)  181:                 {  182:                     MessageBox.Show("Cannot start process "+ClearTextBox.Text);  183:                 }  184:             }  185:             this.linkLabel1.Text = "Been there, Done that!";  186:         }  187:  188:         //This handler is invoked each time the text in the clear text box  189:         // is modified. It transfers the text to the link button.  190:         // but only if the link has been visited.  191:         private new void TextChanged(Object sender, EventArgs e)  192:         {  193:             if(linkLabel1.LinkVisited )  194:             {  195:                 linkLabel1.Text = ClearTextBox.Text;  196:             }  197:  198:         }  199: 

NOTE

The simple controls page is initialized here.

Just before the actual initialization, we have placed the handlers that are associated with the events on this page. This structure is repeated throughout the application.


 200:         // initializes the simple page.  201:         private void InitSimple()  202:         {  203:             SimpleTabPage = new TabPage();  204:             SimpleTabPage.Size = new System.Drawing.Size(576, 422);  205:             SimpleTabPage.TabIndex = 1;  206:             SimpleTabPage.Text = "Simple controls";  207:  208:             button1 = new Button();  209:             button1.Location = new System.Drawing.Point(32, 240);  210:             button1.Size = new System.Drawing.Size(520, 32);  211:             button1.TabIndex = 7;  212:             button1.Text = "Buttons can be clicked...";  213:             button1.Click+=new EventHandler(OnClickedSimple1);  214:             checkBox1 = new CheckBox();  215:             checkBox1.Location = new System.Drawing.Point(32, 288);  216:             checkBox1.Size = new System.Drawing.Size(520, 16);  217:             checkBox1.TabIndex = 8;  218:             checkBox1.Text =  219:                 "Checking this box will make the button "+  220:                 "above say whats in the password box";  221:             checkBox2 = new CheckBox();  222:             checkBox2.Location = new System.Drawing.Point(32, 327);  223:             checkBox2.Size = new System.Drawing.Size(520, 16);  224:             checkBox2.TabIndex = 9;  225:             checkBox2.Text = "Checking this box will make the button" +  226:                 " above edit the colour of the text panel";  227:             checkBox2.Click += new EventHandler(OnCheckColorEdit);  228:  229:             ClearTextBox = new TextBox();  230:             ClearTextBox.Location = new System.Drawing.Point(344, 8);  231:             ClearTextBox.Size = new System.Drawing.Size(216, 20);  232:             ClearTextBox.TabIndex = 2;  233:             ClearTextBox.Text = "This is an editable text box";  234:  235:             domainUpDown1 = new DomainUpDown();  236:             domainUpDown1.AccessibleName = "DomainUpDown";  237:             domainUpDown1.AccessibleRole = AccessibleRole.ComboBox;  238:             domainUpDown1.Location = new System.Drawing.Point(128, 368);  239:             domainUpDown1.Size = new System.Drawing.Size(144, 20);  240:             domainUpDown1.TabIndex = 10;  241:             domainUpDown1.Text = "domainUpDown1";  242:             domainUpDown1.Items.AddRange(new object []{ "England",  243:                                                           "Africa",  244:                                                           "Mongolia",  245:                                                           "Japan"} );  246:  247:             groupBox1 = new GroupBox();  248:             groupBox1.Location = new System.Drawing.Point(8, 80);  249:             groupBox1.Size = new System.Drawing.Size(560, 80);  250:             groupBox1.TabIndex = 5;  251:             groupBox1.TabStop = false;  252:             groupBox1.Text = "A GroupBox";  253:  254:             label1 = new Label();  255:             label1.Location = new System.Drawing.Point(8, 8);  256:             label1.Size = new System.Drawing.Size(144, 24);  257:             label1.TabIndex = 0;  258:             label1.Text = "This is a label control";  259:  260:             label2 = new Label();  261:             label2.Location = new System.Drawing.Point(8, 41);  262:             label2.Size = new System.Drawing.Size(328, 24);  263:             label2.TabIndex = 4;  264:             label2.Text = "The edit box to the right has a password character";  265:  266:             label9 = new Label();  267:             label9.Location = new System.Drawing.Point(16, 368);  268:             label9.Size = new System.Drawing.Size(104, 24);  269:             label9.TabIndex = 12;  270:             label9.Text = "DomainUpDown";  271:  272:             label10 = new Label();  273:             label10.Location = new System.Drawing.Point(276, 370);  274:             label10.Size = new System.Drawing.Size(104, 24);  275:             label10.TabIndex = 13;  276:             label10.Text = "NumericUpDown";  277:  278:             linkLabel1 = new LinkLabel();  279:             linkLabel1.Location = new System.Drawing.Point(152, 8);  280:             linkLabel1.Size = new System.Drawing.Size(176, 24);  281:             linkLabel1.TabIndex = 1;  282:             linkLabel1.TabStop = true;  283:             linkLabel1.Text = "Link labels are like hypertext links";  284:             linkLabel1.Click += new EventHandler(LinkClick);  285:  286:             numericUpDown1 = new NumericUpDown();  287:             numericUpDown1.BeginInit();  288:             numericUpDown1.EndInit();  289:             numericUpDown1.Location = new System.Drawing.Point(392, 368);  290:             numericUpDown1.Size = new System.Drawing.Size(176, 20);  291:             numericUpDown1.TabIndex = 11;  292:  293:             panel1 = new Panel();  294:             panel1.BackColor =  295:                 (System.Drawing.Color)System.Drawing. Color.FromArgb((byte)255,  296:                 (byte)255,  297:                 (byte)128);  298:             panel1.BorderStyle = (BorderStyle)FormBorderStyle.Fixed3D;  299:             panel1.Location = new System.Drawing.Point(8, 168);  300:             panel1.Size = new System.Drawing.Size(560, 64);  301:             panel1.TabIndex = 6;  302:  303:             radioButton1 = new RadioButton();  304:             radioButton1.Location = new System.Drawing.Point(16, 24);  305:             radioButton1.Size = new System.Drawing.Size(504, 16);  306:             radioButton1.TabIndex = 0;  307:             radioButton1.Text = "RadioButtons";  308:  309:             radioButton2 = new RadioButton();  310:             radioButton2.Location = new System.Drawing.Point(16, 48);  311:             radioButton2.Size = new System.Drawing.Size(504, 16);  312:             radioButton2.TabIndex = 1;  313:             radioButton2.Text = "In a groupBox are used to isolate";  314:  315:             radioButton3 = new RadioButton();  316:             radioButton3.Location = new System.Drawing.Point(16, 8);  317:             radioButton3.Size = new System.Drawing.Size(536, 16);  318:             radioButton3.TabIndex = 0;  319:             radioButton3.Text = "Other radio buttons";  320:  321:             radioButton4 = new RadioButton();  322:             radioButton4.Location = new System.Drawing.Point(16, 32);  323:             radioButton4.Size = new System.Drawing.Size(536, 16);  324:             radioButton4.TabIndex = 1;  325:             radioButton4.Text = "in other GroupBoxes, or in this case, Panels.";  326:  327:             panel1.Controls.Add(radioButton3);  328:             panel1.Controls.Add(radioButton4);  329:  330:             groupBox1.Controls.Add(radioButton1);  331:             groupBox1.Controls.Add(radioButton2);  332:  333:             PasswordTextBox = new TextBox();  334:             PasswordTextBox.Location = new System.Drawing.Point(344, 40);  335:             PasswordTextBox.PasswordChar = '*';  336:             PasswordTextBox.Size = new System.Drawing.Size(216, 20);  337:             PasswordTextBox.TabIndex = 3;  338:             PasswordTextBox.Text = "Password";  339:  340:             ClearTextBox.TextChanged += new EventHandler(TextChanged);  341:  342:             SimpleTabPage.Controls.Add(button1);  343:             SimpleTabPage.Controls.Add(checkBox1);  344:             SimpleTabPage.Controls.Add(checkBox2);  345:             SimpleTabPage.Controls.Add(ClearTextBox);  346:             SimpleTabPage.Controls.Add(domainUpDown1);  347:             SimpleTabPage.Controls.Add(groupBox1);  348:             SimpleTabPage.Controls.Add(label1);  349:             SimpleTabPage.Controls.Add(label10);  350:             SimpleTabPage.Controls.Add(label2);  351:             SimpleTabPage.Controls.Add(label9);  352:             SimpleTabPage.Controls.Add(linkLabel1);  353:             SimpleTabPage.Controls.Add(numericUpDown1);  354:             SimpleTabPage.Controls.Add(panel1);  355:             SimpleTabPage.Controls.Add(PasswordTextBox);  356:         }  357: 

NOTE

Event handlers for the list box tab are in the following section.


 358:         // List box tab event handlers.  359:         // This handler transfers the value of the trackbar to the progress bar.  360:         private void OnTrack(Object sender, EventArgs e)  361:         {  362:             TrackBar b=(TrackBar)sender;  363:             this.progressBar1.Value = b.Value;  364:         }  365:  366:         // This handler constructs a sentence from the checked items in the list  367:         // and displays it in a label to the right of the control.  368:         private void CheckedListHandler(Object sender, ItemCheckEventArgs e)  369:         {  370:             StringBuilder sb=new StringBuilder();  371:             int ni=-1;  372:             if(e.NewValue==CheckState.Checked)  373:                 ni=e.Index;  374:             for(int i=0;i<checkedListBox1.Items.Count;i++)  375:             {  376:                 if(i==ni  (i!=e.Index && checkedListBox1. GetItemChecked(i)))  377:                     sb.Append(checkedListBox1.Items[i].ToString()+" ");  378:             }  379:             PickAWord.Text = sb.ToString();  380:         }  381:  382:         // this handler gets the items from the list box and changes their case  383:         // as the mouse passes over them.  384:         private void ListBoxMouseOver(Object sender, MouseEventArgs e)  385:         {  386:             string s;  387:             int i=0;  388:             // first we reset the case of all the strings  389:             foreach(object o in listBox1.Items)  390:             {  391:                 s=(string)o;  392:                 listBox1.Items[i++]=s.ToLower();  393:             }  394:             i = listBox1.IndexFromPoint(e.X,e.Y);  395:             if(i>-1)  396:             {  397:                 s=(string)listBox1.Items[i];  398:                 listBox1.Items[i]=s.ToUpper();  399:             }  400:         }  401:  402:         // Right clicking the combo box invokes this handler  403:         // it sorts the contents of the dropdown.  404:         private void SortComboboxHandler(Object sender, EventArgs e)  405:         {  406:             this.comboBox1.Sorted=true;  407:         }  408: 

NOTE

The next section illustrates how the list box classes are used. There is also a track bar control and a progress bar with which you can experiment.


 409:         // List box tab initialization.  410:         private void InitLists()  411:         {  412:             ListBoxTabPage = new TabPage();  413:             ListBoxTabPage.Size = new System.Drawing.Size(576, 422);  414:             ListBoxTabPage.TabIndex = 2;  415:             ListBoxTabPage.Text = "List boxes";  416:  417:             checkedListBox1 = new CheckedListBox();  418:             checkedListBox1.Items.AddRange(new object[] { "The",  419:                         "These","All","Words","Men","Are","Can","Be",  420:                         "Might","Not","Be","Made","As","Happy","Equal",  421:                         "Stupid","Lost"} );  422:             checkedListBox1.Location = new System.Drawing.Point(216, 8);  423:             checkedListBox1.Size = new System.Drawing.Size(192, 94);  424:             checkedListBox1.TabIndex = 1;  425:             checkedListBox1.CheckOnClick=true;  426:             checkedListBox1.ItemCheck +=  427:                 new ItemCheckEventHandler(CheckedListHandler);  428:  429:             comboBox1 = new ComboBox();  430:             comboBox1.Items.AddRange(new object[] { "A",  431:                         "Little","aardvark","Never","Hurt",  432:                         "Anyone","5 ","9 ","7 ","1","0 ",  433:                         "2","4","3","6","8"} );  434:             comboBox1.Location = new System.Drawing.Point(8, 144);  435:             comboBox1.Size = new System.Drawing.Size(184, 21);  436:             comboBox1.TabIndex = 5;  437:             comboBox1.Text = "Context menu sorts";  438:             ContextMenu m=new ContextMenu();  439:             MenuItem t=new MenuItem("Sort",new EventHandler (SortComboboxHandler));  440:             m.MenuItems.Add(t);  441:             comboBox1.ContextMenu = m;  442:  443:             dateTimePicker1 = new DateTimePicker();  444:             dateTimePicker1.Location = new System.Drawing.Point(216, 272);  445:             dateTimePicker1.Size = new System.Drawing.Size(344, 20);  446:             dateTimePicker1.TabIndex = 7;  447:  448:             label3 = new Label();  449:             label3.Location = new System.Drawing.Point(8, 112);  450:             label3.Size = new System.Drawing.Size(184, 16);  451:             label3.TabIndex = 2;  452:             label3.Text = "A Simple list box";  453:             label4 = new Label();  454:             label4.Location = new System.Drawing.Point(224, 112);  455:             label4.Size = new System.Drawing.Size(184, 16);  456:             label4.TabIndex = 3;  457:             label4.Text = "A Checked list box";  458:             label6 = new Label();  459:             label6.Location = new System.Drawing.Point(216, 248);  460:             label6.Size = new System.Drawing.Size(184, 16);  461:             label6.TabIndex = 8;  462:             label6.Text = "A list view";  463:             label7 = new Label();  464:             label7.Location = new System.Drawing.Point(214, 303);  465:             label7.Size = new System.Drawing.Size(184, 16);  466:             label7.TabIndex = 9;  467:             label7.Text = "A DateTimePicker";  468:             label8 = new Label();  469:             label8.Location = new System.Drawing.Point(7, 341);  470:             label8.Size = new System.Drawing.Size(184, 16);  471:             label8.TabIndex = 11;  472:             label8.Text = "The MonthCalender control";  473:  474:             label11 = new Label();  475:             label11.Location = new System.Drawing.Point(7, 384);  476:             label11.Size = new System.Drawing.Size(184, 16);  477:             label11.TabIndex = 14;  478:             label11.Text = "Trackbar and progress bar (Right)";  479:  480:             listBox1 = new ListBox();  481:             listBox1.Items.AddRange( new object[] { "Fish",  482:                         "Chips","Vinegar","Marmite","Cream Crackers",  483:                         "Marmalade","Stilton","Mushy Peas",  484:                         "Sherbert Lemons","Wellie boots","Spanners"} );  485:             listBox1.Location = new System.Drawing.Point(8, 8);  486:             listBox1.Size = new System.Drawing.Size(184, 95);  487:             listBox1.TabIndex = 0;  488:             listBox1.MouseMove += new MouseEventHandler(ListBoxMouseOver);  489:  490:             listView1 = new ListView();  491:             listView1.ForeColor = System.Drawing.SystemColors.WindowText;  492:             listView1.Items.Add(new ListViewItem("knives"));  493:             listView1.Items.Add(new ListViewItem("forks"));  494:             listView1.Items.Add(new ListViewItem("spoons"));  495:             listView1.Items.Add(new ListViewItem("dogs"));  496:             listView1.Items.Add(new ListViewItem("fish"));  497:             listView1.Location = new System.Drawing.Point(216, 136);  498:             listView1.Size = new System.Drawing.Size(352, 96);  499:             listView1.TabIndex = 6;  500:  501:             monthCalendar1 = new MonthCalendar();  502:             monthCalendar1.Location = new System.Drawing.Point(8, 184);  503:             monthCalendar1.TabIndex = 10;  504:             monthCalendar1.TabStop = true;  505:  506:             progressBar1 = new ProgressBar();  507:             progressBar1.Location = new System.Drawing.Point(216, 344);  508:             progressBar1.Size = new System.Drawing.Size(336, 24);  509:             progressBar1.TabIndex = 13;  510:  511:             PickAWord = new Label();  512:             PickAWord.Location = new System.Drawing.Point(416, 8);  513:             PickAWord.Size = new System.Drawing.Size(152, 96);  514:             PickAWord.TabIndex = 4;  515:  516:             trackBar1 = new TrackBar();  517:             trackBar1.BeginInit();  518:             trackBar1.Location = new System.Drawing.Point(272, 376);  519:             trackBar1.Maximum = 100;  520:             trackBar1.Size = new System.Drawing.Size(184, 42);  521:             trackBar1.TabIndex = 12;  522:             trackBar1.ValueChanged += new EventHandler(OnTrack);  523:             trackBar1.EndInit();  524:  525:  526:  527:             ListBoxTabPage.Controls.Add(checkedListBox1);  528:             ListBoxTabPage.Controls.Add(comboBox1);  529:             ListBoxTabPage.Controls.Add(dateTimePicker1);  530:             ListBoxTabPage.Controls.Add(label11);  531:             ListBoxTabPage.Controls.Add(label3);  532:             ListBoxTabPage.Controls.Add(label4);  533:             ListBoxTabPage.Controls.Add(label6);  534:             ListBoxTabPage.Controls.Add(label7);  535:             ListBoxTabPage.Controls.Add(label8);  536:             ListBoxTabPage.Controls.Add(listBox1);  537:             ListBoxTabPage.Controls.Add(listView1);  538:             ListBoxTabPage.Controls.Add(monthCalendar1);  539:             ListBoxTabPage.Controls.Add(PickAWord);  540:             ListBoxTabPage.Controls.Add(progressBar1);  541:             ListBoxTabPage.Controls.Add(trackBar1);  542:         } 

NOTE

The event handlers for the Dynamic RadioButtons tab are a little complex. A group of buttons is added or destroyed as needed, and a set of events are added or re-directed according to the user's choice.


 543:  544:         // This is the first of two possible events fired by the  545:         // dynamic radio buttons. It adds to a list box.  546:         private void RadioEvent1(Object sender, EventArgs e)  547:         {  548:             RadioButton r = (RadioButton)sender;  549:             this.EventList1.Items.Add("Event #1 from: "+r.Text);  550:         }  551:  552:         // This is the second of two possible events fired by the  553:         // dynamic radio buttons. It adds to a list box.  554:         private void RadioEvent2(Object sender, EventArgs e)  555:         {  556:             RadioButton r = (RadioButton)sender;  557:             this.EventList2.Items.Add("Event #2 from: "+r.Text);  558:         }  559:  560:         // This handler clears the first list box out  561:         private void Clear1(Object sender,EventArgs e)  562:         {  563:             this.EventList1.Items.Clear();  564:         }  565:  566:         // This handler clears the second list box out  567:         private void Clear2(Object sender,EventArgs e)  568:         {  569:             this.EventList2.Items.Clear();  570:         }  571:  572:         // This routine removes all events from all radio buttons  573:         // in the dynamic page.  574:         private void RemoveEvents()  575:         {  576:             DynRadioButtn4.Click -= new EventHandler(RadioEvent1);  577:             DynRadioButtn3.Click -= new EventHandler(RadioEvent1);  578:             DynRadioButtn2.Click -= new EventHandler(RadioEvent1);  579:             DynRadioButtn1.Click -= new EventHandler(RadioEvent1);  580:             DynRadioButtn4.Click -= new EventHandler(RadioEvent2);  581:             DynRadioButtn3.Click -= new EventHandler(RadioEvent2);  582:             DynRadioButtn2.Click -= new EventHandler(RadioEvent2);  583:             DynRadioButtn1.Click -= new EventHandler(RadioEvent2);  584:         }  585:  586:         // This method add the correct event handler alternative  587:         // to the radiobuttons on the dynamic page  588:         private void AddEvents()  589:         {  590:  591:             if(!this.UseAlternates.Checked )  592:             {  593:                 DynRadioButtn4.Click += new EventHandler(RadioEvent1);  594:                 DynRadioButtn3.Click += new EventHandler(RadioEvent1);  595:                 DynRadioButtn2.Click += new EventHandler(RadioEvent1);  596:                 DynRadioButtn1.Click += new EventHandler(RadioEvent1);  597:             }  598:             else  599:             {  600:                 DynRadioButtn4.Click += new EventHandler(RadioEvent2);  601:                 DynRadioButtn3.Click += new EventHandler(RadioEvent2);  602:                 DynRadioButtn2.Click += new EventHandler(RadioEvent2);  603:                 DynRadioButtn1.Click += new EventHandler(RadioEvent2);  604:             }  605:         }  606:  607:         // This event handler swops the dynamic radio button event handlers  608:         public void OnUseAlternates(Object sender, EventArgs e)  609:         {  610:             if(ShowingRadioGroup)  611:             {  612:                 RemoveEvents();  613:                 AddEvents();  614:             }  615:         }  616:  617:         // This method removes the whole dynamic radiobutton  618:         // panel, clears the event list and destroys the items  619:         public void RemoveRadio()  620:         {  621:             if(ShowingRadioGroup)  622:             {  623:                 DynGroup.Controls.Remove(DynRadioButtn4);  624:                 DynGroup.Controls.Remove(DynRadioButtn3);  625:                 DynGroup.Controls.Remove(DynRadioButtn2);  626:                 DynGroup.Controls.Remove(DynRadioButtn1);  627:                 DynamicTabPage.Controls.Remove(DynGroup);  628:  629:                 RemoveEvents();  630:                 DynamicTabPage.Controls.Remove(DynGroup);  631:  632:                 DynRadioButtn4.Dispose();  633:                 DynRadioButtn3.Dispose();  634:                 DynRadioButtn2.Dispose();  635:                 DynRadioButtn1.Dispose();  636:                 DynGroup.Dispose();  637:  638:                 ShowingRadioGroup = false;  639:  640:             }  641:         }  642:  643:         //This method adds the dynamic radio button group and  644:         //wires up the event handlers.  645:         private void AddRadio()  646:         {  647:             if(!ShowingRadioGroup)  648:             {  649:                 DynGroup = new GroupBox();  650:                 DynGroup.Location = new System.Drawing.Point(240, 16);  651:                 DynGroup.Size = new System.Drawing.Size(312, 120);  652:                 DynGroup.TabIndex = 3;  653:                 DynGroup.TabStop = false;  654:                 DynGroup.Text = "Dynamic radiobuttons";  655:  656:                 DynRadioButtn1 = new RadioButton();  657:                 DynRadioButtn1.Location = new System.Drawing.Point(8, 24);  658:                 DynRadioButtn1.Size = new System.Drawing.Size(296, 16);  659:                 DynRadioButtn1.TabIndex = 0;  660:                 DynRadioButtn1.Text = "Choice 1";  661:  662:                 DynRadioButtn2 = new RadioButton();  663:                 DynRadioButtn2.Location = new System.Drawing.Point(8, 41);  664:                 DynRadioButtn2.Size = new System.Drawing.Size(296, 16);  665:                 DynRadioButtn2.TabIndex = 1;  666:                 DynRadioButtn2.Text = "Choice 2";  667:  668:                 DynRadioButtn3 = new RadioButton();  669:                 DynRadioButtn3.Location = new System.Drawing.Point(8, 64);  670:                 DynRadioButtn3.Size = new System.Drawing.Size(296, 16);  671:                 DynRadioButtn3.TabIndex = 2;  672:                 DynRadioButtn3.Text = "Choice 3";  673:  674:                 DynRadioButtn4 = new RadioButton();  675:                 DynRadioButtn4.Location = new System.Drawing.Point(8, 88);  676:                 DynRadioButtn4.Size = new System.Drawing.Size(296, 16);  677:                 DynRadioButtn4.TabIndex = 3;  678:                 DynRadioButtn4.Text = "Choice 4";  679:  680:                 AddEvents();  681:  682:                 DynGroup.Controls.Add(DynRadioButtn4);  683:                 DynGroup.Controls.Add(DynRadioButtn3);  684:                 DynGroup.Controls.Add(DynRadioButtn2);  685:                 DynGroup.Controls.Add(DynRadioButtn1);  686:                 DynamicTabPage.Controls.Add(DynGroup);  687:  688:                 ShowingRadioGroup = true;  689:             }  690:         }  691:  692:         //This event handler uses helper methods to manage the presence of the  693:         //dynamic radiobutton group and the handlers that they use  694:         private void ShowDynamicEvent(Object sender, EventArgs e)  695:         {  696:             CheckBox b=(CheckBox) sender;  697:  698:             if(b.Checked)  699:             {  700:                 AddRadio();  701:             }  702:             else  703:             {  704:                 RemoveRadio();  705:             }  706:  707:         }  708:  709:         // This event handler adds or removes the dynamic check buttons and  710:         // repositions the control to make it look neat and tidy.  711:         private void AddChecks(Object sender, EventArgs e)  712:         {  713:             CheckBox c=(CheckBox)sender;  714:  715:             if(this.HideChecks.Checked)  716:             {  717:                 RemoveRadio();  718:                 c.Location = new Point(8,16);  719:                 DynamicTabPage.Controls.Remove(UseAlternates);  720:                 DynamicTabPage.Controls.Remove(ShowDynamic);  721:                 ShowDynamic.Click -= new EventHandler(ShowDynamicEvent);  722:                 UseAlternates.Dispose();  723:                 ShowDynamic.Dispose();  724:             }  725:             else  726:             {  727:                 c.Location = new Point(8,64);  728:  729:                 ShowDynamic = new CheckBox();  730:                 ShowDynamic.Location = new System.Drawing.Point(8, 16);  731:                 ShowDynamic.Size = new System.Drawing.Size(168, 16);  732:                 ShowDynamic.TabIndex = 0;  733:                 ShowDynamic.Text = "Show dynamic RadioRuttons";  734:                 ShowDynamic.Click += new EventHandler(ShowDynamicEvent);  735:  736:                 UseAlternates = new CheckBox();  737:                 UseAlternates.Location = new System.Drawing.Point(8, 40);  738:                 UseAlternates.Size = new System.Drawing.Size(168, 16);  739:                 UseAlternates.TabIndex = 1;  740:                 UseAlternates.Text = "Use alternate handlers";  741:                 UseAlternates.Click += new EventHandler(OnUseAlternates);  742:  743:                 DynamicTabPage.Controls.Add(UseAlternates);  744:                 DynamicTabPage.Controls.Add(ShowDynamic);  745:             }  746:         }  747: 

NOTE

Initialization for the Dynamic Controls tab follows in the next section.


 748:         // This method initializes the dynamic buttons tab  749:         private void InitDynamic()  750:         {  751:  752:             DynamicTabPage = new TabPage();  753:             DynamicTabPage.Size = new System.Drawing.Size(576, 422);  754:             DynamicTabPage.TabIndex = 3;  755:             DynamicTabPage.Text = "Dynamic controls";  756:  757:             ClearEvents1 = new Button();  758:             ClearEvents1.Location = new System.Drawing.Point(48, 328);  759:             ClearEvents1.Size = new System.Drawing.Size(128, 24);  760:             ClearEvents1.TabIndex = 6;  761:             ClearEvents1.Text = "Clear the events";  762:             ClearEvents1.Click += new EventHandler(Clear1);  763:  764:             ClearEvents2 = new Button();  765:             ClearEvents2.Location = new System.Drawing.Point(340, 330);  766:             ClearEvents2.Size = new System.Drawing.Size(128, 24);  767:             ClearEvents2.TabIndex = 7;  768:             ClearEvents2.Text = "Clear the events";  769:             ClearEvents2.Click += new EventHandler(Clear2);  770:  771:             EventList1 = new ListBox();  772:             EventList1.Location = new System.Drawing.Point(16, 176);  773:             EventList1.Size = new System.Drawing.Size(200, 121);  774:             EventList1.TabIndex = 4;  775:             EventList2 = new ListBox();  776:             EventList2.Location = new System.Drawing.Point(308, 180);  777:             EventList2.Size = new System.Drawing.Size(200, 121);  778:             EventList2.TabIndex = 5;  779:  780:             HideChecks = new CheckBox();  781:             HideChecks.Location = new System.Drawing.Point(8, 64);  782:             HideChecks.Size = new System.Drawing.Size(168, 16);  783:             HideChecks.TabIndex = 2;  784:             HideChecks.Text = "Hide checkboxes";  785:             HideChecks.Click += new EventHandler(AddChecks);  786:             AddChecks(HideChecks,new EventArgs());  787:  788:             DynamicTabPage.Controls.Add(ClearEvents1);  789:             DynamicTabPage.Controls.Add(ClearEvents2);  790:             DynamicTabPage.Controls.Add(EventList2);  791:             DynamicTabPage.Controls.Add(EventList1);  792:             DynamicTabPage.Controls.Add(HideChecks);  793:         }  794: 

NOTE

Now come the handlers for the mouse interaction tab.


 795:         //  796:         // Handlers for the mouse tab  797:         //  798:  799:  800:         // This handler moves the button to the opposite side of the tab-page  801:         // from the mouse cursor  802:         private void OnMouseMoved(Object sender, MouseEventArgs e)  803:         {  804:             Point center =  805:                 new Point(MouseTabPage.Width/2,MouseTabPage.Height/2);  806:             ClickMeButton.Location =  807:                 new Point(center.X- (ClickMeButton.Size.Width/2)-(e.X-center.X),  808:                 center.Y-(ClickMeButton.Size.Height/2)-(e.Y-center.Y));  809:         }  810:  811:         //This handler shows when the button is caught and clicked.  812:         private void OnClickedClickme(Object sender, EventArgs e)  813:         {  814:             MessageBox.Show("Caught me!!");  815:         }  816: 

NOTE

The Mouse Interaction tab initialization is in the following section.


 817:         // This method initializes the mouse page.  818:         private void InitMouse()  819:         {  820:             MouseTabPage = new TabPage();  821:             MouseTabPage.Controls.Add(ClickMeButton);  822:             MouseTabPage.Size = new System.Drawing.Size(576, 422);  823:             MouseTabPage.TabIndex = 4;  824:             MouseTabPage.Text = "Mouse interaction";  825:  826:             ClickMeButton = new Button();  827:             ClickMeButton.Location = new System.Drawing.Point(200, 128);  828:             ClickMeButton.Size = new System.Drawing.Size(184, 112);  829:             ClickMeButton.TabIndex = 0;  830:             ClickMeButton.Text = "Click me!";  831:             ClickMeButton.Click += new EventHandler(OnClickedClickme);  832:  833:             MouseTabPage.Controls.Add(ClickMeButton);  834:  835:             MouseTabPage.MouseMove += new MouseEventHandler(OnMouseMoved);  836:         }  837: 

NOTE

The handlers for the tree tab follow in this section. Note how the Beforexxx and Afterxxx action handlers are used to create the list of actions taking place.


 838:         //Handlers for the tree tab....  839:  840:  841:         //The overloaded list function shows the treee view events in a list  842:  843:         private void List(string s, TreeNode n)  844:         {  845:             string o=s+" "+n.Text;  846:             tvlistBox.Items.Add(o);  847:         }  848:  849:         private void List(string s, string l, TreeNode n)  850:         {  851:             string o=s+" (new = "+l+") current = "+n.Text;  852:             tvlistBox.Items.Add(o);  853:         }  854:  855:         // These handlers simply reflect the event type and a little bit of  856:         // node data to the list box on the right of the treeView1 control.  857:         private void OnAfterCheck(Object sender,TreeViewEventArgs e)  858:         {  859:             List("AfterCheck", e.Node);  860:         }  861:  862:         private void OnAfterCollapse(Object sender,TreeViewEventArgs e)  863:         {  864:             List("AfterCollapse", e.Node);  865:         }  866:  867:         private void OnAfterExpand(Object sender,TreeViewEventArgs e)  868:         {  869:             List("AfterExpand", e.Node);  870:         }  871:  872:         private void OnAfterSelect(Object sender,TreeViewEventArgs e)  873:         {  874:             List("AfterSelect", e.Node);  875:         }  876:         private void OnBeforeCheck(Object sender,TreeViewCancelEventArgs e)  877:         {  878:             List("AfterCollapse", e.Node);  879:         }  880:         private void OnBeforeCollapse(Object sender,TreeViewCancelEventArgs e)  881:         {  882:             List("BeforeCollapse", e.Node);  883:         }  884:         private void OnBeforeExpand(Object sender,TreeViewCancelEventArgs e)  885:         {  886:             List("BeforeExpand", e.Node);  887:         }  888:         private void OnBeforeLabelEdit (Object sender,NodeLabelEditEventArgs e)  889:         {  890:             List("BeforeEdit", e.Label, e.Node);  891:         }  892:  893:         private void OnAfterLabelEdit(Object sender,NodeLabelEditEventArgs e)  894:         {  895:             List("AfterEdit", e.Label, e.Node);  896:         }  897:  898:  899:         private void OnBeforeSelect(Object sender,TreeViewCancelEventArgs e)  900:         {  901:             List("BeforeSelect", e.Node);  902:         }  903:  904:         private void OnAddRoot(Object sender, EventArgs e)  905:         {  906:             button5.Enabled=true;  907:  908:             if(treeView1.Nodes.Count==0)  909:             {  910:                 treeView1.Nodes.Add(new TreeNode("Root node"));  911:             }  912:             else  913:             {  914:                 treeView1.Nodes.Add(new TreeNode("Sibling node"));  915:             }  916:         }  917:  918:         private void OnAddChild(Object sender, EventArgs e)  919:         {  920:             if(treeView1.SelectedNode==null)  921:                 return;  922:             treeView1.SelectedNode.Nodes.Add(new TreeNode("Child"));  923:         }  924: 

NOTE

The following section initializes the tree control test tab.


 925:         //Initializes the tree control.  926:         private void InitTree()  927:         {  928:             TreeTabPage = new TabPage();  929:             TreeTabPage.Text = "TreeView";  930:             TreeTabPage.Size = new System.Drawing.Size(576, 422);  931:             TreeTabPage.TabIndex = 5;  932:  933:             treeView1 = new TreeView();  934:             treeView1.Anchor = AnchorStyles.Left   935:                 AnchorStyles.Top   936:                 AnchorStyles.Right   937:                 AnchorStyles.Bottom;  938:             treeView1.Size = new System.Drawing.Size(264, 360);  939:             treeView1.TabIndex = 0;  940:             treeView1.ShowLines=true;  941:             treeView1.ShowPlusMinus=true;  942:             treeView1.ShowRootLines=true;  943:             treeView1.LabelEdit=true;  944:  945:             treeView1.AfterCheck +=  946:                 new  TreeViewEventHandler(OnAfterCheck);  947:             treeView1.AfterCollapse +=  948:                 new  TreeViewEventHandler(OnAfterCollapse);  949:             treeView1.AfterExpand +=  950:                 new  TreeViewEventHandler(OnAfterExpand);  951:             treeView1.AfterSelect +=  952:                 new  TreeViewEventHandler(OnAfterSelect);  953:             treeView1.AfterLabelEdit +=  954:                 new  NodeLabelEditEventHandler(OnAfterLabelEdit);  955:             treeView1.BeforeCheck +=  956:                 new  TreeViewCancelEventHandler(OnBeforeCheck);  957:             treeView1.BeforeCollapse +=  958:                 new  TreeViewCancelEventHandler(OnBeforeCollapse);  959:             treeView1.BeforeExpand +=  960:                 new  TreeViewCancelEventHandler(OnBeforeExpand);  961:             treeView1.BeforeSelect +=  962:                 new  TreeViewCancelEventHandler(OnBeforeSelect);  963:             treeView1.BeforeLabelEdit +=  964:                 new  NodeLabelEditEventHandler(OnBeforeLabelEdit);  965:  966:  967:             tvlistBox = new ListBox();  968:             tvlistBox.Location = new System.Drawing.Point(272, 0);  969:             tvlistBox.Size = new System.Drawing.Size(304, 424);  970:             tvlistBox.ForeColor = System.Drawing.SystemColors.WindowText;  971:             tvlistBox.TabIndex = 1;  972:  973:  974:             button4 = new Button();  975:             button4.Location = new System.Drawing.Point(16, 376);  976:             button4.Size = new System.Drawing.Size(96, 24);  977:             button4.TabIndex = 2;  978:             button4.Text = "Add Root";  979:             button4.Click += new EventHandler(OnAddRoot);  980:  981:             button5 = new Button();  982:             button5.Location = new System.Drawing.Point(138, 376);  983:             button5.Size = new System.Drawing.Size(96, 24);  984:             button5.TabIndex = 3;  985:             button5.Text = "Add Child";  986:             button5.Click += new EventHandler(OnAddChild);  987:             button5.Enabled=false;  988:  989:             TreeTabPage.Controls.Add(button4);  990:             TreeTabPage.Controls.Add(button5);  991:             TreeTabPage.Controls.Add(tvlistBox);  992:             TreeTabPage.Controls.Add(treeView1);  993:         }  994:  995:  996: 

NOTE

The application puts it all together by calling all the initializers and adding all the tabs to the main page.


 997:         public MungoTabApp()  998:         {  999:             //        components = new System.ComponentModel.Container(); 1000:             AutoScaleBaseSize = new System.Drawing.Size(5, 13); 1001:             ClientSize = new System.Drawing.Size(600, 477); 1002: 1003:             MainTabControl = new TabControl(); 1004: 1005:             MainTabControl.Location = new System.Drawing.Point(8, 8); 1006:             MainTabControl.SelectedIndex = 0; 1007:             MainTabControl.Size = new System.Drawing.Size(584, 448); 1008:             MainTabControl.TabIndex = 0; 1009: 1010:             InitWelcome(); 1011:             InitSimple(); 1012:             InitLists(); 1013:             InitDynamic(); 1014:             InitMouse(); 1015:             InitTree(); 1016: 1017:             mainMenu1 = new MainMenu(); 1018:             Menu = mainMenu1; 1019:             Text = "Mungo Tab App"; 1020:             timer1 = new Timer(); 1021: 1022:             MainTabControl.Controls.Add(SimpleTabPage); 1023:             MainTabControl.Controls.Add(ListBoxTabPage); 1024:             MainTabControl.Controls.Add(DynamicTabPage); 1025:             MainTabControl.Controls.Add(MouseTabPage); 1026:             MainTabControl.Controls.Add(TreeTabPage); 1027: 1028:             Controls.Add(MainTabControl); 1029: 1030:         } 1031: 

NOTE

Finally, we add the Main static function that bootstraps the whole process.


 1032:         public static int Main(string[] args) 1033:         { 1034:             Application.Run(new MungoTabApp()); 1035:             return 0; 1036:         } 1037:     } 1038: } 
I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net