get Current textBox

in c# how can i get the current textBox control,, i was thinking something like this:

Control control;

Type type = control.GetType();
            if (type == typeof(TextBox))
            {
                MessageBox.Show(control.Text);
            }

but that dont work.. and i need to get the selected textbox...

this is because im making a contextmenu with optiosn like copy , paste erase,,, undo bla bla... actually it allready works.. but just with textBox1 xDDD... so i want to use the same engine i got working with a variable of the selected textbox... heres an example of an item of the contextmenu:

private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (textBox1.CanUndo == true)
            {
                textBox1.Undo();
                textBox1.ClearUndo();
            }
        }

as u can see it clears the textBox1... i want to change that for a variable.... of the selected textbox... thanks.


Answer this question

get Current textBox

  • Sam13

    I'm not sure that I quite follow what you want to do but assuming that, given a form, you want to get the currently active control you can use Form.ActiveControl.  This gives you the current control but there is no guarantee that it is a TextBox.  You can do the following to figure that out.

    //Inside a method of your Form class
    if (ActiveControl is TextBoxBase)
    {
       TextBoxBase box = (TextBoxBase)ActiveControl;
        if (box.CanUndo)
           ...
    };

    However implementing undo in this manner limits you to only undoing changes to text boxes.  Also note that it doesn't necessarily work the way you want because if a user makes a change in a text box, clicks in another text box and then clicks your menu item then it'll undo in the wrong control.  I'd recommend trapping the change event for each text box.  Whenever the text changes push the affected text box and the original text to the stack variable inside your form.  Whenever the undo option is selected pop the stack and, using the text box and text that was stored, simply apply the previous text.  The other issue with TextBoxBase's version is it only supports a single undo level (which may be what you wanted).

    struct UndoData
    {
       public TextBoxBase Box;
       public string Text;

       public UndoData ( TextBoxBase box, string text ) { ... }
    }

    //In your form or, even better, a separate class
    private Stack<UndoData> m_UndoStack = ...;

    //In your form and hooked to the KeyDown event of any text boxes
    private void OnTextChanged ( object sender, KeyEventArgs e)
    {
       TextBoxBase box = (TextBoxBase)sender;

       //Push the object and the change being made
       m_UndoStack.Push(new UndoData(box, box.Text));
    }

    //Your menu handler changes to...
    private void undoToolStripMenuItem1_Click (object sender, EventArgs e)
    {
       if (m_UndoStack.Count > 0)
       {
          UndoData data = m_UndoStack.Pop();
          data.Box.Text = data.Text;
       };
    }

    Of course there are some boundary cases to handle like pasting text and consolidating multiple key presses into a single undo event but this gives you the general idea.

    Michael Taylor - 12/6/05

  • get Current textBox