Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a context menu for ListBox items in C#

Status
Not open for further replies.

Khold

MIS
Jun 22, 2008
25
GB
I have a desktop application that looks like the picture present in the attachment.

I want to replicate the same in an ASP.NET web application written in C#. The desktop app currently brings up a context menu for each list whenever the user right clicks on the first cell in a row (colored blue).

The controls were built using Syncfusion, which cannot be used in a web application. So, as an alternative, I could populate a ListBox with these values (they are fetched from a database). I now want to add a context menu which appears whenever users click on an item in the list box and then performs some CRUD operations on the database related to the users, books and groups (adding/removing/deleting mainly).

How do I go about this? Pardon me, because I am new to development using the .NET framework. I am using Visual Studio 2005 with the .NET 3.0 framework.

Some sample code will be much appreciated as I have very little time in which to accomplish this.

Thank you!
 
I did something similar to display a different context menu whenever the user clicked in a specific area of a pivot table. Here's the code as is.

Good luck!

<script language = "javascript" event = "BeforeContextmenu(x, y, Menu, Cancel)" for = "PivotTable1">
{
/*
This script captures and handles the event that occurs before the Context Menu is shown, after
the user right-clicks.
We had to rebuild the Context Menu in order to add the option "Set as KPI"
*/
if (document.Form1.PivotTable1.SelectionType == "PivotAggregates") {


var cmContextMenu = new Array(14);
var cmClearSubMenu = new Array(2);
var cmdTopSubMenu = new Array(11);
var cmdBottomSubMenu = new Array(11);
var cmdShowAsSubMenu = new Array(6);


cmdTopSubMenu[0] = Array("1", 1100);
cmdTopSubMenu[1] = Array("2", 1101);
cmdTopSubMenu[2] = Array("5", 1102);
cmdTopSubMenu[3] = Array("10", 1103);
cmdTopSubMenu[4] = Array("25", 1104);
cmdTopSubMenu[5] = null;
cmdTopSubMenu[6] = Array("1%", 1105);
cmdTopSubMenu[7] = Array("2%", 1106);
cmdTopSubMenu[8] = Array("5%", 1107);
cmdTopSubMenu[9] = Array("10%", 1108);
cmdTopSubMenu[10] = Array("25%", 1109);
cmdTopSubMenu[11] = null;
cmdTopSubMenu[12] = Array("&Other...", 1120);


cmdBottomSubMenu[0] = Array("1", 1110);
cmdBottomSubMenu[1] = Array("2", 1111);
cmdBottomSubMenu[2] = Array("5", 1112);
cmdBottomSubMenu[3] = Array("10", 1113);
cmdBottomSubMenu[4] = Array("25", 1114);
cmdBottomSubMenu[6] = null;
cmdBottomSubMenu[7] = Array("1%", 1115);
cmdBottomSubMenu[8] = Array("2%", 1116);
cmdBottomSubMenu[9] = Array("5%", 1117);
cmdBottomSubMenu[10] = Array("10%", 1118);
cmdBottomSubMenu[11] = Array("25%", 1119);
cmdBottomSubMenu[12] = null;
cmdBottomSubMenu[13] = Array("&Other...", 1120);

cmdShowAsSubMenu[0] = Array("&Normal", 12135);
cmdShowAsSubMenu[1] = Array("Percent of &Row Total", 12136);
cmdShowAsSubMenu[2] = Array("Percent of &Column Total", 12137);
cmdShowAsSubMenu[3] = Array("Percent of &Parent Row Item", 12138);
cmdShowAsSubMenu[4] = Array("Percent of P&arent Column Item", 12139);
cmdShowAsSubMenu[5] = Array("Percent of &Grand Total", 12140);

var kpiId = '<%=Session("kpiId")%>';
var i;
i = 0
if (kpiId != "") {
cmContextMenu = Array("Update KPI", "UpdateKPI");
i++;
}

cmContextMenu = Array("Set as &KPI", "SetAsKPI");
cmContextMenu[i+1] = null;
cmContextMenu[i+2] = Array("&Copy", 1002);
cmContextMenu[i+3] = null;
cmContextMenu[i+4] = Array("Remov&e Total", 12010);
cmContextMenu[i+5] = null;
cmContextMenu[i+6] = Array("Sor&t Ascending", 2000);
cmContextMenu[i+7] = Array("Sort Desce&nding", 2031);
cmContextMenu[i+8] = null;
cmContextMenu[i+9] = Array("Sho&w only the Top", cmdTopSubMenu);
cmContextMenu[i+10] = Array("Show only the &Bottom", cmdBottomSubMenu);
cmContextMenu[i+11] = Array("Show &As", cmdShowAsSubMenu);
cmContextMenu[i+12] = null;
cmContextMenu[i+13] = Array("Commands and &Options", 1005);

Menu.Value = cmContextMenu;
}
}
</script>
 
Thanks for sharing your code. I will see how I can implement this.

Any other inputs from folks? This is an urgent requirement as well...
 
Hey...I implemented a context menu using some MSDN library code.

However, when I right click on an item in my Listbox, two things happen:

a.) The context menu is hidden almost entirely by the Listbox. It is fired using Javascript. Is there a way I can use Javascript to work around it?
b.) On right clicking, the Listbox does not select the item shown.

How do I overcome these two pitfalls?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top