C# controling a panel - c#

I'm using microsoft visual c# and making forms. I don't know if my vocabluary is correct but the idea is that when i create a panel using Panel Figure = new Panel ();
I cannot control it, i.e. change location. when I try
Point MoveLeft = Figure.Location;
MoveLeft.Offset(-25, 0);
Figure.Location = MoveLeft;
it requires already existing panel named "Figure" and my newly created panel doesn't respond to commands.
also is there any way that when I create a panel, it's an already existing panel i.e to create the same panel as panel "triangle"?

If you already have a Panel on your form called "triangle", you can simply reference it in code like this
triangle.Left = -25;
If you create a new Panel you need to add it to the Form's Controls list
Panel Figure = new Panel();
Form1.Controls.Add(Figure);
Form1.Location = new Point(-25, 0);

Related

C# Winforms multiple objects class problem

I'm creating a simple game using c# windows forms. I have a class named Enemy based on the PictureBox. I placed a Label named Display inside the class. Now I can easily put an Enemy on a form.
I tried am placing an Enemy inside a tab control, but I am not able to make this Label appear. I have already declared it, set the TabIndex, Location and the size.
How can I make the Label show on the screen?
class Enemy : PictureBox
{
public Label Display;
public void CreateDisplay()
{
Display = new Label();
Display.AutoSize = true;
Display.Location = new System.Drawing.Point(298, 120);
Display.Name = "test";
Display.Size = new System.Drawing.Size(35, 13);
Display.TabIndex = 3;
Display.Text = "test";
}
}
This is the class code. I call the CreateDisplay() function when the user clicks on the object.
The problem is the new Label is not part of the control tree on the form. That is, when painting the form .Net will start with the form itself, then look inside the form's .Controls collection, then for each control look inside their .Controls collections, and so on. Just because this Label is a member of the Enemy type does not make it part of that Controls collection. Moreover, a PictureBox does not really support adding child controls.
Instead, create a new UserControl or custom control based on a Panel. This new control should have both a PictureBox and a Label and childs of this new control. Do this right, and the PictureBox and Label will both be members of the appropriate Controls collection and show on the screen as expected.

UserControl inside Panel inside UserControl not working

I am having some trouble creating the template of my application :
I am making some dynamic parts of forms and I use a panel in which I include my User Control according to the choice of the user. The panel has the same size as the user control but it doesn't work at runtime. The panel in which the UC is included is inside another UC also.
I tried autoSize = true, fixed size and many things but I don't know why it goes like this. Any idea?
Some parts of my code to load the User Control :
UC_panel.Controls.Clear();
UC_ADDRESS uca = new UC_ADDRESS();
uca.Dock = DockStyle.Fill;
uca.AutoSize = true;
UC_panel.Controls.Add(uca);
My UserControl for the address
At runtime it goes bigger
Since you set
uca.Dock = DockStyle.Fill;
and also
uca.AutoSize = true;
(which is redundant)
it is possible that some other container has its AutoScaleMode set to Font and its font size is bigger.

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);

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)

Is it possible to add more than one panel in same location

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.)

Categories