I would please like to know how to display a context menu when you right click on the window.
Here is my code so far :
private void ShowContextMenu_RightClick(object sender, EventArgs e)
{
toolStripMenuItem5.Visible = true;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programmed by D & K");
}
AFAIK there is no direct RightClick event in winforms. You can use the mousedown event to achieve this
private void toolStripButton1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("Hi there this is my 3rd app which is *animation*.", "Programed by D & K");
}
}
in form cs file you can attach your context menu like this..
public Form1()
{
InitializeComponent();
//Create right click menu..
ContextMenuStrip s = new ContextMenuStrip();
// add one right click menu item named as hello
ToolStripMenuItem hello = new ToolStripMenuItem();
hello.Text = "Hello";
// add the clickevent of hello item
hello.Click += hello_Click;
// add the item in right click menu
s.Items.Add(hello);
// attach the right click menu with form
this.ContextMenuStrip = s;
}
void hello_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello Clicked");
}
You should use MouseDown. Then you can get the clicked button with e.Button and the coordinates with e.X and e.Y.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Button.ToString() + " " + e.X + " " + e.Y);
}
Add a ContextMenuStrip control to the form (note, it does not display on the form but is instead shown at the bottom of the designer).In the form's ContextMenuStrip property, choose the name of the ContextMenuStrip control you just added to the form.That's it. HansPassant stated this in a comment to the question but I think it is being overlooked.
The ContextMenuStrip property is a property of many UI controls that you would use the same technique for.
Related
I have the same ContextMenu assigned to a Form and to a NotifyIcon.
this.ContextMenu = this.contextMenu;
this.notifyIcon.ContextMenu = this.ContextMenu;
In the Popup event of the context menu I'm trying to find out who is displaying the context menu: the form (such as a right click on the form), or the notify icon (a right click on the notification icon):
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(this.contextMenu.SourceControl.Name);
}
However, I always get the form as the source control, even if I right click on the notification icon.
I'm using C#, .NET Framework 4.6 and Windows Forms.
Maybe, a quickest way is to have a flag:
private bool fromIcon;
private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
fromIcon = true;
}
}
private void ContextMenu_Popup(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(fromIcon.ToString());
fromIcon = false;
}
I have a tray application and I would like to open the ContextMenuStrip if I click on the tray logo. This is my code:
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
timeBroMenu.Show(Cursor.Position);
}
}
Double click is working easily.
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
timeBroMenu.Show(Cursor.Position);
}
Is there any way to make the left click work?
You don't need to handle the Click event if you have assigned the NotifyIcon's ContextMenuStrip Property.
Im using windowsForms c# have a listbox which is bound with data items ...So depending on double click of the listbox item i want the corressponding form open...thnx in adv
ListBox has an DoubleClick event. You can access it if you select object, open Events tab in a Properties window.
Double click it and Visual Studio will create an event handler for you like this:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
// here is your code
}
Now, you just need to enter your code.
If you want to open a form with a corresponding item then it will be something like:
public void ListBox1_OnDoubleClick(object sender, EventArgs e)
{
string text = listBox1.Text; // Don't forget to manipulate with it
Form1 form = new Form1();
form.Show();
}
Subscribe to DoubleClick event on ListBox
listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// logic here
}
or
listBox1.DoubleClick += (s,e) => { /*logic here */};
I would use the MouseDoubleClick Event, it provides the cursor position in MouseEventArgs so you can easily detect which item got doubleclicked.
void Listbox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int index = Listbox1.IndexFromPoint(e.Location);
if (index != ListBox.NoMatches)
{
object clickedItem = Listbox1.Items[index];
// open your form here
}
}
How can I return to the last used RibbonTab that was in focus when the window was last closed?
You could create a variable to hold the reference to the RibbonTab and listen to the SelectionChanged event on your Ribbon object.
MyRibbonObj.SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
{
RibbonTab rt = ((sender as Ribbon).SelectedItem as RibbonTab);
MyReferenceToRibbonTab = rt;
}
This way you keep track of the latest selected RibbonTab within your Ribbon.
PS: code might needs tweaks. I didn't test it.
Create a setting LastRibbonTab
Save the last tab used in MainWindow_Closed
MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LastRibbonTab = (MyRibbon.SelectedItem as RibbonTab).Header.ToString();
Properties.Settings.Default.Save();
}
Select the last tab in MainWindow_Loaded
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Init();
foreach (RibbonTab r in MyRibbon.Items)
if (r.Header.ToString() == Properties.Settings.Default.LastRibbonTab)
{
MyRibbon.SelectedItem = r;
break;
}
}
Is there any possibility to get a value of doubleclicked row in ListView?
I registered an event:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems.ToString());
}
But on message, when i doubleclick some row in listview i get:
System.Windows.Forms.ListView+SelectedListViewItemCollection
What is more, I have got 2 columns in listview:
lvLista.Columns.Add("ID");
lvLista.Columns.Add("Tilte");
And i want to show in messagebox the "ID" of doubleclicked row.
How to do it? How to get a values from this event?
If you handle the MouseDown and/or MouseDoubleClick events of the ListView control, and use the HitTest method to determine the target of the mouse action, you will know which item has been double clicked. This is also a good means to determine if NO item was clicked (for example, clicking on the empty area in a partially filled list.
The following code will display the clicked item in a textbox if a single click occurs, and will pop up a message box with the name of the double-clicked item if a double click occurs.
If the click or double click occur in an area of the list view not populated by an item, the text box or message box inform yopu of that fact.
This is a trivial example, and depending on your needs, you will have to mess with it a little.
UPDATE: I added some code which clears the SelectedItems property of the Listview control when an empty area of the list is clicked or double-clicked.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.MouseDown += new MouseEventHandler(listView1_MouseDown);
listView1.MouseDoubleClick += new MouseEventHandler(listView1_MouseDoubleClick);
this.Load += new EventHandler(Form1_Load);
}
void Form1_Load(object sender, EventArgs e)
{
this.SetupListview();
}
private void SetupListview()
{
ListView lv = this.listView1;
lv.View = View.List;
lv.Items.Add("John Lennon");
lv.Items.Add("Paul McCartney");
lv.Items.Add("George Harrison");
lv.Items.Add("Richard Starkey");
}
void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
MessageBox.Show("The selected Item Name is: " + item.Text);
}
else
{
this.listView1.SelectedItems.Clear();
MessageBox.Show("No Item is selected");
}
}
void listView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
ListViewItem item = info.Item;
if (item != null)
{
this.textBox1.Text = item.Text;
}
else
{
this.listView1.SelectedItems.Clear();
this.textBox1.Text = "No Item is Selected";
}
}
}
Try this:
private void lvLista_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(lvLista.SelectedItems[0].SubItems[0].Text);
}
I know this thread is old but nobody here answered the question properly in my opinion. For those in the future, try this, from MSDN:
// User must double-click to activate item
myListView.Activation = System.Windows.Forms.ItemActivation.Standard;
// Add event handler
myListView.ItemActivate += new
System.EventHandler(this.myListView_ItemClick);
Since the accepted answer didn't help me i thought that I would share my solution to the same problem: getting data from a specific column in a listview in the double click event.
The following line returns the data of the second column in the row that I've double clicked on as a string:
private void listViewOutput_DoubleClick(object sender, EventArgs e)
{
string content = listViewOutput.Items[listViewOutput.SelectedIndices[0]].SubItems[1].Text
}
Thanks; this is what I needed. I thought I'd also mention one could set up the local info variable more generally as:
ListViewHitTestInfo info = ((ListView)sender).HitTest(e.X, e.Y);
Try this
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = listView1.HitTest(e.Location);
Rectangle rowBounds = hit.SubItem.Bounds;
Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
int leftMargin = labelBounds.Left - 1;
string x = hit.Item.Text;
}