VS 2005 C# 写PPC程序时,,窗口最上面是TextBox1,下面还有三个Button控件,,运行后光标在TextBox1中,,怎么让光标跳出TextBox,,选择控制其它Button控件呢? 具体操作和代码
注:此窗口是由别的窗口跳过来的,,TextBox1有文章
解决方案 »
- 请问C#调用C的DLL,如何获取通过Marshal.StructureToPtr得到的结构体指针中的元素?
- C#向word中写入文本并加粗问题
- C#中的阻滞Socket与C++的阻滞SOCKET哪个速度更快?
- ASP和C#中互相调用同一个COOKIE
- 抽象接口有什么意义!!
- vs2008里添加新的数据源的时候出现错误 求解决
- 关于控件的问题。
- 为什么往Access数据库插入数据会漏掉一条数据
- 十十十十万火急求助高深技术:c#如何实现三维模型的显示???
- WebForm 中如何在DataGrid中选中一行,并获得此纪录?谢谢!
- 求救,C#+ACCESS用一段时间就出错
- 如何获得ToolStripComboBox控件的值?
获取或设置容器控件上的活动控件。
{
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
// 向上导航
// 向上键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
// 向下导航
// 向下键
this.button1.Focus(); //这句
}
if ((e.KeyCode == System.Windows.Forms.Keys.Left))
{
// 向左键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Right))
{
// 向右键
}
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
// Enter
} }
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button1_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button2_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button3_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
}
public static Control FindFocusedControl(Control container)
{
foreach (Control childControl in container.Controls)
{
if (childControl.Focused)
{
return childControl;
}
} foreach (Control childControl in container.Controls)
{
Control maybeFocusedControl = FindFocusedControl(childControl);
if (maybeFocusedControl != null)
{
return maybeFocusedControl;
}
} return null;
} void setFocus(object sender, KeyEventArgs e)
{ if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{ this.SelectNextControl((Control)sender, true, true, true,true); }
this.textBox1.Text = FindFocusedControl(this).Name;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace DeviceApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button1_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button2_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
} private void button3_KeyDown(object sender, KeyEventArgs e)
{
setFocus(sender, e);
}
public static Control FindFocusedControl(Control container)
{
foreach (Control childControl in container.Controls)
{
if (childControl.Focused)
{
return childControl;
}
} foreach (Control childControl in container.Controls)
{
Control maybeFocusedControl = FindFocusedControl(childControl);
if (maybeFocusedControl != null)
{
return maybeFocusedControl;
}
} return null;
} void setFocus(object sender, KeyEventArgs e)
{ if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{ this.SelectNextControl((Control)sender, true, true, true,true); }
this.textBox1.Text = FindFocusedControl(this).Name;
}
}
}
直接拷贝的话,你还要做几件事
把textBox1的KeyDown的事件指向textBox1_KeyDown
把button1的KeyDown事件指向button1_KeyDown
把button2的KeyDown事件指向button2_KeyDown
把button3的KeyDown事件指向button3_KeyDown
为了让你看清楚当前焦点落在了哪个控件上
所以写了一句 this.textBox1.Text = FindFocusedControl(this).Name;
让textBox1显示当前焦点所在位置你想要的效果,小改动就能实现了
textbox显示的
我那个TextBox1需要一直显示存在相关文章的,,
所以this.textBox1.Text = FindFocusedControl(this).Name;对我来说不是实用
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace DeviceApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_Press(object sender, KeyEventArgs e)
{
Control ctrl = (Control)sender;
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
this.SelectNextControl(ctrl, true, true, true, true);
MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
}
} private void button1_Press(object sender, KeyEventArgs e)
{
Control ctrl = (Control)sender;
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
this.SelectNextControl(ctrl, true, true, true, true);
}
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
this.SelectNextControl(ctrl, false, true, true, true);
}
//按回车要做的事儿
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
}
} private void button2_Press(object sender, KeyEventArgs e)
{
Control ctrl = (Control)sender;
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
this.SelectNextControl(ctrl, true, true, true, true);
}
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
this.SelectNextControl(ctrl, false, true, true, true);
}
//按回车要做的事儿
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
}
} private void button3_Press(object sender, KeyEventArgs e)
{
Control ctrl = (Control)sender;
if ((e.KeyCode == System.Windows.Forms.Keys.Down))
{
this.SelectNextControl(ctrl, true, true, true, true);
}
if ((e.KeyCode == System.Windows.Forms.Keys.Up))
{
this.SelectNextControl(ctrl, false, true, true, true);
}
//按回车要做的事儿
if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
{
MessageBox.Show("当前焦点的控件:" + FindFocusedControl(this).Name);
}
}
public static Control FindFocusedControl(Control container)
{
foreach (Control childControl in container.Controls)
{
if (childControl.Focused)
{
return childControl;
}
}
return null;
}
}
}
出现了错误 “System.Windows.Forms.KeyPressEventArgs”并不包含“KeyCode”的定义
是不是事件指向错还是有什么错误呢?
textBox1指向textBox1_Press事件没错button1要指向button1_Press事件
button2要指向button2_Press事件
button3要指向button3_Press事件
button2事件里的KeyDown指向button2_Press事件
button3事件里的KeyDown指向button3_Press事件
当选到哪个Button控件时,那个控件的背景颜色会改变,