I am programming in C# for Windows Phone 8 and have a Windows.System.Controls.Button object which does not have a "Click" event defined in the XML upon page load. However, after certain pieces of data are obtained, I want to set the button's click event to a specific function I have written.
How can this be done? The "OnClick" event is protected, and I can't find which setting to change in the MSDN site.
from the code you should be able to do something like MyButton.Click += clickhandler, from the XAML you also can do that.
You can potentially change the click event of the button by doing the following:
btButton.Click += NewHandler()
Another way, which isn't as clean would be to have a flag that checks if you have reached a specific stage, declared globally.
bool myBool = false;
Then you can change the bool flag to true and check whether it's true in the click event:
if(myBool)
{
//toDo Logic
}
This way you won't need to create another event handler.
Related
I have done a lot of reading on this and every question I found involves ASP.NET. I'm using Winforms. I have a checkbox (Called CheckboxPicture) on my main form. I want to run a few commands when the state of this checkbox is changed by the user.
This should do it:
public void CheckboxPicture_CheckedChanged(Object sender, EventArgs e)
{
MessageBox.Show("Check State Changed");
}
However checking and unchecking the checkbox dont work. ASP.NET says you need
Autopushback = true but I'm not useing ASP.NET so im not sure where that would go.
A google search for "winforms checkbox event" yields this as its first result:
MSDN: CheckBox.CheckedChanged Event
At some point, they mention:
To run the example code, paste it into a project that contains an instance of type CheckBox named CheckBox1. Then ensure that the event handler is associated with the CheckedChanged event.
(Emphasis mine.)
Unfortunately, they don't show how to "ensure that the event handler is associated with the CheckedChanged event".
In short, somewhere within your code you have to have the following statement:
CheckboxPicture.CheckedChanged += CheckboxPicture_CheckedChanged
In other words, your CheckboxPicture_CheckedChanged() method will not be called by magic, you have to make sure it gets called when the corresponding event of the checkbox is fired.
Go to the form in your designer. Click on the checkbox and look at the properties box. Click on the event handlers and select your handler for the CheckedChanged handler property.
I want to prompt a message to the user Do You want to save the changes?? if he made some changes in any control on the current page before navigation to other page. what is the best way to do this. every control has their events. Like text-box key change event. combo box selectedItemChange event. But a lot of code have to be written in this scenario. I want this modification on each page of the project....
Thanks In Advance...
you'll atleast need to define the event handlers for the controls that can be changed.
Inside the event handlers you can just set a boolean variable to true, and check the value of the variable in OnNavigatedFrom method of the Page, and accordingly show the message.
If you use the Silverlight Navigation framework:
http://msdn.microsoft.com/en-us/library/cc838245(v=vs.95).aspx
There are events that get raised (navigating, navigated, etc...) where you can prompt the user and cancel the navigation.
I'm developing a simple WPF application where I want to use the text changed method for a specific text field. the thing is I implemented the method but, the method gets fired in very short period, like even after i enter one character. I want to check whether the text change is completed in order to go with the methods written in the text changed method.
private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (...) // how to check whether the typing is finished?
{
// code goes here
}
}
I think you'll need the LostFocus event for this. As #SLaks comment says, you can't predict whether the user will press another key. Alternatively, you could databind the control - depending on what you're actually trying to do that may make more sense.
Instead of TextChanged, try monitoring the LostFocus event, which will fire when the user has left the textbox, either through pressing Enter/Tab or clicking somewhere else on the form.
Agree with LostFocus
UIElement.LostFocus Event
Question does not ask about binding but something to consider.
In binding the equivalent is UpdateSourceTrigger="LostFocus"
Binding.UpdateSourceTrigger Property
With binding you can get into more advanced validation UI effects.
How to: Implement Binding Validation
I was wonder how can I fire an event when the user double click on my webbrowser component. Since it has no such event how it could be possible...
Thanks in advance
Sounds like a WPF matter :-)
There you would go with an Behaviour attached to the browser. See this link for more information about this approach if you can alter your application (dependends on what you have done yet).
If you can't apply this solution, just bind a event handler to the click event and count click per time with respect to the mouse movement since the last click and if both conditions are true (two clicks in 0.2 secs, mouse hasn't moved more than 2px, for example) execute your double click code. The events you should use are previewMOUSEdown or MOUSEdown, not KEYdown.
// Call this where you want to create the event (let's say on the form load for example).
webBrowser1.DoubleClick += new EventHandler(webBrowser1_DoubleClick);
// This happens when the event is fired (so when you double click on the webbrowser control).
void webBrowser1_DoubleClick(object sender, EventArgs e)
{
// Code
}
Try this.
I don't know why you can't set this event via the designer :(, but this should work.
I have a check box and I have subscribed for the CheckedChanged event. The handler does some operations in there. I check and uncheck the checkbox programmatically (ex: chkbx_Name.Checked = true), and the CheckedChanged event gets fired.
I want this event to be fired only when I manually check or uncheck it. Is there any way to avoid firing of this event when i check/uncheck it programmatically?
unsubscribe the event before you set:
check1.CheckChanged -= check1_CheckChanged;
then you can programmatically set the value without the checkbox firing its CheckChanged event:
check1.Checked = true;
then re-subscribe:
check1.CheckChanged += check1_CheckChanged;
[EDIT: March 29, 2012]
The problem with Tanvi's approach is you need to catch all source of manual check or uncheck. Not that there's too many(it's only from mouse click and from user pressing spacebar), but you have to consider invoking a refactored event from MouseClick and KeyUp(detecting the spacebar)
It's more neat for a CheckBox(any control for that matter) to be agnostic of the source of user input(keyboard, mouse, etc), so for this I will just make the programmatic setting of CheckBox really programmatic. For example, you can wrap the programmatic setting of the property to an extension method:
static class Helper
{
public static void SetCheckProgrammatically(
this CheckBox c,
EventHandler subscribedEvent, bool b)
{
c.CheckedChanged -= subscribedEvent; // unsubscribe
c.Checked = b;
c.CheckedChanged += subscribedEvent; // subscribe
}
}
Using this approach, your code can respond neatly to both user's mouse input and keyboard input via one event only, i.e. via CheckChanged. No duplication of code, no need to subscribe to multiple events (e.g. keyboard, checking/unchecking the CheckBox by pressing spacebar)
No. Those property change events fire whenever the property value changes, regardless of whether this was done by your code, by the control's own code or databinding. It's all the same code path, usually.
What you can do, however, if your event handler resides in the same class as the code that changes the property value, is to introduce a private boolean field in the class which you use as an indicator of whether the current property change is triggered by your code or by the user. After your change you simply reset it. The event handler would then look at the field and decide of whether it should do anything or not:
class Foo : Form {
private bool checkedProgrammatically = false;
void someMethod() {
// ...
checkedProgrammatically = true;
checkBox1.Checked = true;
checkedProgrammatically = false;
// ...
}
private void checkBox1_CheckChanged(object sender, EventArgs e) {
if (checkedProgrammatically) return;
// ...
}
}
I'm sorry I can't just comment on Michael Buen's answer due to my being new here (no reputation), but for what it's worth I strongly prefer his solution to Johannes Rössel's for a couple of reasons.
1) the checkedProgrammatically variable is a little too close to global for me. There's nothing to stop another method accidentally setting it to true, causing all your events to stop.
2) you could end up with a lot of variables depending on the number of events you're dealing with. It would be easy to change the wrong one and the results can be difficult to debug.
3) it's more obvious what you're doing when you unsubscribe then resubscribe. All the logic is right there, and you don't need to change your event handlers to exit early depending on certain conditions.
I've used both methods extensively and I find Michael's a lot easier in the long run.
You can use the MouseClick event and in that check for the checked state of the checkbox.
This way it wont be triggered programatically, it would only be called when the user manually checks or unchecks the checkbox.
You can set boolean variable before changing value programiticaly, and check than reset that variable in checkedchanged event