lock user input into a control
ok, this is an off-shoot of a question i asked ye开发者_开发问答sterday, but i felt it deserved it's own post. i made this control:
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();
}
}
}
it is a LinkLabel==>Textbox control, and it works about 95%, here is the problem, when user input cliks the linklabel, and turns it into a textbox, i want it to "lock" user input to only the textbox, otherwise, you can keep clicking linklabels, activatin more textboxes. im just wondering if there is a way to disable user input while the textbox is active. Thanks for any help.
i changed part of the method from
{
this.textbox.Focus();
}
to
{
this.textBox.Hide();
this.linkLabel.Text = "<click to add player>"; //my orginal link label text;
this.linkLabel.Show();
}
so this seems to be working.
You could try and add a static "Who has focus" variable, and when you give focus to one you take the old static var and make it show the label.
e.g.
private static LinkLabelTextBoxPlayerName _curSelected;
...
if(_curSelected != null)
{
_curSelected.Blur();
}
_curSelected = this;
This is assuming that you want only one Textbox open at a time and that isn't what is working.
I would set the CausesValidation
property to true for the field you wish to lock user into.
Then, in the Validating
event handler, I'd use;
private void control_Validating(object sender, CancelEventArgs e)
{
if( ! allowUserToLeaveControl )
e.Cancel = true;
}
Hope this works for you!
/A
im sorry i asked this, i should have just put a little more time into it, here is code that works fine,
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();
}
}
}
精彩评论