When I'm trying to add a drop-down menu to my form in a wizard, it gives following error.
Here are the code lines I have written for drop-down menu.
dropDownMenu1 = new ToolStripDropDownMenu();
dropDownMenu1.Location = new System.Drawing.Point(90, 45);
dropDownMenu1.Size = new System.Drawing.Size(70, 20);
this.Controls.Add(dropDownMenu1);
Update
This solution didn't fix my problem. I have used following code already. But still gives the same error.
UserInputForm inputForm= new UserInputForm();
inputForm.TopLevel = false;
inputForm.ShowDialog();
On the basis of your code,you also have to set Items for dropDownMenu1 and set it to display after you set dropDownMenu1.TopLevel = false;. You could try to refer to the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ToolStripDropDownMenu dropDownMenu1 = null;
private void Form1_Load(object sender, EventArgs e)
{
dropDownMenu1 = new ToolStripDropDownMenu();
dropDownMenu1.Items.Add("item1");
dropDownMenu1.Items.Add("item2");
dropDownMenu1.Items.Add("item3");
dropDownMenu1.Location = new Point(90, 45);
dropDownMenu1.Size = new System.Drawing.Size(70, 20);
dropDownMenu1.TopLevel = false;
this.Controls.Add(dropDownMenu1);
}
protected override void OnSizeChanged(EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
//_formContextMenu or this.contextMenuStrip1
dropDownMenu1.Visible = true;
dropDownMenu1.Close();
}
base.OnSizeChanged(e);
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
dropDownMenu1.Show(e.Location);
}
}
If there is not specific goal for using ToolStripDropDownMenu you can also use ContextMenuStrip component and Set ContextMenuStrip property of form. This way you don't need to write code to show context menu.
For more usage methods of ToolStripDropDownMenu, you can refer to ToolStripDropDownMenu Class and ToolStripDropDown Class.