Csharp/C Sharp by API/System.Windows.Forms/ToolBarButton — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:09, 26 мая 2010
new ToolBarButton
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class ToolBarButtonAction : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem Open;
private System.Windows.Forms.MenuItem Test;
private System.Windows.Forms.MenuItem Exit;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.ruponentModel.IContainer components;
public ToolBarButtonAction()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.Open = new System.Windows.Forms.MenuItem();
this.Test = new System.Windows.Forms.MenuItem();
this.Exit = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(292, 39);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.ImageIndex = 0;
this.toolBarButton1.Text = "Open";
this.toolBarButton1.ToolTipText = "Opens a file";
//
// toolBarButton2
//
this.toolBarButton2.ImageIndex = 1;
this.toolBarButton2.Text = "Test";
this.toolBarButton2.ToolTipText = "Test";
//
// toolBarButton3
//
this.toolBarButton3.ImageIndex = 2;
this.toolBarButton3.Text = "Exit";
this.toolBarButton3.ToolTipText = "Close Program";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.Open,
this.Test,
this.Exit});
this.menuItem1.Text = "Main Menu Options";
//
// Open
//
this.Open.Index = 0;
this.Open.Text = "Open";
this.Open.Click += new System.EventHandler(this.OpenMenuItemClick);
//
// Test
//
this.Test.Index = 1;
this.Test.Text = "Test";
this.Test.Click += new System.EventHandler(this.TestMenuItemClick);
//
// Exit
//
this.Exit.Index = 2;
this.Exit.Text = "Exit";
this.Exit.Click += new System.EventHandler(this.ExitMenuItemClick);
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 32);
this.button1.TabIndex = 1;
this.button1.Text = "Open File Dialog";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(160, 72);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 32);
this.button2.TabIndex = 2;
this.button2.Text = "FontDialog";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(40, 120);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 32);
this.button3.TabIndex = 3;
this.button3.Text = "ColorDialog";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 184);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 20);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "textBox1";
//
// ToolBarButtonAction
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button3,
this.button2,
this.button1,
this.toolBar1});
this.Menu = this.mainMenu1;
this.Name = "ToolBarButtonAction";
this.Text = "ToolBarButtonAction";
this.Load += new System.EventHandler(this.ToolBarButtonAction_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new ToolBarButtonAction());
}
private void ToolBarButtonAction_Load(object sender, System.EventArgs e)
{
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if ( e.Button == toolBarButton1 )
{
MessageBox.Show( "Open Button Clicked ");
}
if ( e.Button == toolBarButton2 )
{
MessageBox.Show( "Test Button Clicked ");
}
if ( e.Button == toolBarButton3 )
{
MessageBox.Show( "Exit Button Clicked ");
}
}
private void TestMenuItemClick(object sender, System.EventArgs e)
{
MessageBox.Show( "Test Menu ItemClicked ");
}
private void OpenMenuItemClick(object sender, System.EventArgs e)
{
MessageBox.Show( "Open Menu ItemClicked ");
}
private void ExitMenuItemClick(object sender, System.EventArgs e)
{
MessageBox.Show( "Exit Menu ItemClicked ");
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog" ;
fdlg.InitialDirectory = @"c:\" ;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ;
fdlg.FilterIndex = 2 ;
fdlg.RestoreDirectory = true ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName ;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
FontDialog fntDlg = new FontDialog();
fntDlg.ShowColor = true;
if(fntDlg.ShowDialog() != DialogResult.Cancel )
{
textBox1.Font = fntDlg.Font ;
textBox1.ForeColor = fntDlg.Color;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
ColorDialog colorDlg = new ColorDialog();
colorDlg.ShowDialog();
textBox1.BackColor = colorDlg.Color;
button1.BackColor = colorDlg.Color;
button3.BackColor = colorDlg.Color;
}
}
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();
}
}