How to handle both click and double click in silverlight - silverlight

I want to create paint application with silverligh, but I have the problem. I have a color rectangle When click the rectangle the shape will be stroked and double click to fill the shape. My code below not work exactly, when double click the first ClickCount is 1 and then increasing to 2. Can you tell me how to fix it. Thanks.
private void Rect0_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
RectFront1.Fill = Rect0.Fill;
}
else
{
RectFront.Fill = Rect0.Fill;
}
}

According to this post (http://www.c-sharpcorner.com/uploadfile/dbd951/how-to-handle-double-click-in-silverlight-5/) the e.ClickCount has the following behavior.
The count is calculated based on the time between first click and the second click.
Quote from the post.
Consider if you are trying to click 5 times. After 3rd click, if you
give 200 milliseconds gab between the 3rd and 4th click then the 4th
click will be treated as the 1st click. It will reset the ClickCount
property if the time between the first click and second click is
greater the 200 milliseconds.
Alternativly you could think about having a behavior for a double click since I assume that you would use it quite often in a paint application.
Here is a good article:
Double-Click event in silverlight on framework element.
http://blog.cylewitruk.com/2010/10/double-click-event-in-silverlight-on-frameworkelement/
The third option I can think of would be using RX (https://www.nuget.org/stats/packages/Rx-Silverlight)
https://msdn.microsoft.com/library/hh242985.aspx
Observable.FromEvent<MouseButtonEventArgs>(myControl, "MouseLeftButtonDown").TimeInterval().Subscribe(evt =>
{
if (evt.Interval.TotalMilliseconds <= 300)
{
// Do something on double click
}
});
which was a answer on this question: Cleanest single click + double click handling in Silverlight?

Related

error on exiting a window

In the following picture, when i click File->New Game I see this window:
If i continue and press Start Game, everything works great. But if i just click the red X, i get this error:
This is the code for File->New Game:
private: System::Void newGameToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e)
{
NG->ShowDialog();
ShowPossible();
update_score();
if(pc_exists()==1)
ComputerPlay();
}
NG->ShowDialog() shows the New Game dialog.
And then ShowPossible() shows something on the board (hints for possible moves).
And that's what's making trouble.
I need some code that quits from newGameToolStripMenuItem_Click() on X click instead of continuing to ShowPossible().
I tried making a global variable called ready in form NewGame, and at form load initialized it with 0, and only when i click Start Game it turns to 1. And finally added this condition in the above function:
...
if(ready)
ShowPossible();
...
So this way if i don't click Start Game, and only click X, ready will be 0 and it won't enter ShowPossible(). But it didn't work. Somehow when the code for the button Start Game finishes, ready is still 0.
Is there any more efficient way to deal with this?
Thank you !
EDITED:
Since you are using System.Windows.Forms, check against the DialogResult enumeration (Thanks chris):
if (NG->ShowDialog() == System::Windows::Forms::DialogResult::Cancel)
return;
From MSDN:
When a form is displayed as a modal dialog box, clicking the Close
button (the button with an X at the upper-right corner of the form)
causes the form to be hidden and the DialogResult property to be set
to DialogResult.Cancel.

I have a Mouse Click Event handler which works when i select the first time but..?

I have a DataGridView in C# which is connected with my Access database. I see all the books on my DataGridView. I have created a mouse Click event handler to select a row than I can save that row, as you see below:
private void Mouse_Click(object sender, MouseEventArgs e)
{
try
{
temp = dataGridView1.SelectedRows[0].Index.ToSt…
//This one is selecting the row. To do that select panel>event> on MouseClick write name and double click:
MessageBox.Show("You have selected the row " + temp);
}
catch
{
}
}
It works when I open the form. However, after I save the selected row to another database, the Mouse Click event handler stop working. In other words, after saving the first selected row, when I come back to select another row and save to database the Mouse Click Event handler does not work. The Message says row is out of range.
You didn't give too much information related to connection type, Event Handler and more importantly the way you save/update the record so I'm just guessing here: aren't you accidentally overwriting the DataSource of your grid? That's the only thing I can imagine for such behavior ("row is out of range" - which also assumes the event still works as intended but the code in the function cannot see the data anymore).

Adding items to below gridview when dropped on top gridview

On my main page, I have a button and telerik mainRadGridView. When I click my button, I get a popup which is divided it into two regions left and right.
On left, I have a RadTreeView and on right, I have a popupRadGridView.
Initially when my popup is open, the popupRadGridView is disabled. When I drop from left to right for first time in my popup nothing is added to popupRadGridView since it is disable which is correct.
But when I drop for second time on to my disabled popupRadGridView inside my opened popup, there is an element added to my mainRadGridView on my main page which is wrong.
I cannot understand how to stop adding onto my main page. Any help is greatly appreciated.
check for the e.Options.ParticipatingVisualRoots in Drop Event of GridView - should provide you the no of active opened child windows participating in the operation.
For additional information follow this link
Telerik Silverlight
I solved my issue as below
In my mainpage codebehind. I did like below
void mainRadGridView_OnDropInfo(object sender, DragDropEventArgs e)
{
if(e.Options.ParticipatingVisualRoots.Count > 0)
{
return;
}
else
{
do stuff;
}
}

updating UI in windows phone 7

In method that is working in the background, i have two important lines :
createPopup();
MessageBox.Show(sth);
more lines
more lines
createPopup() just creates a popup, adds a grid as a child and shows popup.
My question is, why first shows up messageBox, then shows up Popup, which appears after all lines in this method done ? How could I make this popup to show before all lines in this method will be done ?
All the UI changes are normally queued up and will be shown at once on the screen.
And this does not include MessageBox. So it shows up immediately and prevents the execution, until user clicks on Ok. Hence eventhough your popUP is first executed, it will be shown in the UI only after the MessageBox.
For your problem, Try placing your MessageBox.Show(something) in a separate thread.
createPopup();
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("some message");
});
more lines
more lines
Give it a try. I am not sure whether it solves your problem or not as I dnt know the code in createPopUp() method.
Creating the pop-up, does not actually draw it on the screen until the Layout event. If you want to ensure that the pop-up has been drawn before you display the pop-up, attach an event handler to the pop-up's LayoutUpdated event and display the message box from within that event handler. Be sure to detach the event handler as well or you will see multiple message boxes.
public InitPage()
{
Popup popup = new Popup();
popup.LayoutUpdated += popup_LayoutUpdated;
LayoutRoot.Controls.Add(popup);
}
void popup_LayoutUpdated(object sender, object e)
{
popup_LayoutUpdated -= popup_LayoutUpdated;
MessageBox.Show("hello");
}

Button simulating keyboard "Shift" key pressed

I Have a WPF window that have a RadGridView and a Button,
My requirement is, when you click on the button and
select a row in the grid, it should work similar to
pressing on the shift key and clicking on a gridview row.
(To select multiple rows).
So, i need to press the shift key programatically in button
click event.
Can this be done?
Appreciate if anyone can provide a solution
Do not fire a KeyPress event manually. Rather, on this page, Telerik indicates that to allow multiple selection, you must set the SelectionMode to Extended:
this.radGridView.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
So, put this in your button click (I guess you'll want it to toggle between Extended and Single. Multiple's behaviour is awkward, but that's just me) and you're good to go.
Edit:
The toggle button will have something like this in its click event handler:
private void toggleButton1_Click(object sender, RoutedEventArgs e)
{
this.radGridView.SelectionMode = toggleButton1.IsChecked ? SelectionMode.Extended : SelectionMode.Single;
}

Resources