Label not visible above ToolStrip - c#

At runtime I add (and remove) several controls, as needed, to a main window which in Designer contains only a ToolStrip with some function buttons. In some cases I want to add an info label next to the toolStrip, but I cannot make it visible, ie. it is hidden below. The code for the label is straightforward
infoLabel = new Label();
infoLabel.AutoSize = true;
infoLabel.Location = new System.Drawing.Point(200, 10);
infoLabel.Size = new System.Drawing.Size(35, 13);
infoLabel.BackColor = System.Drawing.SystemColors.Control;
infoLabel.Font = new System.Drawing.Font("Arial", 13);
infoLabel.ForeColor = System.Drawing.Color.Black;
infoLabel.TabIndex = 1;
infoLabel.Text = "this is info";
infoLabel.BringToFront();
this.Controls.Add(infoLabel);
TabIndex and BringToFront I added as an act of desperation, it does not help. BTW the ToolStrip's TabIndex is 2, and its BackColor I changed to transparent.
However, when I placed a label over the ToolStrip in the Designer, it is visible (ie. on top). I analysed the code then but did not see anything different from what I am writing. What am I missing here?

I suggest calling infoLabel.BringToFront(); at the very end, at least after this.Controls.Add(infoLabel); you current code amended:
infoLabel = new Label();
...
infoLabel.Text = "this is info";
// First Add to this
this.Controls.Add(infoLabel);
// Only then we can make infoLabel be the topmost
// among all existing controls which are on this
infoLabel.BringToFront();
We create infoLabel, add it to this and finally make it topmost on this. To make code more readable I suggest something like this:
// Create a label on this
infoLabel = new Label() {
AutoSize = true,
Location = new System.Drawing.Point(200, 10),
Size = new System.Drawing.Size(35, 13),
BackColor = System.Drawing.SystemColors.Control,
Font = new System.Drawing.Font("Arial", 13),
ForeColor = System.Drawing.Color.Black,
TabIndex = 1,
Text = "this is info",
Parent = this // <- instead of this.Controls.Add(infoLabel);
};
// make infoLabel topmost among all controls on this
infoLabel.BringToFront();

Windows Forms controls do not have a property which you can use to set z-index of controls like one can do in CSS.
You'll need to call Parent.SetChildIndex(control, 0);. The control at the front of Controls collection is the topmost in z-order for a container control.

Related

is it possible to add elements like panel to a listbox, listview or other list with .items value using c# and winforms?

is it possible to add elements like panel to a listbox, listview or other list with .items?
I want to create panels with other elements like labels, checkbox, buttons,.. on a panel.
The panel should then be in a list so that I can check it for example: checkbox (which is on the panel) and is active in the list is showing, the other elements should be hidden in the list.
If a panel is between 2 panels, it should move upwards so that there is no space in between. If I then only display the panels of the non-activated elements, the hidden ones should be displayed again and the displayed ones should be hidden.
I would like to control this display using button event,
show everything,
only activated and
only deactivated.
With another button I would like to be able to bring a panel to the top position if it was declared as a favorite. if the favorite is removed again it should go back to where it was before.
In addition, I would then like to create a search mask that only displays the elements that match the search string when entered.
The only way I found is with listbox1.Controls.Add(panel1); for it to appear.
Unfortunately it doesn't work with listbox1.Items. :(
So I don't have a selectedItem either....
Here is my code that I have so far:
private void Reload_Click(object sender, EventArgs e)
{
Panel panel1 = new Panel();
panel1.Size = new Size (250, 35);
panel1.BackColor = Color.Red;
panel1.ForeColor = Color.Green;
panelxy.Dock = DockStyle.Top;
Panel panel2 = new Panel();
panel2.Size = new Size(250, 35);
panel2.BackColor = Color.Blue;
panel2.ForeColor = Color.Green;
panel2.Dock = DockStyle.Top;
listBox1.Controls.Add(panel1);
listBox1.Controls.Add(panel2);
Button btn_1 = new Button();
btn_1 .Size = new Size(200, 30);
btn_1 .Location = new Point(5, 2);
btn_1 .ForeColor = Color.Blue;
btn_1 .BackColor = Color.Yellow;
btn_1 .Font = new Font("Sitka Text", 15F, (FontStyle)(FontStyle.Bold | FontStyle.Italic), GraphicsUnit.Point, (byte) 0);
btn_1 .Text = "testbutton";
panel1.Controls.Add(btn_1 );
}
And here a Picture to show that:
listbox_elements
I hope someone can help me here. :)
thanks and BR
Cusy
Super thank you!
I just found out that it is possible to realize the required with a TableLayoutPanel. :)
Since I already have about 7000 lines of code, I don't want to convert it to WPF. ;)
i think the Answer from "Jimi" with FlowLayoutPanel are also a Solution. :)
Thanks
BR Cusy

How do I align controls in FlowLayoutPanel properly

I am currently trying to play around with c#. I want to create a simple GUI containing (as minimal example) three labels. The first two should be aligned from top to buttom and the third one to the right of these two button.
I have this in a panel, like
Label p_LabelX = new Label();
p_LabelX.Text = "I am to the right";
FlowLayoutPanel p_ButtonLayout = new FlowLayoutPanel();
p_ButtonLayout.Width = 200;
Label p_Label1 = new Label();
p_Label1.Text = "I am upper left";
Label p_Label2 = new Label();
p_Label2.Text = "I am lower left";
p_ButtonLayout.Controls.Add(p_Label1);
p_ButtonLayout.Controls.Add(p_Label2);
FlowLayoutPanel p_MainLayout = new FlowLayoutPanel();
p_MainLayout.FlowDirection = FlowDirection.LeftToRight;
p_MainLayout.WrapContents = false;
p_MainLayout.AutoScroll = true;
p_MainLayout.Controls.Add(p_ButtonLayout);
p_MainLayout.Controls.Add(p_LabelX);
this.Controls.Add(p_MainLayout);
With this code, it kind of works, but I have some scrollbars and the mainlayout is not fullscreen within the panel. However, if I skip the AutoScroll line, the scrollbars are gone and the layout seems to be fullscreen, but the right button is not visible.
Any suggestions?

Dynamically added labels disappear in runtime

I add labels to my form programmatically, but they disappear, except last one. I'm sure that given location to them is appropriate. But when the second label appears, first disappears, or when third label appears, second disappears.
Here is my code:
Label[] lenlab = new Label[255];
Label lab = new Label();
lab.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);
lab.ForeColor = Color.White;
lab.BackColor = Color.Transparent;
lab.AutoSize = true;
lenlab[1] = lab;
lenlab[1].Location = new Point(50, panel1.Location.Y + panel1.Height + 20);
lenlab[1].Text = c[1];
this.Controls.Add(lenlab[1]);
for (int i = 2; i < c.Count; i++)
{
lenlab[i] = lab;
lenlab[i].Location = new Point(lenlab[i - 1].Location.X + lenlab[i -1].Width + 40, lenlab[i - 1].Location.Y);
lenlab[i].Text = " + " + c[i];
this.Controls.Add(lenlab[i]);
}
This line is causing every position in your array to have a reference to the same Label you created originally, outside the loop, which means all you're doing is changing the position and text of the same Label inside your loop.
lenlab[i] = lab;
The behavior you're seeing is due to the fact that you can only add a particular control to this.Controls once, so the effect is that you see the same label changing position.
Here's the portion of the Add() method that checks whether the control you're adding already has a parent, and if it does, then it removes it from it's current parent before adding it to the new one. So every time you call this.Controls.Add() with the same Label, it removes it from the Form and then adds it again.
// Remove the new control from its old parent (if any)
if (value.parent != null) {
value.parent.Controls.Remove(value);
}
Instead, create a new Label inside your for loop:
lenlab[i] = new Label();
There are controls that can help you layout controls without the need to calculate a new position each time. In particular, read up on the FlowLayoutPanel and TableLayoutPanel classes.
What you are doing there is basically create one Label, change it several times, and attach it several times to the page. What you end up having on the page is the last version of the Label being added once, which is the expected behavior.
If you want to add several labels, you need to new each of them.

c# winforms dock fill tree-view make it disappear

I have a winforms application. In my application I have a user control which I loaded programmatically.
Inside this user-control I have tree view that also will be loaded with items programmatically. My problem is that I want to make my tree-view take the whole size of its parent.
What I have tried
I set the user-control Dock property to DockStyle.Fill to make it take the size of its parent.
I have done the same for the tree-view Dock property; set it to DockStyle.Fill.
What I get
The user-control takes the full size as expected but the tree-view looks like it is hidden. I checked the height, and I noticed it's 0. When I tried to change the height while it has DockStyle.Fill I can't, it changes back to 0.
Any ideas?
Update
The auto generated code for the tree-view:
private void InitializeComponent()
{
this.btnAddServer = new System.Windows.Forms.Button();
this.pnlServersContainer = new System.Windows.Forms.FlowLayoutPanel();
this.treeViewServers = new System.Windows.Forms.TreeView();
this.pnlServersContainer.SuspendLayout();
this.SuspendLayout();
//
// btnAddServer
//
this.btnAddServer.Location = new System.Drawing.Point(89, 478);
this.btnAddServer.Name = "btnAddServer";
this.btnAddServer.Size = new System.Drawing.Size(107, 23);
this.btnAddServer.TabIndex = 3;
this.btnAddServer.Text = "Add New Server";
this.btnAddServer.UseVisualStyleBackColor = true;
this.btnAddServer.Click += new System.EventHandler(this.btnAddServer_Click);
//
// pnlServersContainer
//
this.pnlServersContainer.AutoScroll = true;
this.pnlServersContainer.Controls.Add(this.treeViewServers);
this.pnlServersContainer.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlServersContainer.Location = new System.Drawing.Point(0, 0);
this.pnlServersContainer.Name = "pnlServersContainer";
this.pnlServersContainer.Padding = new System.Windows.Forms.Padding(8, 20, 0, 0);
this.pnlServersContainer.Size = new System.Drawing.Size(318, 463);
this.pnlServersContainer.TabIndex = 2;
//
// treeViewServers
//
this.treeViewServers.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeViewServers.Location = new System.Drawing.Point(11, 23);
this.treeViewServers.Name = "treeViewServers";
this.treeViewServers.Size = new System.Drawing.Size(275, 0);
this.treeViewServers.TabIndex = 0;
this.treeViewServers.DoubleClick += new System.EventHandler(this.treeViewServers_DoubleClick);
//
// ucServersList
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.btnAddServer);
this.Controls.Add(this.pnlServersContainer);
this.Name = "ucServersList";
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 60);
this.Size = new System.Drawing.Size(318, 523);
this.Load += new System.EventHandler(this.ucServersList_Load);
this.pnlServersContainer.ResumeLayout(false);
this.ResumeLayout(false);
}
I recommend to open Document outline editor in Visual Studio.
This shows all controls in their hierarchical order as tree.
It lets you also drag & drop the controls to the right place.
Open it with View > Other windows > Document outline.
You may fix your problem when looking at the controls order.
I have figured it out. but still don't know why this happened!
my tree-view was inside FlowLayoutPanel not Panel. When i changed it to Panel everything goes fine. that's it!
The problem might be that you have added several items to the same parent control, and then when you fill the parent dock with one of them, the behaviour would not be what you expect.
Use a splitcontainer. And when you want to fill out the dock, make sure your control belongs to two differnt panels of a splitcontainer.
See this for an concrete example.
Is there any specific reason why you use FlowLayoutPanel?
It seems that the FlowLayoutPanel does not deal with any other than Dock.None.
I think you should use a simple Panel for this application, because it does not resize the contained controls - the Dock property behaves as expected.
Replacing the FlowLayoutPanel with a Panel will fix your problem.
This is a super old question... but since there are no accepted answers I’ll give it a go.
This happened to me when my Control was set to autosize. Either removing autosize or specifying a minimum height could solve this issue.

C# Window Form Application, how to efficiently create a dynamic panel on a window form

I would like to create a few panel dynamically in my window form application. Each panel will consist of 3 labels and one text box and one button. Now I know I can hard code this all at once by declaring each variable every time, but it takes a lot of coding and it is obviously not efficient at all.
So my question is: Is there a way to create pre-define panel dynamically where each time a panel is created will have a predefined layout setup already. So all i need to do is to add a panel, its location and size every time, and all the content(like labels, text-box and button) inside the panel are already setup with their location associated with the panel itself. Do I really have to create a class just for that?
Thanks in advance for read and taking your time.
Create Windows Forms control or user control, see http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx
Create a user control, and place on it whatever you like (your labels). Expose public methods/properties of that control so you can control the contents of it. Place as many of those on the form as you like, they will all look and behave same.
Here is an example you can play with...
for (int i = 1; i < 5; i++)
{
var panel1 = new Panel() { Size = new Size(90, 80), Location = new Point(10, i * 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 20) });
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 40) });
panel1.Controls.Add(new Label() { Text = i.ToString(), Location = new Point(10, 60) });
Controls.Add(panel1);
}

Categories