hi,
i am creatnig an windows desktop application. and in the main form i have a menu stript. what i want to do is attached one user control to eacn menu stript. each user control has a different design.
how can i do this???
any tutorial
hi,
i am creatnig an windows desktop application. and in the main form i have a menu stript. what i want to do is attached one user control to eacn menu stript. each user control has a different design.
how can i do this???
any tutorial
The simplest, most maintainable pattern for the scenario described by is to treat the menu as a navigation mechanism and host the different screens as UserControls inside a single placeholder on the main form (a Panel). That keeps layout/design work simple (each screen is a designer-backed UserControl), preserves per-screen state when needed, and avoids trying to force controls into the menu itself. ’s post shows embedding a control into a menu for small widgets; the pattern below is for swapping full-area controls.
Create a Panel (e.g. contentPanel) docked to Fill and load UserControls into it at runtime. A small, reusable loader that caches instances preserves state and keeps transitions fast:
private readonly Dictionary<Type, UserControl> _cache = new Dictionary<Type, UserControl>();
private T ShowControl<T>() where T : UserControl, new()
{
Type t = typeof(T);
if (!_cache.TryGetValue(t, out UserControl ctrl))
{
ctrl = new T { Dock = DockStyle.Fill };
_cache[t] = ctrl;
contentPanel.Controls.Add(ctrl);
}
foreach (Control c in contentPanel.Controls) c.Visible = false;
ctrl.Visible = true;
ctrl.BringToFront();
return (T)ctrl;
}
// wire a menu click to:
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowControl<SettingsControl>();
} Notes and troubleshooting:
Dock = DockStyle.Fill so the control resizes with the form. Func<UserControl>) instead of new() or expose an Initialize(...) method. Dispose() if controls are removed permanently to avoid leaks. Jump to Post— nick.crane 342Judith, you asked a similar question in this thread.
What effect are you trying to achieve?
[Edit]
What do you mean by "attach one user control to each menu strip"?
Judith, you asked a similar question in this thread.
What effect are you trying to achieve?
[Edit]
What do you mean by "attach one user control to each menu strip"?
Have you looked at using a ToolStrip instead.
This allows you to "host" any control derived from Control.
A detailed example can be found here.
This is what I came up with to use a PictureBox in a ToolStripMenu.
public partial class Form2 : Form
{
ToolStripPictureBox toolstripPictureBox;
public Form2()
{
InitializeComponent(); //<- toolStip1 added by designer
toolstripPictureBox = new ToolStripPictureBox();
toolstripPictureBox.PictureBoxImage = this.Icon.ToBitmap();
toolStrip1.Items.Add(toolstripPictureBox);
}
}
class ToolStripPictureBox : ToolStripControlHost
{
public ToolStripPictureBox()
: base(new PictureBox())
{
}
public PictureBox PictureBoxControl
{
get { return this.Control as PictureBox; }
}
public Image PictureBoxImage
{
get { return PictureBoxControl.Image; }
set { PictureBoxControl.Image = value; }
}
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.