I have a menustrip that I create using the forms designer. I then want to programmatically add a toolstrip toolbar UNDER the menustrip. The reason is that this toolbar is a set of tools that I add as a plugin to the main application. As such, when the plugin is loaded, I create the toolbar.
When I manually add the toolstrip to the form using the forms designer, the toolstrip is correctly positioned under the menustrip. However when I add it programmatically, it snaps to the topmost part of the form, above the menustrip. Here's the code I use to programmatically add the toolstrip:
stereoBar = new ToolStrip();
stereoBar.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Left);
//y location is set to 22, the width of the menustrip
stereoBar.Location = new System.Drawing.Point(0, 22);
stereoBar.Dock = DockStyle.Top;
stereoBar.Name = "StereoToolbar";
stereoBar.Text = "Stereo Plugin Toolbar";
stereoBar.ShowItemToolTips = true;
stereoBar.GripMargin = new Padding(2);
Controls.Add(stereoBar);
Is there anything simple I am missing here?
Thanks in advance!
As described also in this answer, when docking controls in a form the order in which you add them to the form matters; i.e. when adding multiple controls that all dock to the top, the last control that gets added will be the topmost.
So, because you add your toolstrip programmatically and your menu strip via designer, the designer code gets executed first thus having the menu strip always at the bottom.
I think there are three ways out of the dilemma:
First approach
As Hans Passant pointed out, the easiest way to get things into the right order would be to simply call
stereoBar.BringToFront();
right after you added it to the forms' controls.
Second approach
To circumvent this you could also also add the menu strip programmatically and do this after you added the tool strip.
Third approach
Another way out may be to add another container to the form (Panel or groupbox for example) via designer that also docks to the top that simply just functions as a placeholder where you add your tool strip to (so you do not add to the form directly anymore)
Related
i'm using telerik control.
So i want to ask,
In winforms application ,Is it possible to add more than one panel in same location and display one at a time just like show/hide property.
Make sure you have placed all panel control in same container or form. then you can use Visible property to show and hide panel. BringFront and SendToBack function will be used to bring panel on top or send it to back. If you have placed any panel in another panel then that will be disappeared when you Hide parent panel. So, Make sure all panels' parent control must be same. To determine the parent control simply select that panel and press escape key to select their parent.
private void LoadPanels()
{
panel1.Location = new Point(10,10);
panel2.Location = new Point(10,10);
panel3.Location = new Point(10,10);
panel4.Location = new Point(10,10);
panel5.Location = new Point(10,10);
VisiblePanel("panel1");
}
private void VisiblePanel(string panelName)
{
string[] panels = new string[]{"panel1","panel2","panel3","panel4","panel5"};
for (int i=0;i<panels.Length;i++)
this.Controls[panels[i]].Visible = (panels[i] == panelName);
this.Controls[panelName].BringToFront(); //Not required you can remove this line.
}
Here's a slightly different approach you might want to consider...
Are you wanting to be able to programmatically select the contents of a rectangular area at runtime, selecting among various controls to display? If so, you could use a custom TabControl which has its tabs (not the pages) hidden.
Then you can select which page is displayed by programmatically changing its SelectedIndex property at runtime.
Doing it like this means that your form editor will show a normal tab control, which allows you to much more easily add the content to each page - but at runtime the tabs will be hidden from the user; they will just see the contents of the currently selected page.
See Hans Passant's answer here for how to create such a custom tab control.
(However, you might also want to override the OnKeyDown for the custom tab control in order to ignore Ctrl-Tab.)
I have got a requirement to design a windows forms application using Visual Studio 2010.
According to the design I have to develop the application which contains a menu bar. On selecting of the menus from the Menu bar relevant forms should open. Now as per my requirement these menu forms should be displayed in the same parent Windows Form. Means everything should be in the single form Application.Nothing should be out of that.
The problem that I'm facing is that I don't know how to proceed with this. This is the first time I'm working on Windows Form Application leaving Web.
You are looking for developing an MDI application. MSDN article to guide you - http://msdn.microsoft.com/en-us/library/xyhh2e7e(v=vs.100).aspx
You could build a grid area on your form using a combination of split containers (horizontal and vertical).
Then in separate panels design each of your menu forms, with each panel visibility set to false.
When choosing a menu you would need to assign a parent (split container panel) to the menu form and set it to visible.
Step 1
Create a form and give the name it to "mdiMain" and set the property IsMdiContainer to true.
Add a MenuStrip control from the ToolBar and add some menues.
Step 2
Create another form and give the name it to "frmChild"
Step 3
Write some code in menu click event to display frmChild form in MDI Parent.
Dim frm As New frmChild()
frm.MdiParent = Me
frm.Show()
Now your application is ready. You can place your code and control in frmChild window form.
As an option in some cases you can use WebBrowser control and manipulate html code inside depending on the menu bar selection.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser%28v=vs.110%29.aspx
I would like to know how I could possibly modulate my views in an application. Let me explain.
Instead of building my view and adding all the components in one screen. I want to say put each panel in its own class / form and then have a main form where I can add and remove these 'modular' panels.
Is this possible and how would I go about doing it?
In Windows Forms there is the concept of an empty component called UserControl, that can be freely designed and added at any time to another component or form container. UserControls are used very often in order to create flexible and exchangable UI. You can create a UserControl item in Visual Studio like this:
Name the new control:
After that you can design your UI control:
When your are done with the design, compile your project/solution and go to the form where you want to add your newly designed control. In the toolbar panel you will see your new UserControl, which can be added to the form with drag & drop (with the mouse):
You can create as many UserControls as you want and add/remove them to/from your form.
All of this steps can be done completely in the code. In order to create new view of this kind, you need to create a new class that inherits the predefined UserControl class:
public class EditorUserControl : UserControl
{
}
Every Control element has a ControlsCollection that holds/contains components of type Control that are drawn when the UI is shown. In order to add your new control to the main panel you need to add it to the controls collection:
public partial class EditorUserControl : UserControl
{
public EditorUserControl()
{
var button = new Button();
button.Text = "Import";
this.Controls.Add(button);
}
}
Note, that when adding components manually, you are responsible for sizing and position them. Predefined layout panels can help you here:
TableLayoutPanel - layout with cells
SplitPanel - horizontal or vertical predefined resizable panels
etc.
Now all that left is to add the new user control to the main form just like you added the UI elements to your own control:
var simpleEditor = new EditorUserControl();
simpleEditor.Dock = DockStyle.Fill;
this.Controls.Add(simpleEditor);
You can adjust the UI control settings through its predefined properties.
You can mix predefined containers and UserControls in order to achieve the desired UI:
There are a lot of good beginners tutorials for C# and VS and .NET:
Channel9 tutorials
MSDN Visual Studio UI tutorials
Composite UserControl tutorial
Developing with Windows Forms Documentation and Examples
This is definitely possible. I will use WinForms but there are similar ways in WPF such as frames.
In WinForms you can create a new User Control for each 'modular' panel which will automatically create .cs and .designer.cs files just like in a normal Form. You can then add logic and functionality to the panels as if they were forms themselves. All that would then remain is to add the logic to the form to load the default panel on startup and think of ways of how other panels can be brought into view (e.g. a next button or having a panel on each tab in a tab control). Showing a panel in a form (or any other user control for that matter) is achieved by creating an instance of your desired panel and adding it to you form/control's Controls property like so:
public Form1()
{
InitializeComponent();
MyPanel panel = new MyPanel();
this.Controls.Add(panel);
}
I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?
I want to add a new tab page for every newly opened form.
Example:
frmReport reportform = new frmReport();
report.Show();
When I open the frmReport form, it must be opened in a new TabPage, as in Windows Internet Explorer 7-8 tabpages.
What you would like to achive here is to have "windows inside tab pages". This is not like it supposed to be! It looks like this:
Windows OS
Windows of applications (Window class)
Containers placed on Window (for example: Panel, TabControl!)
Controls placed on Windows and Containers (for example: Button, but also containers like Panel!)
So when you look on this you see that it's not ok to put Windows class into TabControl!
So what to do?
Create for example UserControl class and move all your controls from Window to this new UserControl. Next place on your Window TabControl nad on one of it's TabPages put this newly created UserControl.
In this way you'll have a good designed UI. Once again: You do not put Window on your TabPage!
In fact U can add Windows.Forms to TabPages you just need to do set
Form.TopLevel = false
and you can add it to any container be it TabPage or Panel
i used DevExpress Controls for tabPages.
Sorry, there is no direct way to do it. Better to go with Usercontrols instead of Forms and Add controls to the Tab Page.
See this link.