In my Windows Phone Application when I press a button for navigating to Page1 and before it finishes loading it, I press button to navigate to Page2, my application crashes. Is it correct and what is the best practice to implement this behaviour?
Update
Generally I think, the best way for me will be to block all buttons on the screen after one of them was pressed and make them active after navigation finish, so how can I do this?
The best practice is this case is to disable the Click event after the first click.
If your button was named b and you either assigned event handler in xaml or in codebehind
like
b.Click += new RoutedEventHandler(ButtonOnClick);
void ButtonOnClick(object sender, RoutedEventArgs e)
{
b.Click -= new RoutedEventHandler(ButtonOnClick);
// Navigate away now.
}
Actually my sample will stop double click on current button only but you can disable other buttons as well.
in one of my apps, i maintain a List which i iterate and show / hide, enable / disable a required. all you do is iterate and set the correct property.
foreach(var button in myButtons)
button.IsEnabled = false;
Wrap the call to NavigationService.Navigate(Page2Uri) into Dispatcher.BeginInvoke(()=> {...} ); as follows:
Dispatcher.BeginInvoke (()=> { NavigationService.Navigate(Page2Uri) });
Related
This may seem like a strange request, but I want to cancel the normal behavior of a dropdown control regarding opening or closing the list. I have several dropdowns on a form and it seemed tedious to have to hit the arrow exactly to open the list. I added code in the click event handler to open or close the list depending on its current state:
private void customerCodeComboBox_Click(object sender, EventArgs e)
{
if (customerCodeComboBox.DroppedDown == false)
customerCodeComboBox.DroppedDown = true;
else
customerCodeComboBox.DroppedDown = false;
}
This works just fine unless you actually click on the dropdown arrow. Then it opens and closes immediately since it fires the normal open and then my code closes it! :(
Is there a way to cancel the normal behavior? e.Cancel was not available in the EventArgs. Is there a better way to open the list when a mouse clicks anywhere in the field?
I am using following code in C# to add a Button
Button TextLabel = new Button(); //local variable
TextLabel.Location = new Point(0, 0);
TextLabel.Visible = true;
TextLabel.Enabled = true;
TextLabel.AutoSize = true;
TextLabel.Click += click;
this.Controls.Add(TextLabel);
And its click handler is
protected void click(object o, EventArgs e)
{
MessageBox.Show("hello");
}
Though the Button is visible and responding to mouse hover, but nothing is happening on its click. What could be wrong or missing?
If I write this same code in an independent project, it works!!!!! Strange. but why????
Form Properties: (if required)
1. Show in taskbar: false
2. Borderless
3. 50% Opaque
Today I realised that just registering click event for a control will not make any event to work unless its parent (in my case its form) on which that control is still active.
Parent control will receive event notification earlier than its child controls. This is a simple and obvious observation, but if not paid attention will make undesirable effects.
That's the mistake I did, I made another form active on my form activated event, hence any control in it didn't received events like mouse clicks.
Talking of 'hover effects are working', then yes, even if a form is inactive, hover works.
So I just removed the line of code that made another form active and everything is working fine now.
private void Form1_Activated(object sender, EventArgs e)
{
//if (form2!=null) form2.BringToFront(); //commented this
}
I wanna change a widget's backcolor at a appropriate point with the statement below:
IpAddressTextEdit.BackColor = Color.LimeGreen;
After trying many approaches, it finally works when I put this into a button_click event method and actually click the button physically. Just like below:
public void button3_Click(object sender, EventArgs e)
{
IpAddressTextEdit.BackColor = Color.LimeGreen;
Console.WriteLine("Button 3 is clicked ! ");
}
But, I never want to do this via a click and it doesnt work when I invoke this click by using InvokeOnClick() at somewhere else. Even my debugger indicates that this statement has been executed, and the assignment is successful, the "Button 3 is clicked !" is also printed, all is going as same as actual click. The only difference is my widget doesnt change its color.
I can guess there're some mechanisms behind and debugger wont show me. This assignment just stay background and never impact the front UI. So what is the real difference btw InvokeOnClick and actual click. What should I do to make it work.
code of InvokeOnClick():
if (TcpSocket.Connected)
{
MainForm mf = new MainForm();
SettingsControl sc = new SettingsControl();
sc.IpAddressTextEdit.BackColor = Color.LimeGreen; // assign directly
mf.InvokeOnClick(sc.button3, EventArgs.Empty); // Invoke a btn click
sc.OnSocketConnectedEventHandler(EventArgs.Empty); // raise a event create by my own
if(!SocketRecvThread.IsAlive)
SocketRecvThread.Start(); //Begin receive;
mf.SetupLeakDetector(); //Send command to setup machine
}
As can be seen, I've try approaches including assign the color directly, invoke a event that create by my own, invoke a btnclick event. None of them work.
As you describe the behaviour the only difference I can see is the timinig. So what is the difference between setting backgroundcolor before or after the form is shown? Well you got some events to inspect, Like Form_Load, Form_Shown or Form_Activate. If the backgroundcolor of your textbox is set in any of these events (or in methods called in these events) it will overwrite what you have set before calling .Show() / .ShowDialog() on your form
Complete list of events when a form is shown
I get data from the data base then i show it in a datagrid (wpf) , the user can make quick search (filter) from a textbox after clicking on Enter.
I use this event to handle the button -Enter- click
(OnSearch - This event is spawn whenever the search button is clicked or the Enter key is pressed.)
the problem is when the user don't write anything, the event will not be launched even when he click on Enter-Button !
how can i proceed to make it work
public MainWindow()
{
InitializeComponent();
//m_txtTest is a SearchTextBox
m_txtTest.OnSearch += new RoutedEventHandler(m_txtTest_OnSearch);
}
void m_txtTest_OnSearch(object sender, RoutedEventArgs e)
{
//to get the entered string
SearchEventArgs searchArgs = e as SearchEventArgs;
....
....
....
....
}
So when your user types something in and presses enter it is the textbox handling the Event.
When they don't type anything though the textbox doesn't have focus and cant handle the event.
What I would do is create an event for searching on the window or grid.
Somthing like this
this.OnPreviewKeyDown += new OnPreviewKeyDownEvent;
http://msdn.microsoft.com/en-us/library/ms748948.aspx
You can think of it like this
Window has focus
->Grid has focus
-->textbox has focus
What ever the last thing that has focus (or the most inner element, think of it like a cake) is will be the thing that sees the event first. If you have that event registered for that UI element it will handle it.
The textbox isn't focused so it wont see the event
I have a windows forms application. When I click on a window this activates the form and then I need to click again to call the particular control click event. For example if I click on a button this activates the form and then I need to click the button again.
Is there a way to perform the control click and window activation in one click? Preferably I would want this to work with whatever the clickable control is (menu,button, label etc)
So far I have managed to activate the win form on mouse over and then the control click works. I would like to have the win form activated on click and also run the click command on an underlying control if this has a click event.
Well, here's a way to accomplish what You want (just attach a similar method to Your from's MouseClick event):
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var c = this.GetChildAtPoint(e.Location);
this.InvokeOnClick(c, e);
}
This method has it's drawbacks though - for example the control will be clicked even though it's disabled etc., so You have to make sure the control under the cursor is "clickable" by yourself...