Observer Pattern Code Review
Define the Windows Form Graphical User Interface
Code Review
- Observer Pattern Code Review: Define the Pattern Interface and Handler Object
- Observer Pattern Code Review: Define the Subject, Blogs and Simulator Objects
- Observer Pattern Code Review: Define the Windows Form Graphical User Interface
- Observer Pattern Code Review: Use the Observer Pattern
Code Walkthrough
The Interact class (object) creates the GUI interface to the application.
class Interact : Form
{
public TextBox wall;
public Button subscribeButton;
public Button unSubscribeButton;
public TextBox messageBox;
String name;
public Interact(String name, EventHandler Input) { Control.CheckForIllegalCrossThreadCalls = true;
this.name = name; wall = new TextBox(); wall.Multiline = true; wall.Location = new Point(0, 30); wall.Width = 300; wall.Height = 200; wall.AcceptsReturn = true; wall.Dock = DockStyle.Fill; this.Text = name; this.Controls.Add(wall);
Panel p = new Panel(); messageBox = new TextBox(); messageBox.Width = 120; p.Controls.Add(messageBox);
subscribeButton = new Button(); subscribeButton.Left = messageBox.Width; subscribeButton.Text = "Subscribe"; subscribeButton.Click += new EventHandler(Input); p.Controls.Add(subscribeButton);
unSubscribeButton = new Button(); unSubscribeButton.Left = messageBox.Width + subscribeButton.Width; unSubscribeButton.Text = "Unsubscribe"; unSubscribeButton.Click += new EventHandler(Input); p.Controls.Add(unSubscribeButton);
p.Height = subscribeButton.Height; p.Height = unSubscribeButton.Height; p.Dock = DockStyle.Top; this.Controls.Add(p); } public void Output(String message) { if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { Output(message); }); } else { wall.AppendText(String.Format("{0}\r\n", message)); this.Show(); } } }
String name;
public Interact(String name, EventHandler Input) { Control.CheckForIllegalCrossThreadCalls = true;
this.name = name; wall = new TextBox(); wall.Multiline = true; wall.Location = new Point(0, 30); wall.Width = 300; wall.Height = 200; wall.AcceptsReturn = true; wall.Dock = DockStyle.Fill; this.Text = name; this.Controls.Add(wall);
Panel p = new Panel(); messageBox = new TextBox(); messageBox.Width = 120; p.Controls.Add(messageBox);
subscribeButton = new Button(); subscribeButton.Left = messageBox.Width; subscribeButton.Text = "Subscribe"; subscribeButton.Click += new EventHandler(Input); p.Controls.Add(subscribeButton);
unSubscribeButton = new Button(); unSubscribeButton.Left = messageBox.Width + subscribeButton.Width; unSubscribeButton.Text = "Unsubscribe"; unSubscribeButton.Click += new EventHandler(Input); p.Controls.Add(unSubscribeButton);
p.Height = subscribeButton.Height; p.Height = unSubscribeButton.Height; p.Dock = DockStyle.Top; this.Controls.Add(p); } public void Output(String message) { if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { Output(message); }); } else { wall.AppendText(String.Format("{0}\r\n", message)); this.Show(); } } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!