将用户输入锁定到控件中
本文关键字:控件 锁定 用户 输入 | 更新日期: 2023-09-27 17:48:57
好吧,这是我昨天问的一个问题的一个分支,但我觉得它值得单独发布。我创建了这个控件:
public partial class LinkLabelTextBoxPlayerName : UserControl
{
public LinkLabelTextBoxPlayerName()
{
InitializeComponent();
this.textBox.Hide();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.linkLabel.Hide();
this.textBox.Show();
this.textBox.Focus();
this.textBox.KeyPress += new KeyPressEventHandler(textBoxPlayerName_KeyPress);
this.textBox.LostFocus += new EventHandler(textBoxPlayerName_LostFocus);
}
private void textBoxPlayerName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && !(String.IsNullOrEmpty(this.textBox.Text)))
{
this.linkLabel.Text = this.textBox.Text;
this.textBox.Hide();
this.linkLabel.Show();
}
}
private void textBoxPlayerName_LostFocus(object sender, EventArgs e)
{
if (!(String.IsNullOrEmpty(this.textBox.Text)))
{
this.linkLabel.Text = this.textBox.Text;
this.textBox.Hide();
this.linkLabel.Show();
}
else
{
this.textBox.Focus();
}
}
}
它是一个LinkLabel==>文本框控件,它的工作大约95%,这里是问题,当用户输入点击LinkLabel,并把它变成一个文本框,我希望它"锁定"用户输入到只有文本框,否则,你可以继续点击LinkLabel,激活更多的文本框。我只是想知道是否有一种方法来禁用用户输入,而文本框是活跃的。谢谢你的帮助。
i change part of method from
{
this.textbox.Focus();
}
{
this.textBox.Hide();
this.linkLabel.Text = "<click to add player>"; //my orginal link label text;
this.linkLabel.Show();
}
所以这似乎是工作
你可以试着添加一个静态的"Who has focus"变量,当你把焦点给一个你拿旧的静态变量,让它显示标签。
。
private static LinkLabelTextBoxPlayerName _curSelected;
...
if(_curSelected != null)
{
_curSelected.Blur();
}
_curSelected = this;
这是假设你想一次只打开一个文本框,这不是什么工作。
我将希望锁定用户的字段的CausesValidation
属性设置为true。
然后,在Validating
事件处理程序中,我将使用;
private void control_Validating(object sender, CancelEventArgs e)
{
if( ! allowUserToLeaveControl )
e.Cancel = true;
}
希望这对你有用!
/
很抱歉我问了这个问题,我应该多花一点时间,这里的代码工作得很好,
public partial class LinkLabelTextBoxPlayerName : UserControl
{
public LinkLabelTextBoxPlayerName()
{
InitializeComponent();
this.textBox.Hide();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.linkLabel.Hide();
this.textBox.Show();
this.textBox.Focus();
this.textBox.KeyPress += new KeyPressEventHandler(textBoxPlayerName_KeyPress);
this.textBox.LostFocus += new EventHandler(textBoxPlayerName_LostFocus);
}
private void textBoxPlayerName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && String.IsNullOrEmpty(this.textBox.Text.Trim()))
{
this.textBox.Hide();
this.linkLabel.Text = "<click to add player>"; //orignal text;
this.linkLabel.Show();
}
else if (e.KeyChar == (char)Keys.Enter)
{
this.textBox.Hide();
this.linkLabel.Text = this.textBox.Text;
this.linkLabel.Show();
}
}
private void textBoxPlayerName_LostFocus(object sender, EventArgs e)
{
if (!(String.IsNullOrEmpty(this.textBox.Text.Trim())))
{
this.textBox.Hide();
this.linkLabel.Text = this.textBox.Text;
this.linkLabel.Show();
}
else
{
this.textBox.Hide();
this.linkLabel.Text = "<click to add player>"; //orginal text;
this.linkLabel.Show();
}
}
}