Hi
I would like to ask if it is possible for me to create buttons inside a button using C#, such that when i click a button few more other buttons is available for clicking. How can i do that??
Thanks
Hi
I would like to ask if it is possible for me to create buttons inside a button using C#, such that when i click a button few more other buttons is available for clicking. How can i do that??
Thanks
Two workable patterns will give you the behavior you want without fighting WinForms painting/focus issues. showed adding a button to another button; that can technically work, but a button is not a proper container and it often causes focus, clipping or mouse-event problems. Use a small panel (or FlowLayoutPanel) that you show/hide, or use a popup menu (ContextMenuStrip / ToolStripDropDown) for a menu-like set of choices.
A compact example that uses a Panel placed next to the trigger button (safe and predictable):
// create once (e.g. Form_Load)
var popup = new Panel {
Size = new Size(140, 80),
BorderStyle = BorderStyle.FixedSingle,
Visible = false
};
var a = new Button { Text = "A", Size = new Size(60,24), Location = new Point(8,8) };
var b = new Button { Text = "B", Size = new Size(60,24), Location = new Point(8,40) };
a.Click += (s,e) => MessageBox.Show("A clicked");
b.Click += (s,e) => MessageBox.Show("B clicked");
popup.Controls.Add(a);
popup.Controls.Add(b);
this.Controls.Add(popup);
// toggle from your trigger button
triggerButton.Click += (s,e) => {
popup.Location = new Point(triggerButton.Left, triggerButton.Bottom);
popup.Visible = !popup.Visible;
if (popup.Visible) popup.BringToFront();
}; If you want a menu-like appearance use ContextMenuStrip instead:
var cms = new ContextMenuStrip();
cms.Items.Add("Option 1", null, (s,e) => DoOption1());
cms.Items.Add("Option 2", null, (s,e) => DoOption2());
triggerButton.Click += (s,e) => cms.Show(triggerButton, new Point(0, triggerButton.Height)); Troubleshooting tips: ensure the popup is added to the form Controls, set Location so it is not off-screen, verify Visible is true when expected, call BringToFront if it gets covered, and confirm the click handler is actually wired. For keyboard/accessibility or more complex layouts consider a FlowLayoutPanel or a custom UserControl that visually behaves like a button but hosts child controls.
Jump to Post— kvprajapati 1,826Sure, you can do this.
private void Form1_Load(object sender, EventArgs e) { Button b1 = new Button(); b1.Size = new Size(200, 200); Button b2 = new Button(); // Adding event b2.Click += new EventHandler(test); b1.Controls.Add(b2); Controls.Add(b1); } void test(object sender, EventArgs e) { MessageBox.Show("hi"); }
Sure, you can do this.
private void Form1_Load(object sender, EventArgs e)
{
Button b1 = new Button();
b1.Size = new Size(200, 200);
Button b2 = new Button();
// Adding event
b2.Click += new EventHandler(test);
b1.Controls.Add(b2);
Controls.Add(b1);
}
void test(object sender, EventArgs e)
{
MessageBox.Show("hi");
} Thanks for your help. i am new to c#. Can i ask how to add the code in? just copy and paste? i did this but it cant seem to work.
Sure, you can do this.
private void Form1_Load(object sender, EventArgs e) { Button b1 = new Button(); b1.Size = new Size(200, 200); Button b2 = new Button(); // Adding event b2.Click += new EventHandler(test); b1.Controls.Add(b2); Controls.Add(b1); } void test(object sender, EventArgs e) { MessageBox.Show("hi"); }
Thanks for your help. i am new to c#. Can i ask how to add the code in? just copy and paste? i did this but it cant seem to work.
Double click at Form GUI and paste the code following code in Form1_Load event.
Button b1 = new Button();
b1.Size = new Size(200, 200);
Button b2 = new Button();
// Adding event
b2.Click += new EventHandler(test);
b1.Controls.Add(b2);
Controls.Add(b1); And also Paste the definition of test() Method outside the Form1_Load event.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.