I have the label1 (show 1 value) that has properties
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(554, 636);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(140, 155);
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
It is added in the main form as bellow code
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
//this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
It worked well. However, if I add one more code as
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
I want to auto change location of label 1 such as it still located in the yellow region. Is it possible in C#?
In visual studio labels are by default Anchored to Top and Left. this causes the problem when you maximize your form.
try this line of code
this.label1.Anchor = AnchorStyles.None;
Another way to do this (From Designer)
Click on your label and then Press F4, properties window will appear.
See Anchor Property.
You can change it to NONE. it would solve your problem.
Related
I made custom user control, and i want to place button in that control, with the width same as parent , so that should look like that
I can do that in design mode, simply drag and it looks nice, but i need to do that in code .
This is userControl code in designer.cs
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.button1);
this.Name = "ListBoxWithButton";
this.Size = new System.Drawing.Size(339, 362);
this.ResumeLayout(false);
And this is button code
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = this.Size;
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
With the mark on this line
this.button1.Size = this.Size;
when i type that, i get this:
Why?
there is Anchor property for every control in winform.
this property takes care to resize control properly with resizing it's parent control.
as I can see you want your button's width to be reset with resizing of form, you can Anchor your button to Left and Right.
But make sure you are setting this property from Property window and not from designer.cs file.
Once you do that, in your designer.cs file you can see code like below
this.button1.Anchor = (AnchorStyles.Left| AnchorStyles.Right);
this will adjust button from left and right only, if you want it to be adjusted from Top or Button, you should add that too.
But if you want your button to be docked at specific spot on form or to be Filled in parent form, you should consider Dock property too. this property too can be edited from property tab.
it would look like this (in designer.cs)
this.button1.Dock = DockStyle.Fill;
I use below code to make lightbox effect and it works as i expected. However if i move the parent form it still pop-ups on the center of the screen.
// Execute this code from parent form
Form f = new Form();
f.ShowInTaskbar = false;
f.BackColor = Color.Black;
f.Size = this.Size;
f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f.StartPosition = this.StartPosition;
f.Opacity = 0.6;
f.Show();
So i changed the above code like below;
f.StartPosition = FormStartPosition.CenterParent;
However it still doesn't pop-up center of the parent form.
Also i tried below, It didn't work too;
f.SetBounds(this.Location.X, this.Location.Y,this.Width, this.Height);
I already tried the solutions here;
Show a child form in the centre of Parent form in C#
They also didn't work.
What i want to do is, creating a second form with the same size and same location.
Just changing this line;
f.Show();
to this line, It worked;
f.ShowDialog();
I am new at using Visual Studio as well as C#. I'm having a hard time trying to figure out how to make a output label. Does anyone know how to do this?
What I've currently been doing is dragging the label control to the text box. Then, I would delete the text from the label but then it looks like it disappeared .
first of all: this is broad question! I'll just answer according to my understanding.
Define the AutoSize of your label to False, then you can erase its text (label1.Text = String.Empty) without the label being seen as if it disappeared
I think, this is what you want,
System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
label1.Location = new System.Drawing.Point(34, 49);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(85, 23);
label1.TabIndex = 0;
label1.Text = "label1";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
I have a form with a tableLayoutPanel. It has 1 column and 2 rows with 2 controls. A label and a listView. In design mode I cant set the label height larger than 17. And the listView I can manualy enlarge but not shrink. The controls dimensions remain larger that the form itself. So either the controls are cliped or I end up with the forms scrollbars. So why the controls autosize larger than the form? And when I run the app they dont shrink to minSize either.
//
// labelTitle
//
resources.ApplyResources(this.labelTitle, "labelTitle");
this.labelTitle.ForeColor = System.Drawing.Color.DeepSkyBlue;
this.labelTitle.Name = "labelTitle";
//
// tableLayoutPanel
//
resources.ApplyResources(this.tableLayoutPanel, "tableLayoutPanel");
this.tableLayoutPanel.Controls.Add(this.labelTitle, 0, 0);
this.tableLayoutPanel.Controls.Add(this.aListView, 0, 1);
this.tableLayoutPanel.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.FixedSize;
this.tableLayoutPanel.Name = "tableLayoutPanel";
//
// aListView
//
resources.ApplyResources(this.aListView, "aListView");
this.aListView.AllowDrop = true;
this.aListView.BackColor = System.Drawing.SystemColors.Desktop;
this.aListView.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.aListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.aListView.LargeImageList = this.coverImageList;
this.aListView.MultiSelect = false;
this.aListView.Name = "aListView";
this.aListView.ShowGroups = false;
this.aListView.ShowItemToolTips = true;
this.aListView.TileSize = new System.Drawing.Size(200, 200);
this.aListView.UseCompatibleStateImageBehavior = false;
this.aListView.View = System.Windows.Forms.View.Tile;
//
// form
//
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Desktop;
this.Controls.Add(this.tableLayoutPanel);
this.Name = "Form";
this.albumsContextMenu.ResumeLayout(false);
this.tableLayoutPanel.ResumeLayout(false);
this.ResumeLayout(false);
Instead of docking the controls I anchored them. Now I can change their size in design mode if I start with the top control first.
A Label has AutoSize = true by default, which resizes the control based on content and ignores your manual settings.
Before seeing the effect of you changing the size, you must disable the AutoSize property.
label1.AutoSize = false;
label1.Height = 50;
(Note that you can set the Height first, but you won't see the effect until you disable AutoSize.)
For the second question, you'll have to elaborate on what you mean by "the listView only grows in size".
I'm having an issue with a Windows Forms project. I change tabs but the change does not reflect unless I maximize,minimize,or restore the entire window. After that the newly selected tab will show it's child contents.
I've narrowed the issue down to my trying to programmatic create and name a datagridview
I can do this:
logs_datagrid.Name = "datagrid_logs";
logs_datagrid.AutoSize = true;
logs_datagrid.Dock = DockStyle.Fill;
logs_datagrid.Font = new Font("Microsoft Sans Serif", 8.25F);
logs_datagrid.DataSource = dt_logs_google;
logs_datagrid.AllowUserToAddRows = false;
logs_datagrid.BackColor = System.Drawing.Color.White;
logs_datagrid.BringToFront();
splitContainer2.Panel2.Controls.Add(logs_datagrid);
but as soon as I try to programatically apply edits to the datagridview columns the issue occurs. Just uncommenting the top line here will cause the error.
DataGridViewColumn dvg_col_1 = logs_datagrid.Columns[0];
// DataGridViewColumn dgv_col_2 = logs_datagrid.Columns[1];
// DataGridViewColumn dgv_col_3 = logs_datagrid.Columns[3];
//dgv_col_1.ReadOnly = true;
// dgv_col_1.MinimumWidth = 200;
//dgv_col_2.ReadOnly = true;
//dgv_col_2.MinimumWidth = 200;
//dgv_col_3.ReadOnly = true;
//dgv_col_3.MinimumWidth = 200;
// dgv_col_3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
[edit] Thank you for the help!
Perhaps Update();.
(Didn't check this case, but I've seen Update(); work, when Invalidate(); didn't.)
Try this.Invalidate() of your control
http://windowsclient.net/articles/windowsformspainting.aspx