Is there a way to get OnItemRightTapped event on ListView or GridView that works exactly like ItemClick, except obviously react only on right tap?
You can add an event handler for mouse down and then determine the click source in the code:
private void listView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
{
// Do what u need to do here
}
else
e.Handled = true;
}
Related
I was trying to show a form(FormChild), with some radio buttons in, just to select, close, and get the value of the selected radio button from the calling form(FormParent). On a click event handler for a Button in FormParent, I just did:
var formChild=newFormChild();
formChild.ShowDialog(this);
All was working great until I decided to handle the CheckedChanged event of one of the RadioButtons inside FormChild:
private void SomeRadioButton_CheckedChanged(object sender, EventArgs e)
{
Close();
}
Now the formChild.ShowDialog(this); did not showed formChild and formChild immediately returns DialogResult.Cancel.
Any explanation on this?
Thanks in advance
The lowest Tab Index radiobutton will be checked by default, If this event handler is assigned to that button it will cause the situation that you are describing.
You can either change your Tab Order or create a Boolean Flag that is set in your Forms Shown EventHandler to keep it from Triggering until you check it again.
public partial class Form1 : Form
{
bool initDone;
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (initDone)
{
if (((RadioButton)sender).Checked == true)
{
Close();
}
}
}
private void Form1_Shown(object sender, EventArgs e)
{
initDone = true;
}
}
Is there anywhere if your code that sets the value of the RadioButton? If you programmatically set the "Checked" property of the RadioButton, it will fire the event.
In your situation, the event handler contains your Form.Close() so the form never gets the chance to be visible to the user.
Note: Setting the RadioButton.Checked as "true" in the designer will not fire the event.
I need a mouse click event in my silver light project and I know we need to simulate it ourself if the object is not button. lets say I want mouseclick for my img...
How exactly can we track time between mousedown and mouseup and say if the time between them is less than 300m, we have a mouse click?
Handle the MouseLeftButtonDown and MouseLeftButtonUp events for your image.
private DateTime? startClick;
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startClick = DateTime.Now;
}
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var clickDuration = DateTime.Now - startClick;
if (startClick != null && clickDuration < TimeSpan.FromMilliseconds(300))
{
MessageBox.Show("Less than 300ms!");
}
startClick = null;
}
A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items.
How do I know the ListBoxItem Text that right mouse button clicked on?
private void Init2()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItemOpen = new MenuItem();
menuItemOpen.Click += new RoutedEventHandler(menuItemOpen_Click);
contextMenu.Items.Add(menuItemOpen);
listBox1.ContextMenu = contextMenu;
}
void menuItemOpen_Click(object sender, RoutedEventArgs e)
{
//How do I know the listItem text that right mouse button clicked on?
}
When you right click, you actually also select. So that means you can just do:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
string selectedListBoxItemText = ((ListBoxItem)listBox1.SelectedItem).Content.ToString());
// do your thing
}
If the user clicks on the overlay, I want the ChildWindow to automatically close and return the user to the main screen.
Is there a property that controls this? If not, is there a way to attach a click handler to the overlay?
Turns out you can get a reference to the overlay right after it is created. After that it is a simple matter of attaching the event handler.
private void Overlay_MouseButtonDown(object sender, MouseButtonEventArgs e)
{
this.Close();
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var overlay = (Grid)GetTemplateChild("Overlay");
overlay.MouseLeftButtonDown += Overlay_MouseButtonDown;
overlay.MouseRightButtonDown += Overlay_MouseButtonDown;
}
How can focus the controls when select a tabitem in a tabcontrol?
You should capture the Selection changed event of the TabControl and inside that you focus the control you need. Just as
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
control1.Focus();
}
Not sure I understand what you're asking completely, but you probably want to capture the SelectionChanged event for the TabControl:
public Window1()
{
InitializeComponent();
TabControl1.SelectionChanged += TabControl1_SelectionChanged;
}
private void TabControl1_SelectionChanged(object sender, EventArgs e)
{
TabItem item = (TabItem)TabControl1.SelectedItem;
// Find the first control in the TabItem content and focus it?
}