I have a Page_Load event which contains the code I want executing every time the user opens the application. However, when a user clicks the back button on their Windows device, the application is still open, so when they go onto the application the Page_Load event is not called.
I've also tried an OnNavigatedTo event:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
InitialStartOfApp();
}
But the InitialStartOfApp() doesn't get called. I know it doesn't get called because I try debugging the line, however it isn't executed.
Does anyone know any events that would resolve this or any ideas?
The Page_Load event is this:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
InitialStartOfApp();
}
You need handle the Application.Resuming event that is raised when the apps continues after a previous suspension.
Application app = Application.Current;
app.Resuming += App_Resuming;
...
private void App_Resuming(object sender, object e) {
InitialStartOfApp();
}
Related
I have a TextBox and I want all the text inside of it to be highlighted when the user clicks on it (so that they can replace it easily). I have the following event handler linked up the the TextBox:
private void TextBox_Enter(object sender, EventArgs e) {
SelectAll();
}
When I click on the TextBox, the text is only selected for a fraction of a second (sometime it's so fast I can't see it at all) and then it goes back to being a cursor. Does anyone know how to fix this or if there are any relatively simple workarounds?
I tried the same thing with the TextBox.MouseClick event (and it highlighted the text), but because it was the MouseClick event the text was highlighted every time I clicked the TextBox (even when the TextBox already had focus).
I have also tried SelectionStart = 0; SelectionLength = Text.Length, but the same thing happens. This leads me be believe the issue has something to do with the event.
I also tried the TextBox.GotFocus event and had the exact same problem.
I am doing this in a Windows Form application.
The reason why you didn't see the text getting selected is that the TextBox is busy when one of those events occurred (e.g., caret positioning). You actually select the text, but then the internal event handlers of the TextBox execute and remove the selection e.g. by setting the caret position.
All you have to do is to wait until the internal event handlers have completed.
You do this by using the Dispatcher. When you invoke the Dispatcher asynchronously the delegate is not immediately executed but enqueued and executed once all previously enqueued actions (like the internal event handlers) are cleared from the dispatcher queue.
So going with the TextBox.GotFocus event in WPF (or the TextBox.Enter in WinForms) and the asynchronous Dispatcher will do the trick:
WPF
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
await Application.Current.Dispatcher.InvokeAsync((sender as TextBox).SelectAll);
}
WinForms
private void SelectAll_OnTextBoxEnter(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.BeginInvoke(new Action(textBox.SelectAll));
}
Thankfully I found a solution! It turns out that the Click event is executed before the Enter event, this allowed me to set up a JustGotFocus variable and do the following:
private void myTextBox_Click(object sender, EventArgs e) {
this.JustGotFocus = true;
if (JustGotFocus) {
myTextBox.SelectAll();
}
}
private void myTextBox_Enter(object sender, EventArgs e) {
JustGotFocus = false;
}
If anyone else has this problem hopefully my solution is useful.
I am trying to use windows form application to automate a few browser (internet explorer) tasks.
Here I am trying to select a value from the drop-down list available, which I have achieved by using webbrowser1.set attribute("","") option.
But once I have selected the option by this way the on change event of that drop down text box is not getting triggered whereas when I go back and select options manually its working fine.
Hence, here the on change event is not being triggered if I am using the set attribute option, so can someone suggest how I can select the required value and still trigger the onchange event.
Below is the snippet :
private void button4_Click(object sender, EventArgs e) {
webBrowser1.Document
.GetElementById("ctl00$MainContentPlaceHolder$ddl_Reports")
.SetAttribute("value", "EmployeeInformationReport");
}
Please suggest?
You should attach to the eventhandler with the AttachEventHandler api of the document after the DocumentCompleted event:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.AttachEventHandler("nameoftheevent", eventHandler);
}
private void eventHandler(object sender, EventArgs e)
{
//Do whatever you want
}
I need to send a click event to refreshToolStripMenuItem from another form. Here is what I have, for some reason it doesn't work. Help please.
Menu item click:
public void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
noteslist.Items.Clear();
idlist.Items.Clear();
setnotes();
}
Code used to send event:
frmnotes notes = new frmnotes();
notes.refreshToolStripMenuItem_Click(this, e);
this.Close();
Dont call the event itself.
It's bad code.
Put the create an own protected void updateMyList() Method.
internal void updateMyList()
{
noteslist.Items.Clear();
idlist.Items.Clear();
setnotes();
}
Then call the update-method from within your event.
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
updateMyList();
}
Then simply call the update-method from your form:
frmnotes notes = new frmnotes();
notes.updateMyList();
this.Close();
Btw.: Set the modifier of your Click events i.e. refreshToolStripMenuItem_Click to private.
You never should call them from outside the form.
Take a look at the MVC pattern for more info. It really helps.
In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.
I have tried raising the event in the following fashion.
private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
MouseLeftButtonDown(sender, e);
}
However, a compiler error occurs:
"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=
What do I need to do to manually raise this event?
The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.
It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.
Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.
You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.
e.g.
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
//Handle left or right mouse buttons up event
}
In your page's init function you need to have this line of code
someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);
This is pure psuedo code but should lead you on the right track
also your method will need to look like this
void DoSomething(object sender, MouseButtonEventArgs e) { }
I have a Windows Forms Link Label, "Refresh", that refreshes the display.
In another part of my code, part of a separate windows form, I have a dialog that changes the data loaded into the display in the first place. After executing this other code, pressing "Refresh" updates the data correctly.
Is there a simple way for the dialog menu to "click" the "refresh" Link Label after it has finished altering the data?
Using Visual Studio 2008.
For button is really simple, just use:
button.PerformClick()
Anyway, I'd prefer to do something like:
private void button_Click(object sender, EventArgs e)
{
DoRefresh();
}
public void DoRefresh()
{
// refreshing code
}
and call DoRefresh() instead of PerformClick()
EDIT (according to OP changes):
You can still use my second solution, that is far preferable:
private void linkLabel_Click(object sender, EventArgs e)
{
DoRefresh();
}
public void DoRefresh()
{
// refreshing code
}
And from outside the form, you can call DoRefresh() as it is marked public.
But, if you really need to programmatically generate a click, just look at Yuriy-Faktorovich's Answer
You could call the PerformClick method. But Generally it is better to have the Click event of the button call a Refresh method you write. And the menu call that method as well. Otherwise your menu depends on the button being there.
Edit:
A LinkLabel implements the IButtonControl explicitly. So you could use:
((IButtonControl)button).PerformClick();
you can use a method to refrech display, the bouton_click and the dialogBox call this method
public void refrechDate()
{
}
private void button_click(...)
{
refrechData();
}
private void MyMethod()
{
// ...
// calling refresh
this.button1_Click(this.button1, EventArgs.Empty);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
// refresh code
}