Cut, Copy Paste/Text Box with Toolbar : Toolbar « GUI Windows Forms « C# / CSharp Tutorial






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();
     }
}








23.38.Toolbar
23.38.1.Toolbar with ComboBoxToolbar with ComboBox
23.38.2.ToolBar Linked With MenuToolBar Linked With Menu
23.38.3.ToolBar Button ActionToolBar Button Action
23.38.4.Add Button action to ToolBar ButtonAdd Button action to ToolBar Button
23.38.5.Add ToolTip to ToolBar ButtonAdd ToolTip to ToolBar Button
23.38.6.Create an ImageList and use it for ToolBarCreate an ImageList and use it for ToolBar
23.38.7.Add Image to ToolBar ButtonAdd Image to ToolBar Button
23.38.8.ToolBar Button border style: BorderStyle.Fixed3DToolBar Button border style: BorderStyle.Fixed3D
23.38.9.Cut, Copy Paste/Text Box with Toolbar
23.38.10.Designed Toolbar
23.38.11.ToolBar Button Style
23.38.12.Toolbar Styles