C#中如何判断当前光标在哪个TextBox中
如果我有多个TextBox,当我用TAB键移动光标时,我如何判断光标是在哪个TextBox中,

解决方案 »

  1.   

    正确答案:this.ActiveControl.Text
      

  2.   

    用眼看!
    不要用this.ActiveControl哦
    一般人我不告诉他!
      

  3.   

    好像不能用,我是想,当光标在textbox1中时,按BUTTON执行一部分代码,如果在textbox2中时,BUTTON执行另外一部分代码
      

  4.   


    哈哈你这样做肯定不行的,你点击button的时候,焦点就到了button上了
      

  5.   

    当光标在textbox1中时,按ENTER键执行一部分代码,如果在textbox2中时,按ENTER键执行另外一部分代码,该如何实现呀,谢谢
      

  6.   

    在各自的KeyDown事件里处理吧。
      

  7.   

    在KeyDown事件里处理判断testbox控件是否获取焦点
      

  8.   

    现在是如何判断textbox是否获得焦点,本人不会写,谢谢
      

  9.   

    在textbox1和textbox2中分别写一个mousedown事件,然后把选中textbox后要执行的代码写到想对应的事件中。
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace Key
    {
        public partial class Form1 : Form
        {        IntPtr ptr = (IntPtr)null;
            [DllImport("user32.dll")]        public static extern IntPtr GetForegroundWindow();        [DllImport("user32.dll", CharSet = CharSet.Auto)]        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);        [DllImport("user32.dll")]        static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);        [DllImport("user32.dll")]        static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);        [DllImport("user32.dll")]
            static extern IntPtr SetFocus(IntPtr hWnd);        [StructLayout(LayoutKind.Sequential)]        public struct GUITHREADINFO        {            public int cbSize;            public int flags;            public IntPtr hwndActive;            public IntPtr hwndFocus;            public IntPtr hwndCapture;            public IntPtr hwndMenuOwner;            public IntPtr hwndMoveSize;            public IntPtr hwndCaret;            public RECT rectCaret;        }
            bool Find = false;
            public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)        {            if (hwnd != IntPtr.Zero)            {
                    uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);                GUITHREADINFO guiThreadInfo = new GUITHREADINFO();                guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);                if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)                    return null;                return guiThreadInfo;            }            return null;        }
            public Form1()
            {
                InitializeComponent();
                
            }        private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 100;
                timer1.Tick += timer1_Tick;
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (Find == false)
                {
                    IntPtr hwnd = GetForegroundWindow();                GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);                if (guiInfo != null)
                    {                    ptr = (IntPtr)guiInfo.Value.hwndCaret;                    if (ptr != IntPtr.Zero && ptr != this.textBox1.Handle)
                        {
                            this.textBox1.Focus();
                            this.Visible = true;
                            Find = true;
                            
                            SetFocus(this.Handle);
                            textBox1.Text = "";
                        }                }
                }
            }        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
               
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.Visible = false;
                SetFocus(this.Handle);            string text = textBox1.Text;
                for (int i = 0; i < text.Length; i++)
                {
                    SendMessage(ptr, 0x0102, (IntPtr)(int)text[i], IntPtr.Zero);            }
                Find = false;
      
            }
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }
    }
      

  11.   

    可以参考msdn:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.gotfocus.aspx以及:http://zhidao.baidu.com/question/20159474.html
      

  12.   

    随意添加一个100毫秒启动一次的Timer?我想凡是处理过那些“花孔雀”式的实习生写代码的PM,都会深恶痛绝这类编程的。
      

  13.   

    其实你可以判断上一次是哪一个Textbox获得焦点了。比如我做一个类成员:
    // 记录最后一次获得焦点的Textbox
    private TextBox _lastFocusTextBox;
    然后可以在所有的Textbox的enter事件里写上:
    _lastFocusTextBox = (TextBox)sender;
    这样在Button_click里,就可以通过_lastFocusTextBox知道,刚刚是在哪一个TextBox呆过了。
      

  14.   

    我同意27楼的做法,操作的时候直接用_lastFocusTextBox.ID就知道是哪个控件了。
    ActiveControl我没测试过不敢说,如果用户点击的是按钮,那ActiveControl还会是输入框吗,应该会变成按钮了吧?未测试过,纯路过好奇问一下。
      

  15.   

    我找到个办法,判断textBox.Focused,不知道是不是楼主想要的,我也是新手。