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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Toolbar 
23.38.9.Cut, Copy Paste/Text Box with Toolbar
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.SelectionLength0;
          tbbPaste.Enabled = 
               Clipboard.GetDataObject().GetDataPresent(typeof(string));
     }
     void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea)
     {
          ToolBarButton tbb = tbbcea.Button;
          MenuItem mi = (MenuItemtbb.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.