求助:VS 2005 C# 编写PPC程序时,怎么让光标跳出TextBox,选择控制其它Button控件呢? VS 2005 C# 写PPC程序时,,窗口最上面是TextBox1,下面还有三个Button控件,,运行后光标在TextBox1中,,怎么让光标跳出TextBox,,选择控制其它Button控件呢? 具体操作和代码注:此窗口是由别的窗口跳过来的,,TextBox1有文章 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.ActiveControl = textbox1; 汗。。this.ActiveControl = button1; ContainerControl.ActiveControl 属性获取或设置容器控件上的活动控件。 和winform是不一样的.ppc里用键盘或者指点笔来切换键盘事件里设置,private void Form1_KeyDown(object sender, KeyEventArgs e) { 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 } } 写了个Demo,按回车键循环切换控件,没来得及写注释,希望能看明白~~ 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里,,按模拟器上的中间键和键盘的回车都没有效果 直接拷贝的话,你还要做几件事把textBox1的KeyDown的事件指向textBox1_KeyDown把button1的KeyDown事件指向button1_KeyDown把button2的KeyDown事件指向button2_KeyDown把button3的KeyDown事件指向button3_KeyDown 我将4个事件指向了,,运行后按回车,TextBox1显示了Button1,在按一次显示Button2,依次循环,,,,我想要的是 因为我们运行光标在textbox1里,,,按下回车,使光标跳出来,用上下方向键来选择控件,,按回车会实行控件所写的触发事件,,就像我们用鼠标来选择控件,,点击控件会实行控件的事件一样 之前你可能没开发过ppc,仔细看看,按了enter已经跳出来了为了让你看清楚当前焦点落在了哪个控件上所以写了一句 this.textBox1.Text = FindFocusedControl(this).Name;让textBox1显示当前焦点所在位置你想要的效果,小改动就能实现了textbox显示的 我之前只写过半个记事本的程序,,所以我的C#基础差得一塌糊涂,,所以你的代码我只看懂几句话的功能我那个TextBox1需要一直显示存在相关文章的,,所以this.textBox1.Text = FindFocusedControl(this).Name;对我来说不是实用 写这句this.textBox1.Text = FindFocusedControl(this).Name;只是为了让你看到当前那个按钮被激活,不写也不影响被激活的顺序稍等,改下给你 不好意思,中午有点事儿离开了按上下键切换焦点,按回车键显示当前焦点的messageusing 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_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; } }} 我把textBox1以及Button的KeyPress的事件指向textBox1_Press出现了错误 “System.Windows.Forms.KeyPressEventArgs”并不包含“KeyCode”的定义是不是事件指向错还是有什么错误呢? textBox1指向textBox1_Press事件没错button1要指向button1_Press事件button2要指向button2_Press事件button3要指向button3_Press事件 textBox1事件里的KeyDown指向textBox1_Press事件button1事件里的KeyDown指向button1_Press事件button2事件里的KeyDown指向button2_Press事件button3事件里的KeyDown指向button3_Press事件 你有什么更好的方法可以实现模拟器上方向键选择控制控件以及按下中间键可以执行控件内容呢?当选到哪个Button控件时,那个控件的背景颜色会改变, 新手之windows服务程序 正则表达式,去除嵌套div问题 谁知道c#怎么在关闭一个窗体的同时打开另一个窗体啊? 用C#+winform,开发宿舍管理系统中《水电费管理》板块中遇到一个问题 MatLab 怎么生成C#支持的DLL文件啊? C# 如何实现任务栏高亮(闪烁两次就停止并一直处于高亮状态。) OWC画多条折线 纵坐标数据有问题 高分求HTML和DHTML的区别 如何获取网络上的SMTP/POP3的包并解析? 救教 C# 读取资源中SQL脚本 执行到数据库中 求救,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控件时,那个控件的背景颜色会改变,