C#中如何判断当前光标在哪个TextBox中

rephy 2011-02-24 02:07:48
C#中如何判断当前光标在哪个TextBox中
如果我有多个TextBox,当我用TAB键移动光标时,我如何判断光标是在哪个TextBox中,
...全文
1377 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjx279742048 2012-10-10
  • 打赏
  • 举报
回复
我找到个办法,判断textBox.Focused,不知道是不是楼主想要的,我也是新手。
Choice 2011-02-25
  • 打赏
  • 举报
回复
我同意27楼的做法,操作的时候直接用_lastFocusTextBox.ID就知道是哪个控件了。
ActiveControl我没测试过不敢说,如果用户点击的是按钮,那ActiveControl还会是输入框吗,应该会变成按钮了吧?未测试过,纯路过好奇问一下。
lh1611 2011-02-25
  • 打赏
  • 举报
回复
this.ActiveControl
rephy 2011-02-25
  • 打赏
  • 举报
回复
不好意思,我是初学C#,对以上理解不了,还望各位赐教!
wangoqoq 2011-02-25
  • 打赏
  • 举报
回复
如果直接通过JS 似乎是很难搞的了
机器人 2011-02-25
  • 打赏
  • 举报
回复
其实你可以判断上一次是哪一个Textbox获得焦点了。

比如我做一个类成员:
// 记录最后一次获得焦点的Textbox
private TextBox _lastFocusTextBox;

然后可以在所有的Textbox的enter事件里写上:
_lastFocusTextBox = (TextBox)sender;

这样在Button_click里,就可以通过_lastFocusTextBox知道,刚刚是在哪一个TextBox呆过了。
  • 打赏
  • 举报
回复
随意添加一个100毫秒启动一次的Timer?

我想凡是处理过那些“花孔雀”式的实习生写代码的PM,都会深恶痛绝这类编程的。
zhaohetian_wq 2011-02-25
  • 打赏
  • 举报
回复
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;
}
}
轮销桂魄@ 2011-02-25
  • 打赏
  • 举报
回复
this.ActiveControl或者用API函数
xixihaha_2011_098 2011-02-25
  • 打赏
  • 举报
回复
难道啊,大部分意见怎么统一。哈哈
xiaotiange 2011-02-25
  • 打赏
  • 举报
回复
需求总是千奇百怪的
g505149841 2011-02-25
  • 打赏
  • 举报
回复
在textbox1和textbox2中分别写一个mousedown事件,然后把选中textbox后要执行的代码写到想对应的事件中。
屹生爱你 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 yalan 的回复:]
引用 12 楼 rephy 的回复:
好像不能用,我是想,当光标在textbox1中时,按BUTTON执行一部分代码,如果在textbox2中时,BUTTON执行另外一部分代码


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

正解~
rephy 2011-02-25
  • 打赏
  • 举报
回复
现在是如何判断textbox是否获得焦点,本人不会写,谢谢
g505149841 2011-02-25
  • 打赏
  • 举报
回复
在KeyDown事件里处理判断testbox控件是否获取焦点
机器人 2011-02-25
  • 打赏
  • 举报
回复
在各自的KeyDown事件里处理吧。
rephy 2011-02-25
  • 打赏
  • 举报
回复
当光标在textbox1中时,按ENTER键执行一部分代码,如果在textbox2中时,按ENTER键执行另外一部分代码,该如何实现呀,谢谢
yalan 2011-02-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 rephy 的回复:]
好像不能用,我是想,当光标在textbox1中时,按BUTTON执行一部分代码,如果在textbox2中时,BUTTON执行另外一部分代码
[/Quote]

哈哈你这样做肯定不行的,你点击button的时候,焦点就到了button上了
rephy 2011-02-24
  • 打赏
  • 举报
回复
好像不能用,我是想,当光标在textbox1中时,按BUTTON执行一部分代码,如果在textbox2中时,BUTTON执行另外一部分代码
加载更多回复(11)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧