在 c# windows 表单应用程序上获得焦点或 Select 文本框
Focus or Select text box on c# windows forms application
我正在制作一个 windows 表单应用程序,在应用程序启动时,光标位于错误的文本框中。
我试过在线搜索其他一些问题,但似乎没有任何效果。
我试过了 inputBox.Focus(); bot 在 Initialize Component 之后,我也在我的输入框方法中尝试过这个,我试过 inputBox.Select();在一些地方也是如此。好像没什么区别。
我也看到可以将文本框的tab索引设置为零,可惜我不懂。我在 visual studio 的任何地方都找不到这个选项。我认为这将在设计器中文本框的属性中。我找错地方了吗?或者我应该寻找不同的解决方案?
这是我的代码:
namespace Project_9
{
public partial class Form1 : Form
{
const int MAX = 10;
Bowling objectRef;
public Form1()
{
InitializeComponent();
objectRef = new Bowling(10);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("Jonathan Spalding\nCS1400\nProject 9");
}
private void inputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
string text = inputBox.Text;
if (text == "")
{
highScoreBox.Text = objectRef.GetHighScorePlayer() + ": " + string.Format("{0:d}", objectRef.GetHighScore());
lowScoreBox.Text = objectRef.GetLowScorePlayer() + ": " + string.Format("{0:d}", objectRef.GetLowScore());
averageScoreBox.Text = string.Format("{0:f2}", objectRef.GetAverageScore());
}
else
{
inputBox.Clear();
objectRef.AddPlayer(text);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
inputBox.Focus();
}
}
}
在加载事件中
ActiveControl = inputBox;
您可以将TabIndex
属性设置为大于0的值。值最小的控件将专注于启动。
然后您可以在其他控件上设置 TabIndex,这样当您按下 Tab 键时,下一个控件(具有更高 TabIndex)将获得焦点。
Select 文本框。在属性中 window 找到 TabIndex(如果找不到,请按照红色小矩形中的 A-Z 对属性进行排序。
确保:
1. TabIndex 是与其他控件相比的最小值(在我的示例中,两个组合框每个都有一个 TabIndex 值)
2.TabStop值为True。
在您的表单构造函数中,您可以使用:
public Form1()
{
InitializeComponent();
inputBox.Focus();
}
或者
public Form1()
{
InitializeComponent();
inputBox.Focus();
inputBox.Select();
}
我认为最好的方法是通过在控件上单击鼠标来设置 TabIndex。只需转到查看菜单,然后转到 TabIndexOrder 菜单选项。
然后,您只需按照您想要的方式单击表单上表示的 TabIndex 即可设置 TabIndex 顺序。
当您再次完成 select TabIndexOrder 菜单选项以退出助手时。
我正在制作一个 windows 表单应用程序,在应用程序启动时,光标位于错误的文本框中。
我试过在线搜索其他一些问题,但似乎没有任何效果。
我试过了 inputBox.Focus(); bot 在 Initialize Component 之后,我也在我的输入框方法中尝试过这个,我试过 inputBox.Select();在一些地方也是如此。好像没什么区别。
我也看到可以将文本框的tab索引设置为零,可惜我不懂。我在 visual studio 的任何地方都找不到这个选项。我认为这将在设计器中文本框的属性中。我找错地方了吗?或者我应该寻找不同的解决方案?
这是我的代码:
namespace Project_9
{
public partial class Form1 : Form
{
const int MAX = 10;
Bowling objectRef;
public Form1()
{
InitializeComponent();
objectRef = new Bowling(10);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
MessageBox.Show("Jonathan Spalding\nCS1400\nProject 9");
}
private void inputBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
string text = inputBox.Text;
if (text == "")
{
highScoreBox.Text = objectRef.GetHighScorePlayer() + ": " + string.Format("{0:d}", objectRef.GetHighScore());
lowScoreBox.Text = objectRef.GetLowScorePlayer() + ": " + string.Format("{0:d}", objectRef.GetLowScore());
averageScoreBox.Text = string.Format("{0:f2}", objectRef.GetAverageScore());
}
else
{
inputBox.Clear();
objectRef.AddPlayer(text);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
inputBox.Focus();
}
}
}
在加载事件中
ActiveControl = inputBox;
您可以将TabIndex
属性设置为大于0的值。值最小的控件将专注于启动。
然后您可以在其他控件上设置 TabIndex,这样当您按下 Tab 键时,下一个控件(具有更高 TabIndex)将获得焦点。
Select 文本框。在属性中 window 找到 TabIndex(如果找不到,请按照红色小矩形中的 A-Z 对属性进行排序。
确保: 1. TabIndex 是与其他控件相比的最小值(在我的示例中,两个组合框每个都有一个 TabIndex 值) 2.TabStop值为True。
在您的表单构造函数中,您可以使用:
public Form1()
{
InitializeComponent();
inputBox.Focus();
}
或者
public Form1()
{
InitializeComponent();
inputBox.Focus();
inputBox.Select();
}
我认为最好的方法是通过在控件上单击鼠标来设置 TabIndex。只需转到查看菜单,然后转到 TabIndexOrder 菜单选项。 然后,您只需按照您想要的方式单击表单上表示的 TabIndex 即可设置 TabIndex 顺序。 当您再次完成 select TabIndexOrder 菜单选项以退出助手时。