如何使用 contextmenuStrip 清除文本框文本
How to clear Textbox text using contextmenuStrip
我想知道当我点击contextMenuStrip(1 item named clear)时,有没有办法清除文本框。我有 4 个文本框,我为所有文本框指定了相同的 contextMenuStrip 名称。当 运行 应用程序时,我向 textbox1 添加了一些文本。当我右键单击 textbox1 和 select clear(contextMenuStripItem) 时,所有文本都应该被清除。同样,当我右键单击 textbox2 并 select clear 时,所有文本都应该被清除。我在 winforms VS2010
中编程
提前致谢:)
编辑:我尝试的代码可能不符合正确的命名约定:
在Designer.cs
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.clearToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(100, 26);
//
// textBox1
//
this.textBox1.ContextMenuStrip = this.contextMenuStrip1;
this.textBox1.Location = new System.Drawing.Point(224, 191);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 8;
//
// clearToolStripMenuItem
//
this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
this.clearToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.clearToolStripMenuItem.Text = "clear";
this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
this.Controls.Add(this.textBox1);
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem;
private System.Windows.Forms.TextBox textBox1;
在Form.cs
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripItem menuItem = sender as ToolStripItem;
if (menuItem != null)
{
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
Control sourceControl = owner.SourceControl;
MessageBox.Show(sourceControl.Name);
MessageBox.Show(sourceControl.Text);
}
}
}
所以当我右键单击 textBox1 时,它会显示其文本和名称。所以我的问题是如何清除该文本。对所有文本框使用相同的上下文菜单并清除文本是否足够,或者我必须对不同的文本框使用不同的上下文菜单。
替换
Control sourceControl = owner.SourceControl;
和
TextBox sourceControl = owner.SourceControl as TextBox;
sourceControl.Text = string.Empty;
// or sourceControl.Clear();
我想知道当我点击contextMenuStrip(1 item named clear)时,有没有办法清除文本框。我有 4 个文本框,我为所有文本框指定了相同的 contextMenuStrip 名称。当 运行 应用程序时,我向 textbox1 添加了一些文本。当我右键单击 textbox1 和 select clear(contextMenuStripItem) 时,所有文本都应该被清除。同样,当我右键单击 textbox2 并 select clear 时,所有文本都应该被清除。我在 winforms VS2010
中编程提前致谢:)
编辑:我尝试的代码可能不符合正确的命名约定:
在Designer.cs
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.clearToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(100, 26);
//
// textBox1
//
this.textBox1.ContextMenuStrip = this.contextMenuStrip1;
this.textBox1.Location = new System.Drawing.Point(224, 191);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 8;
//
// clearToolStripMenuItem
//
this.clearToolStripMenuItem.Name = "clearToolStripMenuItem";
this.clearToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.clearToolStripMenuItem.Text = "clear";
this.clearToolStripMenuItem.Click += new System.EventHandler(this.clearToolStripMenuItem_Click);
this.Controls.Add(this.textBox1);
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem clearToolStripMenuItem;
private System.Windows.Forms.TextBox textBox1;
在Form.cs
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripItem menuItem = sender as ToolStripItem;
if (menuItem != null)
{
ContextMenuStrip owner = menuItem.Owner as ContextMenuStrip;
if (owner != null)
{
Control sourceControl = owner.SourceControl;
MessageBox.Show(sourceControl.Name);
MessageBox.Show(sourceControl.Text);
}
}
}
所以当我右键单击 textBox1 时,它会显示其文本和名称。所以我的问题是如何清除该文本。对所有文本框使用相同的上下文菜单并清除文本是否足够,或者我必须对不同的文本框使用不同的上下文菜单。
替换
Control sourceControl = owner.SourceControl;
和
TextBox sourceControl = owner.SourceControl as TextBox;
sourceControl.Text = string.Empty;
// or sourceControl.Clear();