I am still new to the Event driven GUI programming paradigm of C# so I am just running with an idea of how to implement the desired effect: Imagine a chat room
A ListBox that has a list of names. The Users can highlight and select a user (that is easy to implement) then when 2 conditions are met, a ContextMenuStrip will appear.
Conditions to be met: 1. A name must be selected
2. the mouse click must have occured while hovering over the selected name.
I am using inherintance / Encapsulation to solve this delima.
UListBox contains all normal logic + the logic for using the ContexMenuStrip
Now so far I have been able to create a ListBox with names... but since they are nothing more than strings inside of the ListBox, there is no association to the ContextMenuStrip, thus right clicks do nothing.
Now, I could attach the ContextMenuStrip to the ListBox as so: ListBox.ContextMenu = menu; ... but this means that Condition #1 & 2 may not be met since the user would see the menu no matter what the clicked on within the bounds of the ListBox.
I do want to attach the ContextMenuStrip to The UListBox BUT it needs to implement the logic necessary to include the conditions required for it to be displayed.
I have been working on an implementation method to solve this problem but I'm stuck because I need the coordinates of the name that was clicked. If someone could help me complete this solution or give me some guidence that would be great. If a better solution exists then I'm also all ears for it.
Please refrain from being a coding style critic ... unless something is blatently inefficient then just save us both the time and don't mention it. I'm not a coding expert but I don't need people showing off there views of coding style. Thanks
A ListBox that has a list of names. The Users can highlight and select a user (that is easy to implement) then when 2 conditions are met, a ContextMenuStrip will appear.
Conditions to be met: 1. A name must be selected
2. the mouse click must have occured while hovering over the selected name.
I am using inherintance / Encapsulation to solve this delima.
UListBox contains all normal logic + the logic for using the ContexMenuStrip
Now so far I have been able to create a ListBox with names... but since they are nothing more than strings inside of the ListBox, there is no association to the ContextMenuStrip, thus right clicks do nothing.
Now, I could attach the ContextMenuStrip to the ListBox as so: ListBox.ContextMenu = menu; ... but this means that Condition #1 & 2 may not be met since the user would see the menu no matter what the clicked on within the bounds of the ListBox.
I do want to attach the ContextMenuStrip to The UListBox BUT it needs to implement the logic necessary to include the conditions required for it to be displayed.
I have been working on an implementation method to solve this problem but I'm stuck because I need the coordinates of the name that was clicked. If someone could help me complete this solution or give me some guidence that would be great. If a better solution exists then I'm also all ears for it.
Please refrain from being a coding style critic ... unless something is blatently inefficient then just save us both the time and don't mention it. I'm not a coding expert but I don't need people showing off there views of coding style. Thanks
Code:
using System;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
namespace chatTesting.GUI
{
public class UListBox : ListBox
{
private ContextMenuStrip menu;
private System.Drawing.Point mousePosition;
public UListBox():
base()
{
InitializeListBox();
InitializeDelegates();
InitializeContextMenu();
}
/// INITIALIZATIONS
/// ////////////////////////////////
private void InitializeListBox()
{
this.FormattingEnabled = true;
this.ScrollAlwaysVisible = true;
this.Dock = DockStyle.Fill;
this.Location = new System.Drawing.Point(0, 0);
this.Name = "Channel Users";
this.Size = new System.Drawing.Size(160, 200);
this.TabIndex = 1;
}
private void InitializeDelegates()
{
this.MouseClick += new MouseEventHandler(UListBox_MouseMove);
}
private void InitializeContextMenu()
{
menu = new ContextMenuStrip();
menu.DefaultDropDownDirection = ToolStripDropDownDirection.AboveRight;
menu.Visible = true;
menu.Opening += new System.ComponentModel.CancelEventHandler(menu_Opening);
ToolStripMenuItem whisperItem = new ToolStripMenuItem("Whisper");
whisperItem.Size = new System.Drawing.Size(117, 22);
//whisperItem.Click += new EventHandler(whisperItem_Clicked);
ToolStripMenuItem silenceItem = new ToolStripMenuItem("Silence");
silenceItem.Size = new System.Drawing.Size(117, 22);
//silenceItem.Click += new EventHandler(silenceItem_Clicked);
ToolStripMenuItem whoIsItem = new ToolStripMenuItem("WhoIs");
whoIsItem.Size = new System.Drawing.Size(117, 22);
//whoIsItem.Click += new EventHandler(whoIsItem_Clicked);
menu.Items.AddRange(new ToolStripItem[] { whisperItem, whoIsItem, silenceItem });
this.ContextMenuStrip = menu;
}
/// CALLBACK METHODS
/// ////////////////////////////////
private void menu_Opening(object sender, CancelEventArgs arg)
{
//need to implement logic here... for checking to see if menu should display
//|| (i want to check to see if the mouse click is within the bounds
//of the correct name[using mousePosition & unknown source ])
}
private void UListBox_MouseClick(object sender, MouseEventArgs arg)
{
if (arg.Button == MouseButtons.Right)
{
mousePosition.X = arg.X;
mousePosition.Y = arg.Y;
}
}
/// HELPER METHODS
/// ////////////////////////////////
public void PopulateUsersList(string[] users)
{
//Singleton InsertionList that is not necessary for this thread to be displayed
users = InsertionSort.sort(users);
this.Items.AddRange(users);
}
}
}