// Obtain a new color from the color grid. void ColorGridOnClick(object objSrc, EventArgs args) { ToolStripColorGrid clrgrid = (ToolStripColorGrid)objSrc; PropertyInfo pi = (PropertyInfo)clrgrid.Tag; pi.SetValue(txtbox, clrgrid.SelectedColor, null); }
public CustomColorMenu() { Text = "Custom Color Menu"; MenuStrip menu = new MenuStrip(); menu.Parent = this; ToolStripMenuItem itemFormat = new ToolStripMenuItem("&Format"); itemFormat.DropDownOpening += FormatOnDropDownOpening; menu.Items.Add(itemFormat); ToolStripMenuItem itemBackground = new ToolStripMenuItem("&Background Color"); itemFormat.DropDownItems.Add(itemBackground); clrgrd = new ToolStripColorGrid(); clrgrd.Click += ColorGridOnClick; itemBackground.DropDownItems.Add(clrgrd); itemBackground.DropDownItems.Add(new ToolStripSeparator()); ToolStripMenuItem item = new ToolStripMenuItem("&More Colors..."); item.Click += MoreColorsOnClick; itemBackground.DropDownItems.Add(item); }
// Initialize the color grid when the drop down is opening. void ColorOnDropDownOpening(object objSrc, EventArgs args) { ToolStripSplitButton btn = (ToolStripSplitButton)objSrc; ToolStripColorGrid clrgrid = (ToolStripColorGrid)btn.DropDownItems["ColorGrid"]; PropertyInfo pi = (PropertyInfo)clrgrid.Tag; clrgrid.SelectedColor = (Color)pi.GetValue(txtbox, null); }
public FormattingToolStrip() { Text = "Formatting ToolStrip"; Width = 800; // Obtain the horizontal resolution of the screen in dots per inch. Graphics grfx = CreateGraphics(); xDpi = grfx.DpiX; grfx.Dispose(); // Load in many bitmaps for the tool strip. ImageList imglst = new ImageList(); imglst.TransparentColor = Color.Magenta; imglst.Images.Add("BackColor", new Bitmap(GetType(), "ChooseColor.bmp")); imglst.Images.Add("ForeColor", new Bitmap(GetType(), "Forecolor.bmp")); imglst.Images.Add("Left", new Bitmap(GetType(), "AlignTableCellMiddleLeftJustHS.bmp")); imglst.Images.Add("Right", new Bitmap(GetType(), "AlignTableCellMiddleRight.bmp")); imglst.Images.Add("Center", new Bitmap(GetType(), "AlignTableCellMiddleCenter.bmp")); imglst.Images.Add("Bullets", new Bitmap(GetType(), "List_Bullets.bmp")); // Create a few more bitmaps. imglst.Images.Add("Bold", FontStyleBitmap("B", FontStyle.Bold)); imglst.Images.Add("Italic", FontStyleBitmap("I", FontStyle.Italic)); imglst.Images.Add("Underline", FontStyleBitmap("U", FontStyle.Underline)); imglst.Images.Add("Strikeout", FontStyleBitmap("S", FontStyle.Strikeout)); // Create the RichTextBox control that dominates the client area. txtbox = new RichTextBox(); txtbox.Parent = this; txtbox.Dock = DockStyle.Fill; txtbox.SelectionChanged += TextBoxOnSelectionChanged; // Create the ToolStrip control. ToolStrip tool = new ToolStrip(); tool.Parent = this; tool.ImageList = imglst; // Create and fill the font name combo box. comboName = new ToolStripComboBox(); comboName.ToolTipText = "Font Name"; comboName.SelectedIndexChanged += NameOnSelectionChanged; tool.Items.Add(comboName); foreach (FontFamily fntfam in FontFamily.Families) { comboName.Items.Add(fntfam.Name); } // Create and fill the font size combo box. comboSize = new ToolStripComboBox(); comboSize.ToolTipText = "Font Size"; comboSize.SelectedIndexChanged += SizeOnSelectionChanged; tool.Items.Add(comboSize); for (int i = 8; i <= 10; i++) { comboSize.Items.Add(i.ToString()); } for (int i = 12; i <= 28; i += 2) { comboSize.Items.Add(i.ToString()); } for (int i = 36; i <= 72; i += 12) { comboSize.Items.Add(i.ToString()); } // Create the bold, italic, underline, and strikeout buttons. btnBold = new ToolStripButton(); btnBold.ImageKey = "Bold"; btnBold.ToolTipText = "Bold"; btnBold.Tag = FontStyle.Bold; btnBold.CheckOnClick = true; btnBold.Click += FontStyleOnClick; tool.Items.Add(btnBold); btnItalic = new ToolStripButton(); btnItalic.ImageKey = "Italic"; btnItalic.ToolTipText = "Italic"; btnItalic.Tag = FontStyle.Italic; btnItalic.CheckOnClick = true; btnItalic.Click += FontStyleOnClick; tool.Items.Add(btnItalic); btnUnderline = new ToolStripButton(); btnUnderline.ImageKey = "Underline"; btnUnderline.ToolTipText = "Underline"; btnUnderline.Tag = FontStyle.Underline; btnUnderline.CheckOnClick = true; btnUnderline.Click += FontStyleOnClick; tool.Items.Add(btnUnderline); btnStrikeout = new ToolStripButton(); btnStrikeout.ImageKey = "Strikeout"; btnStrikeout.ToolTipText = "Strikeout"; btnStrikeout.CheckOnClick = true; btnStrikeout.Tag = FontStyle.Strikeout; btnStrikeout.Click += FontStyleOnClick; tool.Items.Add(btnStrikeout); tool.Items.Add(new ToolStripSeparator()); // Create background color drop-down button. ToolStripSplitButton spltbtn = new ToolStripSplitButton(); spltbtn.ImageKey = "BackColor"; spltbtn.ToolTipText = "Background Color"; spltbtn.ButtonClick += delegate { txtbox.SelectionBackColor = txtbox.BackColor; }; spltbtn.DropDownOpening += ColorOnDropDownOpening; tool.Items.Add(spltbtn); // Create PropertyInfo for background color. PropertyInfo pi = typeof(RichTextBox).GetProperty("SelectionBackColor"); // Add Color Grid to drop down. ToolStripColorGrid clrgrid = new ToolStripColorGrid(); clrgrid.Name = "ColorGrid"; clrgrid.Tag = pi; clrgrid.Click += ColorGridOnClick; spltbtn.DropDownItems.Add(clrgrid); spltbtn.DropDownItems.Add(new ToolStripSeparator()); // Add "More Colors" item to drop down. ToolStripMenuItem item = new ToolStripMenuItem("More colors..."); item.Tag = pi; item.Click += MoreColorsOnClick; spltbtn.DropDownItems.Add(item); // Create foreground color button likewise. spltbtn = new ToolStripSplitButton(); spltbtn.ImageKey = "ForeColor"; spltbtn.ToolTipText = "Font Color"; spltbtn.ButtonClick += delegate { txtbox.SelectionColor = txtbox.ForeColor; }; spltbtn.DropDownOpening += ColorOnDropDownOpening; tool.Items.Add(spltbtn); // Create PropertyInfo for foreground color. pi = typeof(RichTextBox).GetProperty("SelectionColor"); // Add Color Grid and "More Colors" items. clrgrid = new ToolStripColorGrid(); clrgrid.Name = "ColorGrid"; clrgrid.Tag = pi; clrgrid.Click += ColorGridOnClick; spltbtn.DropDownItems.Add(clrgrid); spltbtn.DropDownItems.Add(new ToolStripSeparator()); item = new ToolStripMenuItem("More colors..."); item.Tag = pi; item.Click += MoreColorsOnClick; spltbtn.DropDownItems.Add(item); tool.Items.Add(new ToolStripSeparator()); // Create buttons for left, right, and center alignment. btnLeft = new ToolStripButton(); btnLeft.ImageKey = "Left"; btnLeft.ToolTipText = "Align Left"; btnLeft.Tag = HorizontalAlignment.Left; btnLeft.Checked = true; btnLeft.Click += AlignOnClick; tool.Items.Add(btnLeft); btnRight = new ToolStripButton(); btnRight.ImageKey = "Right"; btnLeft.ToolTipText = "Align Right"; btnRight.Tag = HorizontalAlignment.Right; btnRight.Click += AlignOnClick; tool.Items.Add(btnRight); btnCenter = new ToolStripButton(); btnCenter.ImageKey = "Center"; btnLeft.ToolTipText = "Align Center"; btnCenter.Tag = HorizontalAlignment.Center; btnCenter.Click += AlignOnClick; tool.Items.Add(btnCenter); // Create button for bullets. btnBullets = new ToolStripButton(); btnBullets.ImageKey = "Bullets"; btnBullets.ToolTipText = "Bullets"; btnBullets.CheckOnClick = true; btnBullets.Click += BulletsOnClick; tool.Items.Add(btnBullets); tool.Items.Add(new ToolStripSeparator()); // Create labels and text boxes for indentation. ToolStripLabel lbl = new ToolStripLabel("Left:"); tool.Items.Add(lbl); txtLeftIndent = new ToolStripTextBox(); txtLeftIndent.ToolTipText = "Left Indentation in Inches"; txtLeftIndent.TextChanged += IndentOnTextChanged; tool.Items.Add(txtLeftIndent); lbl = new ToolStripLabel("Right:"); tool.Items.Add(lbl); txtRightIndent = new ToolStripTextBox(); txtRightIndent.ToolTipText = "Right Indentation in Inches"; txtRightIndent.TextChanged += IndentOnTextChanged; tool.Items.Add(txtRightIndent); lbl = new ToolStripLabel("First line:"); tool.Items.Add(lbl); txtFirstLine = new ToolStripTextBox(); txtFirstLine.ToolTipText = "First Line Indentation in Inches"; txtFirstLine.TextChanged += IndentOnTextChanged; tool.Items.Add(txtFirstLine); // Initialize the ToolStrip. TextBoxOnSelectionChanged(txtbox, EventArgs.Empty); }