Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'        Slide program -- Keyboard shortcut: 't'    A window with two buttons and a textbox.Lecture 6 - slide 14 : 20
Program 4
using System;
using System.Windows.Forms;
using System.Drawing;

// In System:
// public delegate void EventHandler (Object sender, EventArgs e)

public class Window: Form{   

  private Button b1, b2;     
  private TextBox tb;        

  // Constructor
  public Window (){               
    this.Size=new Size(150,200);  
                                  
    b1 = new Button();            
    b1.Text="Click Me";
    b1.Size=new Size(100,25);
    b1.Location = new Point(25,25);
    b1.BackColor = Color.Yellow;
    b1.Click += ClickHandler;                              
                              // Alternatively:            
                              // b1.Click+=new EventHandler(ClickHandler);
    b2 = new Button();  
    b2.Text="Erase";
    b2.Size=new Size(100,25);
    b2.Location = new Point(25,55);
    b2.BackColor=Color.Green; 
    b2.Click += EraseHandler; 
                              // Alternatively:
                              // b2.Click+=new EventHandler(EraseHandler);
    tb = new TextBox();                                    
    tb.Location = new Point(25,100);
    tb.Size=new Size(100,25);
    tb.BackColor=Color.White;
    tb.ReadOnly=true;
    tb.RightToLeft=RightToLeft.Yes;

    this.Controls.Add(b1);    
    this.Controls.Add(b2);    
    this.Controls.Add(tb);    
  }

  // Event handler:
  private void ClickHandler(object obj, EventArgs ea) {   
    tb.Text = "You clicked me";                           
  }                                                       

  // Event handler:                                       
  private void EraseHandler(object obj, EventArgs ea) {   
    tb.Text = "";                                         
  }

}

class ButtonTest{   

  public static void Main(){
    Window win = new Window();   
    Application.Run(win);        
  }

}
 
 
 
 
 
 
 
A Window is a Form (inherits from Form).
 
Two button variables
and a text box variable.
 
 
The Window constructor.
Large, because it constructs and
sets up the graphical user interface.
Make button and assigns it to b1.
 
 
 
 
Add the method ClickHandler to the
Click event handler.
 
Similar for the other button.
 
 
 
 
 
 
 
Now set up the text box.
 
 
 
 
 
 
Add the two buttons and the
text box to the current Window 
object.
 
 
 
A method that conforms to the
EventHandler. Actived when 'Click Me'
clicked.
 
Another method that conforms to 
EventHandler. Activated when 'Erase'
is clicked.
 
 
 
 
A client of Window.
 
 
Makes a Window.
Starts the event-driven program.