Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
C#
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (textBox1.SelectionLength > 0)
        textBox1.Cut();
}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
        if (textBox1.SelectionLength > 0)
            textBox1.Copy();
}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
    {
            textBox1.Paste();
    }
}

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

I make this code but i dont know how to copy, paste and cut text box where is cursor? Not textbox1 like in this code?
Posted
Comments
Sergey Alexandrovich Kryukov 17-Apr-13 15:43pm    
Witch means this:
http://en.wikipedia.org/wiki/Witchcraft

Sorry, I did not mean to make fun of your English or anything like that, I just advise you to use a spell checker. Spell checkers are anywhere, so this is a matter of mere politeness.
And "which text box is cursor" is also unclear. Do you mean which text box has a (usually blinking) cursor indicating where the keyboard input currently goes, also know as "caret"? Or something else?

—SA
Sergey Alexandrovich Kryukov 17-Apr-13 15:47pm    
And you always need to tag the UI library you are using or application type (WPF? Forms? ASP.NET? Silverlight? something else?).
—SA

Please see my comment to the question. I think my assumption is correct. You are probably looking at the text input cursor, also know as caret. The idea is: you need a control having a focus, and the focus always means keyboard focus. Only one control at a time, in the whole system, has a focus, and the input goes there. The focus does not have to be in the top-level windows, but typically it is there. All .NET libraries returns the focus to a previously focused control when a window/form is activated. That's basically all.

The libraries of .NET FCL usually provide class members to check up if some control currently has focus (which can be named Focused, IsFocused), and windows/control has a method to determine if there is a focused control in its hierarchy of children, such as ContainsFoculs. It can also return a control currently owning the focus or virtual focus (a control which may or may not be focused but is supposed to get focus if the form/window is shown and activated), such as ActiveControl.

As you did not properly tag your UI library, you will have to find relevant methods by yourself, but this is really easy, with the use of appropriate MSDN help page.

—SA
 
Share this answer
 
Comments
Maciej Los 17-Apr-13 16:16pm    
It is called intuition!
+5
Sergey Alexandrovich Kryukov 17-Apr-13 16:26pm    
"Intuition is a daughter of information".
Thank you, Мацей (hope this spelling is correct enough, isn't it?) :-)
—SA
Maciej Los 17-Apr-13 16:30pm    
Not correct enough, absolutely proper ;)
Sergey Alexandrovich Kryukov 17-Apr-13 16:40pm    
Pleasure to meet you. :-)
—SA
You could do this using the events GotFocus and LostFocus. Everytime one of your multiple textboxes receives the focus (GotFocus event) set a variable of type TextBox in your Form to the TextBox that just received the GotFocus event. When any other control receives the focus set that same variable to null. This way you'll be able to tell if one of your textboxes was active before the menu strip had been clicked. If anything else beside the textboxes had the focus before the click the variable will be set to null.
In your tool strip menu click events just use the textbox variable instead of TextBox1.

Regards,

— Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Apr-13 16:27pm    
Good points, a 5.
—SA
Maciej Los 17-Apr-13 18:09pm    
Agree, +5!
Sergey Alexandrovich Kryukov 17-Apr-13 18:56pm    
Maciej, Manfred,

Unfortunately, I could not read Maciej's joke and tell my related one: someone removed the thread:
http://www.codeproject.com/Questions/579571/Forumplusinplusasp-netpluscplus-23

This is the second time today when I loose my answer due to removed question. Are we facing a kind of sabotage?

—SA
Maciej Los 17-Apr-13 19:01pm    
Sometimes it happens twice in a day...
Sergey Alexandrovich Kryukov 17-Apr-13 19:07pm    
I believe that. Actually, the post is marked "Closed", not "Deleted", I don't know what it is. Perhaps one of administrators though we're going too much off-topic.
—SA
As Sergey Alexandrovich Kryukov said :

You can use the Control property Focused like that :

C#
foreach (Control item in this.Controls)
            {
                if (item.Focused&&item.GetType()==typeof(TextBox))
                {
                    MessageBox.Show("I'm the textbox focused");
                }
            }


Or for more easy and direct approach with ActiveControl :

C#
if (ActiveControl.GetType() == typeof(TextBox))
            {
                MessageBox.Show(ActiveControl.ToString());
            }


Sorry if you feel that is a repost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900