Button isn't disabling after I click it - c#

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
}

Related

Enable a disabled button in WPF

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.

Simple ON/OFF toggle button with image

I'm working on a WinForms project where I'm trying to create an ON/OFF toggle button that uses two separate images (both located in project resources) for both the "ON" setting, and the "OFF" setting.
Based on what I've found online, I've used a CheckBox with its appearance set to "Button".
Here is the code I've got so far for my button:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_ON);
}
else
{
ToggleButton.BackgroundImage.Equals(Properties.Resources.ToggleButton_OFF);
}
}
For some reason nothing happens when I click on the button, and I'm not sure what I've done wrong here.
Basically, I'd like the background image to cycle back and fourth between ToggleButton_ON and ToggleButton_OFF when the user clicks on the button.
Change your code to:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_ON;
else
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_OFF;
}
The .Equals is for checking equality which you can override in your own classes.

C# checkbox and mouse click event

I am self teaching myself C# and ran into a problem I haven't seem to find an answer too. I have a Form that when I mouse click the check box the state goes to true but also immediately triggers the mouse click event I have code follows:
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
//MouseEventArgs me = (MouseEventArgs) e;
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
I have searched the stack overflow and Google and found similar items but not in C# but no luck on the fixed solution. What I want to do it use the first click to change the check box to true without triggering the mouse click event. I want to delay the event to the 2nd mouse click and not the first.
I have tried the following:
for loop
Clicks == 2 with if statement
subscribing but at a loss on what to use
Instead of the Click event you could subsribe to the CheckedChanged event :
The Handler will look look exactly like yours :
private void uxMouseCopyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
The only difference is that we want the Message box to be shwon only on the second click so when you will uncheck the checkbox.
Be careful though, if you change the default state of the checkbox, it will no longer work.
If you want a really robust solution Grant's one is IMHO the best, mine was just here to show you how to adapt your code for it to work
Just use a boolean variable as a flag.
private bool wasAlreadyClickedOnce;
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
if (!wasAlreadyClickedOnce)
{
wasAlreadyClickedOnce = true;
return;
}
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
Try using the Click event instead of CheckedChanged event to check or uncheck the CheckBox and then you can use the MouseClick event for other stuff.

Programatically set Focus to a TextBox in Windows Phone

In a Windows Phone app I have an TextBox and a Button. The user writes some text to the TextBox and taps the Button, the text from the TextBox is added to a list. The TextBox loses focus after the Button is tapped.
What I want to do is to set the focus back to the TextBox after the Button is tapped so the user can continue writing another text without needing to tap the TextBox.
I tried calling the Focus() method of the TextBox in the Button handler but this does not work. is there another, if any, way to do this?
When Button clicked try to add bollean flag = true. Then check this flag on event OnTextBoxLostFocus.
<TextBox x:Name="tb" Grid.Row="1" LostFocus="Tb_OnLostFocus"/>
<Button x:Name="btn" Click="Btn_OnClick" />
public partial class MainPage : PhoneApplicationPage
{
private bool flag;
public MainPage()
{
InitializeComponent();
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
flag = true;
tb.Focus();
}
private void Tb_OnLostFocus(object sender, RoutedEventArgs e)
{
if (!flag) return;
tb.Focus();
flag = false;
}
}
Hope its help.
I have tried a lot of solutions, but this is the only one that works for me (Windows Phone 8.1 app).
First catch your TextBox's Loaded event, then call Focus(FocusState.Keyboard).
private void myTextBox_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus(FocusState.Keyboard);
}
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
What happens if you call:
yourTextBox.Select(0,0)
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
The way that it worked best for me on the phone was, if I wanted to focus on a particular textbox when the page loaded:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());
}
or if I just wanted it at a certain point. Just repeat these two lines:
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());

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.

Categories