Click here to Skip to main content
15,916,941 members
Home / Discussions / C#
   

C#

 
GeneralRe: Plz Help on Net Send Pin
Dario Solera1-Jul-05 23:40
Dario Solera1-Jul-05 23:40 
GeneralIntegration of Window Service and my project through set up program Pin
ksanju10001-Jul-05 19:09
ksanju10001-Jul-05 19:09 
GeneralRe: Integration of Window Service and my project through set up program Pin
mav.northwind1-Jul-05 20:10
mav.northwind1-Jul-05 20:10 
GeneralHey all, i have a bit of a problem with event handlers... Pin
tom_dx1-Jul-05 16:43
tom_dx1-Jul-05 16:43 
GeneralRe: Hey all, i have a bit of a problem with event handlers... Pin
ACorbs1-Jul-05 17:02
ACorbs1-Jul-05 17:02 
GeneralSorting problem Pin
damir_tk1-Jul-05 11:25
damir_tk1-Jul-05 11:25 
GeneralRe: Sorting problem Pin
Guffa1-Jul-05 12:34
Guffa1-Jul-05 12:34 
GeneralDynamic Menus in C# Pin
0ryan01-Jul-05 9:08
0ryan01-Jul-05 9:08 
I am having some problems adding dynamic MenuItems to a context menu in a C# Windows application. Look at the pasted code below. Click the Add Item button a couple times, then right click anywhere in the checked list box. List Items should expand with a list of all the items in the checked list box.

Now, click the button a couple more times and notice the change on the right-click context menu. List Items no longer expands!

I've also tried:
Clearing ListItems.MenuItems collection and re-adding new MenuItems on Popup
A static list of MenuItems added to ListItems.MenuItems in InitializeComponent, set visible or not depending on the number of items in the check box

Any ideas?

Compile the code with: csc /target:winexe MenuTest.cs

////////////////////////////////////////////////
// Begin Pasted Code Here
///////////////////////////////////////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class MenuTest : System.Windows.Forms.Form
{
private System.Windows.Forms.Button AddItem;
private System.Windows.Forms.ContextMenu DynamicMenu;
private System.Windows.Forms.CheckedListBox ListBox;
private int ListItemCount = 0;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem ListItems;
private System.ComponentModel.Container components = null;

public MenuTest()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.ListBox = new System.Windows.Forms.CheckedListBox();
this.AddItem = new System.Windows.Forms.Button();
this.DynamicMenu = new System.Windows.Forms.ContextMenu();
this.ListItems = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();

this.ListBox.Location = new System.Drawing.Point(8, 32);
this.ListBox.Name = "ListBox";
this.ListBox.Size = new System.Drawing.Size(120, 94);
this.ListBox.TabIndex = 0;
this.ListBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ListBox_MouseDown);

this.AddItem.Location = new System.Drawing.Point(32, 152);
this.AddItem.Name = "AddItem";
this.AddItem.TabIndex = 1;
this.AddItem.Text = "Add Item";
this.AddItem.Click += new System.EventHandler(this.AddItem_Click);

this.DynamicMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.ListItems});
this.DynamicMenu.Popup += new System.EventHandler(this.DynamicMenu_Popup);

this.ListItems.Index = 0;
this.ListItems.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.ListItems.Text = "List Items";

this.menuItem2.Index = 0;
this.menuItem2.Text = "Here Is A List";

this.menuItem3.Index = 1;
this.menuItem3.Text = "-";

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(136, 189);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.AddItem,
this.ListBox});
this.Name = "MenuTest";
this.Text = "MenuTest";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new MenuTest());
}

private void AddItem_Click(object sender, System.EventArgs e)
{
this.ListItemCount++;
this.ListBox.Items.Add( "item" + ListItemCount.ToString() );
}

private void ListBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right )
{
System.Drawing.Point pt = new Point( e.X + 10 , e.Y + 25 );
this.DynamicMenu.Show( this, pt );
}
}

private void DynamicMenu_Popup(object sender, System.EventArgs e)
{
foreach( object o in this.ListBox.Items )
{
this.ListItems.MenuItems.Add( o.ToString() );
}
}
}

GeneralRe: Dynamic Menus in C# Pin
rudy.net1-Jul-05 18:01
rudy.net1-Jul-05 18:01 
GeneralSendMessage Pin
ladwah1-Jul-05 8:46
ladwah1-Jul-05 8:46 
GeneralRe: SendMessage Pin
Judah Gabriel Himango1-Jul-05 9:47
sponsorJudah Gabriel Himango1-Jul-05 9:47 
GeneralRe: SendMessage Pin
ladwah1-Jul-05 10:36
ladwah1-Jul-05 10:36 
GeneralC# Threading problem Pin
X3mFran1-Jul-05 7:50
X3mFran1-Jul-05 7:50 
GeneralRe: C# Threading problem Pin
S. Senthil Kumar1-Jul-05 8:34
S. Senthil Kumar1-Jul-05 8:34 
GeneralRe: C# Threading problem Pin
X3mFran1-Jul-05 8:58
X3mFran1-Jul-05 8:58 
GeneralDataGrid row tag Pin
Jassim Rahma1-Jul-05 7:04
Jassim Rahma1-Jul-05 7:04 
GeneralRe: DataGrid row tag Pin
Steve Maier1-Jul-05 7:23
professionalSteve Maier1-Jul-05 7:23 
GeneralRe: DataGrid row tag Pin
glenn2011-Jul-05 12:46
glenn2011-Jul-05 12:46 
GeneralRe: DataGrid row tag Pin
Jassim Rahma1-Jul-05 12:54
Jassim Rahma1-Jul-05 12:54 
GeneralRe: DataGrid row tag Pin
glenn2011-Jul-05 17:03
glenn2011-Jul-05 17:03 
Questionhow to detect changes to text property of multiple controls bound using CurrencyManger Pin
therealmccoy1-Jul-05 6:13
therealmccoy1-Jul-05 6:13 
Generaluse debug info from external application Pin
Piovra_1-Jul-05 4:14
Piovra_1-Jul-05 4:14 
GeneralRe: use debug info from external application Pin
Roland Bär1-Jul-05 5:48
Roland Bär1-Jul-05 5:48 
GeneralRe: use debug info from external application Pin
S. Senthil Kumar1-Jul-05 6:07
S. Senthil Kumar1-Jul-05 6:07 
GeneralRe: use debug info from external application Pin
Piovra_6-Jul-05 2:13
Piovra_6-Jul-05 2:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.