Move a control to a splitcontainer runtime - c#

I have a custom control (a textbox) that is slow to replicate. I have have code that adds a new split container to the current form when the user clicks the button. Is there any way that I can move my existing control to the sub panels in the split container at run-time or would I have to make a new instance in the panel? Any help is appreciated. Thanks!!

you can add the TextBox to your SplitContainer like this:
Assuming the TextBox name is txt1, SplitContainer's name is spl1 and spl1 has a panel called panel1
spl1.Panel1.Controls.Add(txt1);
txt1.Location=new Point(10,10);

Related

Navbar control docking in split container panel1

I am creating new dynamic navigation panel without using GUI. I have Split Container consist of panel1 and panel2. And also creating new navigation bar.
I try to do Navigation bar control object should be docking style as fill in split container panel1.
SplitContainerControl splitContainerControl1 = new SplitContainerControl();
splitContainerControl1.Dock = System.Windows.Forms.DockStyle.Fill
NavBarControl navBar = new NavBarControl();
this.Controls.Add(navBar);
navBar.BringToFront();
navBar.Dock = DockStyle.Fill;
How do I fix this problem?
I have found another solution via different perspective and adapted to my code. here is the code if someone face this problem.
The idea is simple. just you need to add navbar in splitcontainercontrol panel property as follow;
splitContainerControl1.Panel1.Controls.Add(navBar);
splitContainerControl1.Panel1.Controls.SetChildIndex(navBar, 0);

Performance Issue : Adding ChildPanels in a Panel vertically taking a significant time

I am working on VSTO winforms for Outlook widget, my requirement is to add button in a child Panel and adding multiple child panel to a container Panel at runtime. The number of childPanel depends on the number of loop iteration. Right now rendering the child panels in the container Panel is taking a long time and slowing down the application. Pagination is part which I must implement but if the Iterations are less < 10 then also I am getting a late rendering. Please suggest me if there is a better way to do it or if you have a better alternate combination of controls which could give me the same look and feel. Thanks in advance.
foreach (var item in Items)
{
Button itemButton = new Button();
itemButton.Text = item.name;
itemButton.fieldType = item.type;
itemButton.fieldId = item.id;
itemButton.Click += itemHandler;
// add the button in the child panel
CustomItemPanel childItemPanel = new CustomItemPanel();
childItemPanel.Controls.Add(itemButton);
// add to the clicked container panel
containerPanel.Controls.Add(childItemPanel);
}
The reason why I want to add a panel in a panel is to add a RichTextBox control in the child panel on click of the button.
Below is the listing of child Panels containing Button in a container Panel :
When I click the button, I am adding a richTextBox in the childPanel of the clicked button as below :

c# toolstrip doesn't dock under menustrip

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)

Adding a Design time Panel to a TabPage at run time

I wish to have a Panel with the controls on it at design time but I want to add this panel to a desired tabPage of my TabControl at run time.
I wrote something like this, did not work : the panel does not show up in the tab page.
please help me.
panel2.Parent = tabGuy.TabPages[0];
tabGuy.SuspendLayout();
tabGuy.TabPages[0].Controls.Add(panel2);
tabGuy.ResumeLayout();
panel2.Show();
You probably need to set the coordinates of the Panel as well, or (better) panel2.Dock = DockStyle.Fill;
And you don't have to set the .Parent or call the .Show()

Create a new conrol after program running C#

For example,I click button and then on the form appears GroupBox with some controls inside.One more click-one more GroupBox.This may lasts to infinity.Please,tell me how to do that???
You can create a new control and add it to the group you want to:
Your_Control ctl = new Your_Control();
Your_Parent_Control.Controls.Add(ctl);
Link to Control Class:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx
Link to the controls property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx
You can create controls in code just like any other object. Looks something like this.
GroupBox box = new GroupBox();
this.Controls.Add(box); // assuming "this" is your parent form
You can add controls to your group box in the same way. All Control objects have a Controls collection for child controls. Although it doesn't really make sense for some of them.

Categories