c#确定在女巫文本框中是否为光标? [英] c# determine in witch text box is cursor?

查看:76
本文介绍了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?

推荐答案

请参阅我对该问题的评论。我认为我的假设是正确的。您可能正在查看文本输入光标,也称为插入符号。这个想法是:你需要一个具有焦点的控件,焦点总是意味着键盘焦点。在整个系统中,一次只有一个控件具有焦点,输入就在那里。重点不一定是在顶级窗口,但通常它在那里。当窗口/窗体被激活时,所有.NET库都将焦点返回到先前聚焦的控件。这基本上就是全部。



.NET FCL的库通常会提供类成员来检查某些控件当前是否具有焦点(可以命名为聚焦 IsFocused ),并且windows / control有一种方法来确定其子层次结构中是否存在聚焦控制,例如as ContainsFoculs 。它还可以返回当前拥有焦点或虚拟焦点的控件(一个控件可能会聚焦,也可能不会聚焦,但如果显示和激活窗体/窗口则应该聚焦),例如 ActiveControl



由于你没有正确标记你的UI库,你必须自己找到相关的方法,但这很简单,使用适当的MSDN帮助页。



-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


你可以使用事件GotFocus和LostFocus。每当您的多个文本框中的一个接收焦点(GotFocus事件)时,将Form中TextBox类型的变量设置为刚收到GotFocus事件的TextBox。当任何其他控件收到焦点集时,该变量设置为null。通过这种方式,您可以在单击菜单条之前判断其中一个文本框是否处于活动状态。如果文本框旁边的其他内容在点击之前有焦点,则变量将设置为null。

在工具条菜单中单击事件只使用文本框变量而不是TextBox1。



问候,



- Manfred
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


As < b> Sergey Alexandrovich Kryukov 说:



你可以使用控制属性聚焦



As Sergey Alexandrovich Kryukov said :

You can use the Control property Focused like that :

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





或者使用 ActiveControl 进行更简单直接的操作:





Or for more easy and direct approach with ActiveControl :

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





很抱歉,如果您认为这是重新发布。



Sorry if you feel that is a repost.


这篇关于c#确定在女巫文本框中是否为光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆