new ToolBarButtonClickEventHandler
using System;
using System.Drawing;
using System.Windows.Forms;
class TextBoxWithToolBar: Form
{
TextBox txtbox = new TextBox();
MenuItem miEditCut, miEditCopy, miEditPaste;
ToolBarButton tbbCut, tbbCopy, tbbPaste;
public static void Main()
{
System.Threading.Thread.CurrentThread.ApartmentState =
System.Threading.ApartmentState.STA;
Application.Run(new TextBoxWithToolBar());
}
public TextBoxWithToolBar()
{
txtbox.Parent = this;
txtbox.Dock = DockStyle.Fill;
txtbox.Multiline = true;
txtbox.ScrollBars = ScrollBars.Both;
txtbox.AcceptsTab = true;
Bitmap bm = new Bitmap(GetType(), "TextBoxWithToolBar.bmp");
ImageList imglst = new ImageList();
imglst.Images.AddStrip(bm);
imglst.TransparentColor = Color.Cyan;
ToolBar tbar = new ToolBar();
tbar.Parent = this;
tbar.ImageList = imglst;
tbar.ShowToolTips = true;
tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick);
Menu = new MainMenu();
MenuItem mi = new MenuItem("&Edit");
mi.Popup += new EventHandler(MenuEditOnPopup);
Menu.MenuItems.Add(mi);
miEditCut = new MenuItem("Cu&t");
miEditCut.Click += new EventHandler(MenuEditCutOnClick);
miEditCut.Shortcut = Shortcut.CtrlX;
Menu.MenuItems[0].MenuItems.Add(miEditCut);
tbbCut = new ToolBarButton();
tbbCut.ImageIndex = 4;
tbbCut.ToolTipText = "Cut";
tbbCut.Tag = miEditCut;
tbar.Buttons.Add(tbbCut);
miEditCopy = new MenuItem("&Copy");
miEditCopy.Click += new EventHandler(MenuEditCopyOnClick);
miEditCopy.Shortcut = Shortcut.CtrlC;
Menu.MenuItems[0].MenuItems.Add(miEditCopy);
tbbCopy = new ToolBarButton();
tbbCopy.ImageIndex = 5;
tbbCopy.ToolTipText = "Copy";
tbbCopy.Tag = miEditCopy;
tbar.Buttons.Add(tbbCopy);
miEditPaste = new MenuItem("&Paste");
miEditPaste.Click += new EventHandler(MenuEditPasteOnClick);
miEditPaste.Shortcut = Shortcut.CtrlV;
Menu.MenuItems[0].MenuItems.Add(miEditPaste);
tbbPaste = new ToolBarButton();
tbbPaste.ImageIndex = 6;
tbbPaste.ToolTipText = "Paste";
tbbPaste.Tag = miEditPaste;
tbar.Buttons.Add(tbbPaste);
Timer timer = new Timer();
timer.Interval = 250;
timer.Tick += new EventHandler(TimerOnTick);
timer.Start();
}
void MenuEditOnPopup(object obj, EventArgs ea)
{
miEditCut.Enabled =
miEditCopy.Enabled = (txtbox.SelectionLength > 0);
miEditPaste.Enabled =
Clipboard.GetDataObject().GetDataPresent(typeof(string));
}
void TimerOnTick(object obj, EventArgs ea)
{
tbbCut.Enabled =
tbbCopy.Enabled = (txtbox.SelectionLength) > 0;
tbbPaste.Enabled =
Clipboard.GetDataObject().GetDataPresent(typeof(string));
}
void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea)
{
ToolBarButton tbb = tbbcea.Button;
MenuItem mi = (MenuItem) tbb.Tag;
mi.PerformClick();
}
void MenuEditCutOnClick(object obj, EventArgs ea)
{
txtbox.Cut();
}
void MenuEditCopyOnClick(object obj, EventArgs ea)
{
txtbox.Copy();
}
void MenuEditPasteOnClick(object obj, EventArgs ea)
{
txtbox.Paste();
}
}
Related examples in the same category