我有一个使用c#的Windows应用程序。我创建了一个表格frmMain
它加载当用户登录时,有4个选项像客户创造,供应商创造,员工创造,用户创建维护用户控件的焦点Windows应用程序
该用户工作正常。
问题出现在焦点。当用户加载Customer
用户控件并填充一些数据并让我们假设用户位于第4个控件(Textbox
/Combobox
或任何其他Windows窗体输入控件)时突然点击CreateUser
,然后CreateUser
控制负载,但焦点仍然在用户控制Customer
中的第4个控件。
我想要的是将焦点集中在当前用户控件所在的位置,如果它是新加载的,则将焦点放在默认控件上。
请检查我现在用的是代码,
// this method gets called if the form was opened earlier
private void ShowOpenForm(ControlItem _item)
{
try
{
//Get item from menu
ControlItem _menuI = null;
foreach (ToolStripmenuI menuI in tsmenuWindow.DropDownItems)
{
_menuI = (ControlItem)menuI.Tag;
if (_menuI.Control.Name.ToLower() == _item.Control.Name.ToLower())
{
break;
}
_menuI = null;
}
if (_menuI != null)
{
WmenuI_ClearAllSelection();
for (int index = 0; index < PnlUserCtrl.Controls.Count; index++)
{
Control ctl = PnlUserCtrl.Controls[index];
if (ctl.Name.ToLower() == _menuI.Control.Name.ToLower())
{
ctl.Visible = true;
ctl.BringToFront();
break;
}
}
WmenuI_SetCurrentItemChecked(_menuI);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//User can navigate to opened items from menu as well
private void WindowMenuItem_Click(object sender, EventArgs e)
{
try
{
ControlItem _item = (ControlItem)((ToolStripMenuItem)sender).Tag;
WmenuI_ClearAllSelection();
for (int index = 0; index < PnlUserCtrl.Controls.Count; index++)
{
Control ctl = PnlUserCtrl.Controls[index];
if (ctl.Name.ToLower() == _item.Control.Name.ToLower())
{
ctl.Visible = true;
ctl.BringToFront();
if (ctl is BaseControl)
{
((BaseControl)ctl).SetFocus(); // This sets the focus to default textbox
}
break;
}
}
WmenuI_SetCurrentItemChecked(_item);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
但是这一套专注于默认文本框。不在从用户移动到其他用户控件的位置进行控制。
只需将重点放在用户控件上。由于UserControl从不需要焦点,它会自动将焦点移动到具有最低TabIndex的控件。 –