I a have simple login form and I have set it's accept button property to "OK" button,
and I have a textbox "username" which I set it's KeyDown event to do some processing.
the Ok has enabled set to false.
btnOk.Enbled = false;
this.AcceptButton = btnOk;
txtUsername.KeyDown += new KeyEventHandler(KeyDownHandle);
when I hit enter in the username textbox I do some processing and then set accept button enabled to true.
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
txtPassword.Focus();
}
}
then I write password in password textbox and hit enter so "OK.Click" is triggered.
but the problem is the keyDown is not working because "accept button".
what can I do to solve this?
Edit: Just want to say the problem is resolved if I set acceptButton to "none" but that's not what I'm looking for.
Why don't you remove this.AcceptButton = btnOk from the constrctor (I suppose) and put it in the KeyDownHandler, that way btnOk will accept Enter key only after it is enabled and after the username has been inserted. so the code should be like this:
btnOk.Enabled = false;
txtUsername.KeyDown += new KeyEventHandler(textBox1_KeyDown);
btnOk.Click += new EventHandler(button1_Click);
private void KeyDownHandle(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// some processing
btnOk.Enabled = true;
this.AcceptButton = btnOk;
txtPassword.Focus();
}
}
Related
i have a Textbox and some RadioButtons in C#.
Now i want to set a Text to a disabled Textbox.
What i tried:
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox1.Text = "****";
TextBox1.Enabled = false;
}
This way i cant see the Text.
If i enable the Textbox, the TextBox shows me the String (****)
What can i do to set a Text to a disabled Textbox?
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox1.Text = "*****";
textBox1.ReadOnly = true;
textBox1.Enabled = false;
this.Invalidate(); //to perform form re-draw
}
You can change the PasswordChar property based on whether or not the textbox is enabled:
TextBox1.PasswordChar = TextBox1.Enabled ? '\0' : '*';
The \0 character will show the contents in plain text.
See this answer for similar results.
I use Enter event in textbox to manage my placeholder but the Enter event fire when my form is show without user action.
How can I fix it ?
Form loginWindow = new Form();
loginWindow.Show();
private void IdTextBox_Enter(object sender, EventArgs e)
{
if (IdTextBox.Text == "Identifiant")
{
IdTextBox.Text = "";
IdTextBox.ForeColor = Color.Black;
}
}
I have winform app that have main form and logging form. When logging form is shown I want it to have focus until it will be closed. I tried:
loggingForm = new LoggingForm();
loggingForm.FormClosing += loggingForm_FormClosing;
loggingForm.bOK.Click += bOK_Click;
loggingForm.Show();
loggingForm.Activate();
loggingForm.Focus();
loggingForm.TopMost = true;
loggingForm.TopMost = false;
void loggingForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (isValidPass)
e.Cancel = false;
else
e.Cancel = true;
}
Try this:
loggingForm.ShowDialog();
I have a textBox that the user should fill it. Default text of the textBox is blank. I want that if the user enter some text in it, buttons will be enable.
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
}
but in this code, if the user enter some text and then erase it, it dosent work. I mean the buttons will be enable ...
how can I do that?
thanks
just do btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text != "")
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
Problem : You don't have any logic to disable the Button.
Solution : You need to add else block to disable the Button.
Suggestion: i would suggest you to use String Method String.IsNullOrEmpty() to check wether your textbox input string is Null or Empty.
if (!String.IsNullOrEmpty(txtEconomic.Text))
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
private void txtLicense_TextChanged(object sender, EventArgs e)
{
if (txtEconomic.Text.Length > 0)
btnInsert.Enabled = true;
else
btnInsert.Enabled = false;
}
The code:
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
OptionsDB.Set_localOnly(checkBox2.Checked);
if (checkBox2.Checked)
{
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
crawlLocaly1.Close();
}
else if (dr == DialogResult.OK)
{
LocalyKeyWords.Add(crawlLocaly1.getText());
crawlLocaly1.Close();
}
removeExt = true;
}
else
{
removeExt = false;
}
}
This line:
OptionsDB.Set_localOnly(checkBox2.Checked);
Save the state of the checkBox2 if its checked or not. If its checked next time i will run my program i will see the V in the checkBox2 checked box. If i will uncheck the checkBox next time i run my program the box of the checkBox2 will be unchecked.
The problem is when i check the checkBox2 once close my program and run it again since the checkBox is checked now then for some reason it will make this:
DialogResult dr = crawlLocaly1.ShowDialog(this);
Wich will open and show the user a new Form.
But i dont want it to be like that.
I want that if the user checked the checkBox when the program is running the new Form will show up. But if the user is running the program from the beginning and the checkBox is checked dont show the new Form just show that the checkBox is checked !
How should i fix it ?
You need an other boolean flag checkedInThisSession which initially set to false, and just set it to true in a checkbox OnChecked handler, then you can check this state easily. Hope all is clear
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
OptionsDB.Set_localOnly(checkBox2.Checked);
// UPDATED
if (checkedInThisSession && checkBox2.Checked)
{
DialogResult dr = crawlLocaly1.ShowDialog(this);
// ...
}
else
{
removeExt = false;
}
// UPDATED
checkedInThisSession = checkBox2.Checked;
}
// In constructor
checkedInThisSession = false;
checkBox2.Checked = OptionsDB.Get_localOnly();
The CheckedChanged event is fired every time the checkbox is set, also programmatically. So to solve this issue you need to ignore the first time the event is fired. So a boolean could be your solution:
private bool ignore = true;
private void checkBox2_CheckedChanged(object sender, EventArgs e){
if(ignore == false){
//your code here
}
else
ignore = false;
}