Enable a disabled button in WPF - c#

So, I have a button that 's disabled by default on form load and I'd like to enable it when all conditions evaluate to TRUE.
private void OnFormLoad(object sender, RoutedEventArgs e)
{
BtnInvoice.IsEnabled = false;
}
private void Btn_Click(object sender, RoutedEventArgs e)
{
if (Regex.IsMatch(A.Text, "^[a-zA-Z' ]*$") && Regex.IsMatch(B.Text, "^[0-9.]*$") && Regex.IsMatch(C.Text, "^[0-9.]*$"))
{
BtnInvoice.IsEnabled = true;
}
In the above code, when all conditions evaluate to TRUE, my button remains disabled.
I'm pretty sure I put the enabling block in the wrong place may also have a logic error.

In situations that I must determine when a button needs to be enabled or not, I prefer implement a Command on that button. The CanExecute on the command holds the logic whether the button is actually enabled or not.
See http://www.wpf-tutorial.com/commands/using-commands.

Related

Button isn't disabling after I click it

I have created a button that resets a textbox value to the default value, as shown:
<Button x:Name="DefaultButton"
Grid.Row="0"
Grid.Column="3"
Click="OnDefaultClicked">
Here is the Click method:
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
displayedData = defaultData;
//rest of method code
}
When I click the button, the data resets to its default value automatically, but the button does not get disabled until I click it a second time. I am not sure why this is happening.
I could implement the IsEnabled property in the xaml code and bind it to a method that determines whether the button should be enabled based on the value of displayedData, but since the button is not re-enabled/disabled anywhere else in my application or used for any other purpose, this seems kind of like overkill... as far as I know, the Click event should be able to handle this alone. Regardless, my main problem is that I just don't understand why the button wouldn't get disabled until the 2nd click since the OnDefaultClicked method explicitly states it should be disabled when clicked.
Am I missing something?
Turns out the problem was in the rest of my code. The snippet posted above worked perfectly fine. I realized I had created a method that re-enables the button when the text in the box changes, to allow the user to again reset it to default after making a change. So when the default button is clicked, the text in the box changes and therefore both the OnTextChanged and OnDefaultClicked methods are triggered, which causes simultaneous enabling and disabling of the button.
Here's how I fixed it:
private bool DefaultClicked;
private void OnDefaultClicked(object sender, RoutedEventArgs e)
{
DefaultButton.IsEnabled = false;
DefaultClicked = true;
displayedData = defaultData;
//rest of method code
}
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if(!DefaultClicked)
{
DefaultButton.IsEnabled = true;
}
DefaultClicked = false;
//rest of method code
}

How to validate textbox in C# WF?

I have two text boxes in windows form.
Also one disabled button.
How I can do validation text box:
if field is empty then disable button
if value inside field is less then 5 then disable button
other case - enable button
I tried this on event TextChange, but when I tried to enter value 43 I get notification, because event TextChange works after each typing symbols.
Code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox2.Text))
{
button6.Enabled = true;
}
}
If you don't want to validate each time a key is pressed but would rather validate when the user leaves the field, instead of hooking into the TextChanged event, hook into the Leave event.
private void textBox2_Leave(object sender, EventArgs e)
{
button6.Enabled = !(string.IsNullOrEmpty(textBox2.Text)) && textBox2.Text.Length >= 5;
if (!button6.Enabled)
{
textBox2.Focus();
}
}
Update your event handle like this :
private void textBox2_TextChanged(object sender, EventArgs e)
{
button6.Enabled =
!String.IsNullOrEmpty(textBox2.Text) && textBox2.Text.Length > 5
}
As for disabling the button on start up, you just set button6 to be disabled by default.
Or, invoke your validation in your constructor :
textBox2_TextChanged(null, null);
Neither TextChanged nor Leave events are appropriate for this. The proper event is called (surprise:-) Validating. You need to set e.Cancel = true if validation is wrong. More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

Setting myButton.Enabled = true causes radiobutton's CheckedChanged handler to fire

I have absolutely no programmatic links or properties set such that my CheckedChanged fires as a result of anything except checking the radio button.
However, when I click a different, unrelated button, the button's click handler fires (this is expected). In this click handler, the button disables itself (it re-enables on a different button's click), which then triggers myRadioButton_CheckedChanged handle for an unrelated radiobutton fires.
The call stack that I'm seeing is essentially
myRadioButton_CheckedChanged (...)
myButton_Click(...)
Main(...)
The line in myButton_Click that is triggering the myRadioButton_CheckedChanged is apparently
myButton.Enabled = false;
The related code is:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// L-R
if (radioButton1.Checked == true)
{
orientation_left = 3;
pictureBox2.Invalidate();
Debug.Print("left {0}", orientation_left);
}
}
private void select1_Click(object sender, EventArgs e)
{
Debug.Print("select1click");
if (select1Down == false)
{
// ... stuff
select1.Enabled = false; // Causing the CheckedChanged to fire
select2.Enabled = false;
select1Down = true;
}
}
Ok, got it.
Check the TabOrder on your Button and RadioButton.
Seems that when you disable the Button, the focus is shifted to the next control, which is probably your RadioButton, causing it to become checked.
On my test From, all I had to do was to make sure that the RadioButton's TabOrder was not right after the Button.
Cheers
EDIT:
This seems to be a known problem as I just found this MSDN thread: http://social.msdn.microsoft.com/Forums/windows/en-US/77fbec3b-1f63-42e1-a200-19b261b63794/the-radiobutton-clicked-event-is-fired-without-the-radio-button-beeing-clicked-?forum=winforms
Okay, it's kinda hacky but it works without changing anything to the tab order:
private void select1_Click(object sender, EventArgs e)
{
if (!select1Down)
{
// ... stuff
SendKeys.SendWait("{Tab}");
select1.Enabled = false;
select2.Enabled = false;
select1Down = true;
}
}

Check Box changes

I have a check box, which when it is checked, fires a call to a web service.
This works fine as it is using a toggle function in a database which is updating as expected.
However my problem being I need the toggle function to be activated when a user unchecks the checkbox.
For some reason this does not seem to fire the toggle function in the database. I am using the following code -
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
if (checkCounter1 == 0)
{
}
else
{
//WebService call
}
checkCounter1 = 1;
}
I tried the checkBox_Changed event however this did not work. How can I do this?
Since SL is more or less WPF. That's why i think in SL just like WPF there should be Checked and UnChecked event.
Assign Single event code to both these events (Since both take same arguments) like this
private void checkBox1_Checked_Unchecked(object sender, RoutedEventArgs e)
{
if (checkBox1.IsChecked)
{
//WebService call
}
else
{
}
}
Try the CheckBox1.IsChecked or CheckBox1.Checked property of the checkbox and see if they are checked in the event functions.

How can I disable node renaming for the TreeView in WinForms?

Is it possible to disable the option to get into "Rename" mode when clicking on a tree-node?
I don't want to disable renaming completely, only to not allow doing it by clicking on the node.
I don't know why would you change the default behavior, but anyway here's a possible solution to edit the nodes with LabelEdit set to true.
Just catch BeforeLabelEdit event and cancel it, unless your specific action occurred. The following code does this for F2 key press:
bool _allowNodeRenaming;
private void treeView1_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
{
if (!_allowNodeRenaming)
{
e.CancelEdit = true;
}
_allowNodeRenaming = false;
}
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F2)
{
_allowNodeRenaming = true;
treeView1.SelectedNode.BeginEdit();
}
}
You'll have to turn the LabelEdit property on and off as needed:
private void startLabelEdit() {
treeView1.LabelEdit = true;
treeView1.SelectedNode.BeginEdit();
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
treeView1.LabelEdit = false;
}
Beware that this has side effects, the LabelEdit property is a style flag for the native Windows control. Changing it requires completely destroying the window and re-creating it from scratch. The most visible side-effect is a small flicker when the window redraws itself after getting created. There could be other ones, I didn't see anything go wrong myself.

Categories