WinForms Forms.Loaded like member - winforms

I have a form
on that form I have a radiobutton
When the radiobutton changes I want to do some stuff.
However I only want to do this if the FormLoad event has fired
and dor some wierd reason the radiobutton changed event is getting hit prior to the FormLoad
Call stack is not much use, but its coming from the settings.designer.cs file
Anyway short of setting a flag on the onLoadEvent is there some intrinsic property of the form like IsLoaded which i can use to make sure that my radio button code only executes once the form is loaded

You can check the IsHandleCreated property of your form to determine if the OnLoad has been called. This is the closest thing to a IsLoaded property.

Here is a different way...
RadioButton has an AutoCheck property which by default is set to true, you want to set this to false in the designer.
And then override it manually in the Form Load event to true like so:
radioButton1.AutoCheck = true;
You can set the CheckedChanged property setup in the designer still and it should work (won't trigger change event).

In the event handler, check to see if the radio button and/or form is visible. If the radio button's state is being changed before the form has been loaded, then RadioButton.Visible should be false.

You may also like to subscribe to the FormShown event. E.g:
private void Form1_Shown(object sender, EventArgs e)
{
radioButton1.Checked = true;
}

You can check that the control has focus or not. If not, then ignore. Eg:
if(radioButton1.ContainsFocus)
{
//Event handling code here...
}

Related

Silverlight Checkbox unchecked event finished

I have a Silverlight 5 Datagrid that has a checkbox in the first column. When the checkbox gets unchecked, I need to fire off an event that changes the value in another cell. The problem I am having is, the checkbox is actually still checked when the Unchecked event fires off so the value in the other cell doesn't change. Is there an event I can wire into that lets me know when the unchecked event is finished? Thanks in advance.
You can try this:
First you 'll increment the unchecked event on code behind like this:
checkBox1.Unchecked +=new RoutedEventHandler(checkBox1_Unchecked2);
So, on this event you can do this
private void checkBox1_Unchecked2(object sender, RoutedEventArgs e)
{
if ((bool)checkBox1.IsChecked)
//Your code
}
The default unchecked event will be fired and then when it reaches the second the checkbox will be already marked as unchecked, then you can do wherever you want.
Hope it helps.

WPF close window when property in ViewModel changes

I was wondering if there was a way to close a window when a property in the view model changes. In my situation I have a login window with an Ok button bound to a LoginCommand so that the function Login executes when Ok is clicked. If the login is successful, I want the window to close.
Now I know I could do this by adding an event handler on my button, which calls a function like this:
private void Button_Click(object sender, RoutedEventArgs e)
{
DatabaseCredentialsViewModel vm = (this.DataContext as DatabaseCredentialsViewModel);
vm.Login();
if (vm.LoginSuccessful)
{
this.Close();
}
}
But I was wondering if there was a way to close the window when LoginSuccessful property changes without having an event handler on my button (I like working only with command binding and not having event handlers on Click event).
Thank you
Here's a similar question, which filled my need.
Basically, you use an attached property for your window, which binds to a bool? property on your VM. When the VM property is set to something non-null, the attached property sets the Window's DialogResult, which will automatically close the window.
If you want you can try this different approach.
You can do this by associating the OK button with a command. Create an event such as LoginSuccess and when then add a window.Close() to the list of event callback. Then you have only to raise the LoginSuccess event to close the windows.
In my opinion, this respect the MVVM pattern defining an event that can be used for other trigger and not only for closing windows.
You could do this fairly easily by creating an attached property or Behavior (from Blend SDK) that hooked into your Window.
I posted a sample behavior to the Expression Code Gallery which does something similar (though definitely different) - it prevents a window from being closed via a property on the VM. You could very easily adapt the code (included in the download) to just close the window on a property change.

unchecked wpf radio buttons

I have two radiobuttons.
One (rb1) is binded to a property of my ViewModel. If the property is true rb1 is checked when the application is loaded. If the property is false rb1 is unchecked (that's right).
But in the last case, both radiobuttons are unchecked, and I need the second radiobutton (rb2) is checked when property is false. How could I do this??
The issue you are encountering is that the DataBinding is "lost". Let me quote Matt Thalman:
The click would change the UI state of
the buttons correctly (for example,
clicking Bar would uncheck Foo and
check Bar). But I noticed that if the
underlying value of IsFoo and IsBar
ever changed after that point, the
buttons would not have their IsChecked
state updated. Using the Snoop tool,
I discovered that the IsChecked state
had had its state set manually after
clicking on one of the buttons. Once
a dependency property has been set
manually, it loses its Binding. This
is why the IsChecked state was not
being changed when the properties
being bound to were updated.
A simple solution is to subclass the RadioButton class:
public class DataBoundRadioButton : RadioButton
{
protected override void OnChecked(RoutedEventArgs e)
{
// Do nothing. This will prevent IsChecked from being manually set and overwriting the binding.
}
protected override void OnToggle()
{
// Do nothing. This will prevent IsChecked from being manually set and overwriting the binding.
}
}
See this blog entry for more details.

How do I hook an event to a control that comes in dynamically as part of a DataTemplate?

I have a ListBox that uses DataTemplateSelector to dynamically decide what
template to use based on the type of the item in the list. I now want to hook
the events that could be fired by controls within the DataTemplate. For example,
if one of the templates has a checkbox in it, I want the application using the
control to be notified when the checkbox is checked. If a different template has
a button within it, I want to be notified when the button is clicked.
Also, since its a ListBox, many of the items could have the same template. So
I will need some kind of RoutedEventArgs so I can walk up from OriginalSource to get
some context information to handle the event.
My solution was to use MouseLeftButtonUp. This works fine for TextBlocks, but it looks like CheckBox and Button controls set handled to true, so the event doesnt bubble up. How can I address these
events so I can assign handlers to them in my calling application?
(Also, Silverlight doesn't actually support DataTemplateSelector, so i followed this example to implement it)
If you are defining the templates in the Xaml for the user control where your event handlers are placed then you should simply be able to assign the event handlers in the Xaml.
However in the specific scenario you outline you can also listen for the MouseLeftButtonUp event via the AddHandler method:-
myListBox.AddHandler(UIElement.MouseLeftButtonUpEvent, myListBox_MouseLeftButtonUp, true);
...
private void myListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
//e.OriginalSource available for your inspection
}
Note by using AddHandler and passing true in the third parameter you will get the event regardless of whether it has been handled.

WPF: Textbox not firing onTextInput event

So basically, I have a bunch of TextBoxes that the user gets to fill out. I've got a button that I want to keep disabled until all the TextBoxes have had text entered in them. Here is a sample XAML TextBox that I'm using:
<TextBox Name="DelayedRecallScore" TextInput="CheckTextBoxFilled" Width="24" />
And here is the function that I'm trying to trigger:
//Disables the OK button until all score textboxes have content
private void CheckTextBoxFilled(object sender, RoutedEventArgs e)
{
/*
foreach (TextBox scorebox in TextBoxList)
{
if (string.IsNullOrEmpty(scorebox.Text))
{
Ok_Button.IsEnabled = false;
return;
}
}
Ok_Button.IsEnabled = true;
*/
MessageBox.Show("THIS MAKES NO SENSE");
}
The MessageBox is not showing up when TextInput should be getting triggered. As an experiment I tried triggering CheckTextBoxFilled() on PreviewTextInput, and it worked fine then, meaning that for whatever reason, the function just isn't getting called. I also have a validation function that is triggered by PreviewTextInput, which works as it should. At first I thought PreviewTextInput might somehow be interfering with TextInput, so I took PreviewTextInput off the TextBox, but that hasn't managed to fix anything. I'm completely befuddled by why this might happen, so any help would be appreciated.
Your handler for the TextInput event is not fired because the TextBox is handling the event. You could try using the TextChanged event instead, since really you just want to know when characters were added or removed from the TextBox.
InitializeComponent();
textbox.AddHandler(TextBox.TextInputEvent,
new TextCompositionEventHandler(TextBox_TextInput_1),
true);
Use "PreviewTextInput" instead, it will work.
Create a new class derived from TextBox. In the new class override the OnTextInput method. Your OnTextInput method will get called before the TextBox gets it.

Resources