How to create a ToolStripDropDownButton? - c#

I want to create a ToolStripDropDownButton which looks like the below image
But when I tried to search for ToolStripDropDownButton control in the Toolbox I was unable to find it because, after some googling, I found out that it is a class not namespace.
Then I googled out the code below
ToolStripDropDownButton dropDownButton1 = new ToolStripDropDownButton();
ToolStripDropDown dropDown = new ToolStripDropDown();
dropDownButton1.Text = "A";
dropDownButton1.DropDown = dropDown;
dropDownButton1.Height = 200;
dropDownButton1.Width = 200;
Set the drop-down direction.
dropDownButton1.DropDownDirection = ToolStripDropDownDirection.Left;
// Do not show a drop-down arrow.
dropDownButton1.ShowDropDownArrow = false;
Controls.Add((Control)dropDownButton1); //Doesn't work
But the last line of code is not valid and gives runtime error
Cannot convert type 'System.Windows.Forms.ToolStripDropDownButton' to
'System.Windows.Forms.Control'
Can someone tell me how to add such a button in C# Windows Form or what am I missing in the code?
Platform : VS2008 Express (i know it's old)

The error message says that ToolStripDropDownButton is not a Control and thus, you can't add it to your form directly.
ToolStripDropDownButton and other tool strip items only work as a part of a ToolStrip. So, you need to put a toolstrip on your form, then you can add items to it.
Code example, if you want to do it programmatically:
ToolStrip toolStrip = new System.Windows.Forms.ToolStrip();
toolStrip.Items.Add(dropDownButton1);
Controls.Add(toolStrip);
It looks like your code comes from ShowDropDownArrow example on MSDN. Check out the complete code.
Also, you can do it in a form designer in Visual Studio. Look for ToolStrip in a toolbox.
Relevant links:
MSDN: How to: Create a Basic Windows Forms ToolStrip with Standard Items
Using the Designer
MSDN: How to: Add ToolStrip Items Dynamically
C# Corner: ToolStrip in C#
StackOverflow: Windows.Forms button with drop-down menu - discussion of alternative ways to implement this kind of control. One of the answers suggests to use a standalone ToolStrip.

Related

How to create tab pages content dynamically in windows forms?

I am using windows form to build an app that draws the form controls based on the connected device dynamically. So I have a tab control and when the user select tab3 for instance the tab page content will be drawing based on connected device for example add two text boxes and a button. How can I do this. I would like also to know how to position those controls after they are created.
private void tabPage3_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
this.tabPage3.Controls.Add(text);
}
As you just stated, you create your Controls like in your example. Positioning is achieved by the Left and Top Properties of your freshly created control. BUT, my advise is, it will be easier to use predefined UserControls and add them dynamically, because I think you don't have nearly unlimited types of devices.
If you are curious how Visual Studio Designer is creating those code, just look up Designer.cs in InitializeComponent()

Winforms button properties

I am currently using windows forms application in visual studio. I want to change button and textbox properties programmatically and not use the the properties tab. How do i do this? Is there a way to access the code of the UI of that button/Textbox after it is changed in the properties tab?
Of course you can change that programmatically. If you have for example a button called btnStart, then you have in your form access to all properties:
btnStart.Text = "start";
Have also a look at: Change properties of programmatically created buttons
EDIT:
If you change it programmatically after the InitializeComponent(); it will override changed properties set manually in the properties tab.
Yes. And in fact it's always done in code -- Properties window (i.e. VS winforms designer) just writes some code for you. You can see that code when you delve into the InitializaComponent() method call in the form's constructor (right click InitializeComporent and select "Go to definition").
Anytime after this InitializeComponent() call, you can add code to change what you want:
public Form1()
{
InitializeComponent();
button1.Text = "Go!";
}

How to add link Labels at run time in Windows form

I have been making a Windows Form Application in C# using the Visual C# 2008 IDE.
There are basically two forms in my application. One is created at Runtime and it's layout is undefined and the second one's predefined.
Now, I have been adding form elements using the toolbox provided and don't have any idea how to add them using written code(not using toolbox). I want to add n number of Labels to the second form which is undefined. n can be anything(decided at runtime, depending on the user's input). Can anybody tell me what is the efficient way to do this?
Just a quick example of a "dynamic control" being created at run-time and added to the form:
Label lbl = new Label();
lbl.Text = "Hello World!";
lbl.Location = new Point(100, 25);
this.Controls.Add(lbl);
You can replace "this" with the container to add it to, like "panel1" for instance. For containers that have their own layout engine, like a FlowLayoutPanel, then you wouldn't need to specify a Location().
Create a new LinkLabel(), set its properties (in particular, text and position), then add it to the Controls collection of your form or any panel.
You may also want to add event handlers, and store them somewhere (probably in a List<T>) so you can change or remove them later.
Create one in the designer, configure it's properties as you wish. Then go to the designer file, which name is like Form1.Desiner.cs, copy the code related to your LinkLabel (find everything with text search) and paste it where you wish :)

how to display form within another form when i click the menu item

i'm new to windows application.can anybody help me.here is my doubt.im having one parent form and it has four menu items. when i click any of one menu item ,it should display another form within that parent form itself. how to do it?
Thanks in advance
According to details you ve provided it seems that you need to use MDI Forms concept in your app. It s very easy to learn and refer to the following links:
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
How to open a form within a form?
Just include the code in the 2nd link within your menuitem_Click event...
Hope this helps...
There are several ways you could do it.
One simple way for a newcomer is to add the form to the parent form in the designer. Set the visible Property to false (in the properties) so it will not be shown at first when your program is run.
Then you can set the visible property to true when you handle the menu item clicking.
There are code ways to do it too at runtime etc.
Hers an article with stuff about adding controls (and implicitly child forms) at runtime.
Inside your main form, add a panel and then use the below method to display the child form.
private void InitChildForm(Form childForm, Panel parent)
{
childForm.TopLevel = false;
childForm.Parent = parent;
childForm.Dock = DockStyle.Fill;
childForm.Show();
parent.Parent = this;
parent.Dock = DockStyle.Fill;
}

keyboard shortcuts do not work when adding a form to a panel c#

I am writing an application where I have a form with a panel. I have noticed that when I add another form to the panel, that the added form's keyboard shortcuts stop working.
I am using the following code :
MainMenu m = new MainMenu();
m.TopLevel = false;
m.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
m.Dock = System.Windows.Forms.DockStyle.Fill;
pnl.Controls.Add(m);
m.Visible = true;
pnl.ResumeLayout();
Is there anyway to make the keyboard shortcuts work?
Regards
My first guess (and it's totally a guess) is that you need to pass the parent/owner when constructing the child object, rather than just assigning parent ... could you show us that part of your code?
Also, just glancing over your code, it seems strange to, for a MainMenu, set Dock to Fill....
Adding a form to a panel inside another form? That is not a supported scenario; frankly I'm surprised it doesn't throw an exception. A better way to do this would be to use UserControls.
I found out that it was due to focus issues.
I have since converted my forms to user controls and the problems have gone away.

Categories

Resources