Browse files

2006-10-30 Jonathan Pobst <monkey@jpobst.com>

        * ToolStripDemo.cs: Update to showcase MenuStrip.

svn path=/trunk/winforms/; revision=67178
  • Loading branch information...
1 parent 0bb27ae commit a2a4182e1d8aa23a880304673d136751218235f0 @jpobst jpobst committed Oct 31, 2006
Showing with 146 additions and 45 deletions.
  1. +146 −45 toolstrip20/ToolStripDemo.cs
View
191 toolstrip20/ToolStripDemo.cs
@@ -11,55 +11,60 @@ public class ToolStripDemo : Form
[STAThread]
static void Main ()
{
- Application.EnableVisualStyles ();
- Application.Run (new ToolStripDemo ());
+ Application.EnableVisualStyles ();
+ Application.Run (new ToolStripDemo ());
}
private ToolStrip ts;
private TextBox rtb;
private string image_path;
private ToolStripComboBox tscb;
private ToolStripComboBox font_combo;
-
- public ToolStripDemo()
+ private MenuStrip ms;
+
+ public ToolStripDemo ()
{
- this.Text = "ToolStrip Sample";
- this.Size = new Size(750, 450);
+ this.Text = "ToolStrip Notepad Sample";
+ this.Size = new Size (750, 450);
+ this.StartPosition = FormStartPosition.CenterScreen;
image_path = Path.Combine (Path.GetDirectoryName (Application.ExecutablePath), "images");
rtb = new TextBox ();
rtb.Multiline = true;
rtb.Dock = DockStyle.Fill;
+ rtb.BorderStyle = BorderStyle.FixedSingle;
+ rtb.MouseUp += new MouseEventHandler (rtb_MouseUp);
this.Controls.Add (rtb);
-
+
ts = new ToolStrip ();
- this.Controls.Add(ts);
+ this.Controls.Add (ts);
Image image1 = Image.FromFile (Path.Combine (image_path, "document-new.png"));
- ToolStripButton tb1 = new ToolStripButton ("New Document", image1, new EventHandler (New_Document_Clicked));
+ ToolStripButton tb1 = new ToolStripButton ("&New Document", image1, new EventHandler (New_Document_Clicked));
tb1.DisplayStyle = ToolStripItemDisplayStyle.Image;
ts.Items.Add (tb1);
Image image2 = Image.FromFile (Path.Combine (image_path, "document-open.png"));
- ToolStripButton tb2 = new ToolStripButton ("Open Document", image2, new EventHandler (Open_Document_Clicked));
+ ToolStripButton tb2 = new ToolStripButton ("&Open Document", image2, new EventHandler (Open_Document_Clicked));
tb2.DisplayStyle = ToolStripItemDisplayStyle.Image;
ts.Items.Add (tb2);
Image image3 = Image.FromFile (Path.Combine (image_path, "document-save.png"));
- ToolStripButton tb3 = new ToolStripButton ("Save Document", image3, new EventHandler (Save_Document_Clicked));
+ ToolStripButton tb3 = new ToolStripButton ("&Save Document", image3, new EventHandler (Save_Document_Clicked));
tb3.DisplayStyle = ToolStripItemDisplayStyle.Image;
ts.Items.Add (tb3);
-
- ts.Items.Add(new ToolStripSeparator());
+
+ ts.Items.Add (new ToolStripSeparator ());
Image image5 = Image.FromFile (Path.Combine (image_path, "edit-cut.png"));
- ToolStripButton tb5 = new ToolStripButton ("Cut", image5, new EventHandler (Cut_Clicked));
+ ToolStripButton tb5 = new ToolStripButton ("Cut", image5, new EventHandler (Cut_Clicked), "Cut");
tb5.DisplayStyle = ToolStripItemDisplayStyle.Image;
+ tb5.Enabled = false;
ts.Items.Add (tb5);
Image image6 = Image.FromFile (Path.Combine (image_path, "edit-copy.png"));
- ToolStripButton tb6 = new ToolStripButton ("Copy", image6, new EventHandler (Copy_Clicked));
+ ToolStripButton tb6 = new ToolStripButton ("Copy", image6, new EventHandler (Copy_Clicked), "Copy");
tb6.DisplayStyle = ToolStripItemDisplayStyle.Image;
ts.Items.Add (tb6);
@@ -102,18 +107,86 @@ public ToolStripDemo()
ts.Items.Add (tscb);
- ToolStripLabel tsl2 = new ToolStripLabel("Button Style:");
- ts.Items.Add (tsl2);
-
Image image10 = Image.FromFile (Path.Combine (image_path, "image-x-generic.png"));
- ToolStripButton tb10 = new ToolStripButton ("Style", image10, new EventHandler(Change_Style_Clicked));
- tb10.DisplayStyle = ToolStripItemDisplayStyle.Image;
- ts.Items.Add (tb10);
font_combo.SelectedIndex = font_combo.FindStringExact (rtb.Font.Name);
- tscb.SelectedIndex = tscb.FindStringExact (rtb.Font.Size.ToString());
+ tscb.SelectedIndex = tscb.FindStringExact (rtb.Font.Size.ToString ());
+
+ ms = new MenuStrip ();
+ ms.Dock = DockStyle.Top;
+ this.Controls.Add (ms);
+
+ // Top level menu
+ ToolStripMenuItem mi = new ToolStripMenuItem ("File");
+ ToolStripMenuItem mi2 = new ToolStripMenuItem ("Edit");
+ ToolStripMenuItem mi3 = new ToolStripMenuItem ("View");
+ ToolStripMenuItem mi4 = new ToolStripMenuItem ("Tools");
+ ToolStripMenuItem mi5 = new ToolStripMenuItem ("Help");
+ ms.Items.Add (mi);
+ ms.Items.Add (mi2);
+ ms.Items.Add (mi3);
+ ms.Items.Add (mi4);
+ ms.Items.Add (mi5);
+
+ // File menu items
+ mi.DropDownItems.Add ("New", image1, new EventHandler (New_Document_Clicked));
+ mi.DropDownItems.Add ("Open", image2, new EventHandler (Open_Document_Clicked));
+ mi.DropDownItems.Add ("Save", image3, new EventHandler (Save_Document_Clicked));
+ mi.DropDownItems.Add ("-");
+ mi.DropDownItems.Add ("Exit", null, new EventHandler (Exit_Clicked));
+
+ // Edit menu items
+ mi2.DropDownItems.Add ("Undo", null, new EventHandler (Undo_Clicked));
+ mi2.DropDownItems.Add ("-");
+ mi2.DropDownItems.Add ("Cut", image5, new EventHandler (Cut_Clicked)).Name = "Cut";
+ mi2.DropDownItems.Add ("Copy", image6, new EventHandler (Copy_Clicked)).Name = "Copy";
+ mi2.DropDownItems.Add ("Paste", image7, new EventHandler (Paste_Clicked));
+
+ // View menu items
+ ToolStripMenuItem WordWrapMenuItem = (ToolStripMenuItem)mi3.DropDownItems.Add ("Word Wrap");
+ WordWrapMenuItem.CheckOnClick = true;
+ WordWrapMenuItem.CheckedChanged += new EventHandler (WordWrap_CheckStateChanged);
+ WordWrapMenuItem.Checked = true;
+
+ ToolStripMenuItem ToolStripStyleMenuItem = (ToolStripMenuItem)mi3.DropDownItems.Add ("ToolStrip Style", image10);
+
+ // ToolStripStyle sub menu items
+ ToolStripStyleMenuItem.DropDownItems.Add ("Image", null, new EventHandler (Change_Style_Clicked));
+ ToolStripStyleMenuItem.DropDownItems.Add ("Image and Text", null, new EventHandler (Change_Style_Clicked));
+ ToolStripStyleMenuItem.DropDownItems.Add ("Text", null, new EventHandler (Change_Style_Clicked));
+ ToolStripStyleMenuItem.DropDownItems.Add ("None", null, new EventHandler (Change_Style_Clicked));
+
+ (ToolStripStyleMenuItem.DropDownItems[0] as ToolStripMenuItem).Checked = true;
+
+ // Help menu items
+ mi5.DropDownItems.Add ("Contents", null, new EventHandler (NotImplemented));
+ mi5.DropDownItems.Add ("Search", null, new EventHandler (NotImplemented));
+ mi5.DropDownItems.Add ("-");
+ mi5.DropDownItems.Add ("About", null, new EventHandler (NotImplemented));
+
+ rtb_MouseUp (null, null);
}
-
+
+ void rtb_MouseUp (object sender, MouseEventArgs e)
+ {
+ bool enabled = rtb.SelectionLength > 0;
+
+ ts.Items["Cut"].Enabled = enabled;
+ ts.Items["Copy"].Enabled = enabled;
+ (ms.Items[1] as ToolStripMenuItem).DropDownItems["Cut"].Enabled = enabled;
+ (ms.Items[1] as ToolStripMenuItem).DropDownItems["Copy"].Enabled = enabled;
+ }
+
+ void WordWrap_CheckStateChanged (object sender, EventArgs e)
+ {
+ rtb.WordWrap = (sender as ToolStripMenuItem).Checked;
+ }
+
+ void Exit_Clicked (object sender, EventArgs e)
+ {
+ this.Close();
+ }
+
void font_combo_SelectedIndexChanged (object sender, EventArgs e)
{
if (font_combo.SelectedIndex >= 0)
@@ -123,23 +196,22 @@ void font_combo_SelectedIndexChanged (object sender, EventArgs e)
void tscb_SelectedIndexChanged (object sender, EventArgs e)
{
if (tscb.SelectedIndex >= 0)
- rtb.Font = new Font (rtb.Font.Name, float.Parse(tscb.SelectedItem.ToString()));
+ rtb.Font = new Font (rtb.Font.Name, float.Parse (tscb.SelectedItem.ToString ()));
}
public void New_Document_Clicked (object sender, EventArgs e)
{
- rtb.Clear();
- }
-
+ rtb.Clear ();
+ }
+
public void Open_Document_Clicked (object sender, EventArgs e)
{
- OpenFileDialog ofd = new OpenFileDialog();
-
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- StreamReader sr = new StreamReader(ofd.OpenFile());
- rtb.Text = sr.ReadToEnd();
- sr.Close();
+ OpenFileDialog ofd = new OpenFileDialog ();
+
+ if (ofd.ShowDialog () == DialogResult.OK) {
+ StreamReader sr = new StreamReader (ofd.OpenFile ());
+ rtb.Text = sr.ReadToEnd ();
+ sr.Close ();
}
}
@@ -149,37 +221,66 @@ public void Save_Document_Clicked (object sender, EventArgs e)
if (sfd.ShowDialog () == DialogResult.OK) {
StreamWriter sw = new StreamWriter (sfd.OpenFile ());
- sw.Write(rtb.Text);
- sw.Close();
+ sw.Write (rtb.Text);
+ sw.Close ();
}
}
+ public void Undo_Clicked (object sender, EventArgs e)
+ {
+ rtb.Undo ();
+ }
+
public void Cut_Clicked (object sender, EventArgs e)
{
- rtb.Cut();
+ rtb.Cut ();
}
public void Copy_Clicked (object sender, EventArgs e)
{
- rtb.Copy();
+ rtb.Copy ();
}
public void Paste_Clicked (object sender, EventArgs e)
{
- rtb.Paste();
+ rtb.Paste ();
}
public void Change_Style_Clicked (object sender, EventArgs e)
{
- int style = (int)ts.Items[0].DisplayStyle + 1;
+ ToolStripMenuItem tsi = (ToolStripMenuItem)sender;
- if (style > 3)
- style = 1;
+ foreach (ToolStripMenuItem tsi2 in (tsi.OwnerItem as ToolStripMenuItem).DropDownItems)
+ tsi2.Checked = false;
- foreach (ToolStripItem tsi in ts.Items)
- if (tsi is ToolStripButton)
- tsi.DisplayStyle = (ToolStripItemDisplayStyle)style;
+ tsi.Checked = true;
+
+ ToolStripItemDisplayStyle style;
+
+ switch (tsi.Text) {
+ case "Image":
+ default:
+ style = ToolStripItemDisplayStyle.Image;
+ break;
+ case "Image and Text":
+ style = ToolStripItemDisplayStyle.ImageAndText;
+ break;
+ case "Text":
+ style = ToolStripItemDisplayStyle.Text;
+ break;
+ case "None":
+ style = ToolStripItemDisplayStyle.None;
+ break;
+ }
+
+ foreach (ToolStripItem tsi2 in ts.Items)
+ if (tsi2 is ToolStripButton)
+ tsi2.DisplayStyle = style;
}
+ public void NotImplemented (object sender, EventArgs e)
+ {
+ MessageBox.Show ("Nothing to see here");
+ }
}
}

0 comments on commit a2a4182

Please sign in to comment.