0
votes

I want to know whether there is any way to clear the textbox when I click on contextMenuStrip(1 item named clear). I have 4 textboxes, which i have given same contextMenuStrip name to all textboxes. when running the application, I added some text to textbox1. when I right click on textbox1 and select clear(contextMenuStripItem), all the text should be cleared. Similarly when I right click on textbox2 and select clear, all the text should be cleared. I am programming in winforms VS2010

Thanks in advance :)

EDIT: The code which I tried may not in correct naming convention:

In 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;

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

    }

so when I right click on textBox1, it is displaying its text and name. So my question is how to clear that text. Is it suffecient to have same contextMenu for all textboxes and clear the text or I have to use different contextMenu's for different textBoxes.

1
Have you got a context opening on the right click event already? or do you need the code for this too?Simon Price
@X-TECH I am able to access the textbox name which I clicked and its text, but I am not able to clear it. :(Gopi
@SimonPrice I got context opening on right click and able to access data but unable to clear textbox textGopi
@X-TECH I clearly mentioned I used same contextmenu for 4 textboxes so i cannot directly write code textBox1.Clear(); as at runtime we are not sure it is textbox1.Gopi

1 Answers

4
votes

Replace

Control sourceControl = owner.SourceControl;

with

TextBox sourceControl = owner.SourceControl as TextBox;
sourceControl.Text = string.Empty;    
// or   sourceControl.Clear();