How do I make a Output Label on Visual Studio C# - c#

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;

Related

How can I change location of label when using maximized window

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.

Get value from richtextbox and assign them to radio button in c#?

I'm working on program that will take the input from richtextboxes in a loop and then I want to assign the text that I get from user to radiobutton that will be in a groupbox. I successfully developed the form with richtextboxes in a loop but now I stuck how I will assign them to radio button in a loop? When I click button?? Please guide me in this regard?
a piece of code here
Label label1 = new Label();
label1.Size = new System.Drawing.Size(96, 24);
label1.Text = "Question" + _openCount++;
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Size = new System.Drawing.Size(600, 91);
richTextBox1.Text = "";
Label label2 = new Label();
label2.Size = new System.Drawing.Size(96, 24);
label2.Text = "Option1";
......................
now i want to put my code in this button how i further start my code???
void button1(object sender, EventArgs e)
{
}
Create a list of your radio buttons, then create a list of your rich textbox strings.
Enumerable.Zip these two lists together then foreach through that list.
foreach(var item in zippedList)
{
item.RadioButton.Text = item.RichTextBoxText.Text;
}
The syntax for this would be way off, but I hope you get the idea. This is also assuming that you have an equal number of radio buttons and lines of text. You'll need to take that into consideration and will have to modify the two lists before zipping them if they are different lengths.

When changing tab selections, child panels are not redrawn unless widnow screen is minimized, maximized or restored

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

Measuring a string vertically

I have a C# Winform application. In my form, I have a panel and in my panel, I have a label. The label gets created dynamically. I have the following code:
Label label1 = new Label();
label1.MaximumSize = new Size(400, 0);
label1.Location = new Point(posX, posY);
label1.Text = myText;
label1.AutoSize = true;
posY += 15;
Okay, everything is working. The text of the label automatically wraps after 400 pixels. The problem is, I need to create a second label, but how do I know what to set the the Location to? This new label need to be placed just below the first label and the first label might be 1 line long or 5 lines long. any help would be appreciated.
try to place your label within FlowLayoutPanel, set the FlowDirection to Top Down.
I would support the answer which provided by Int3, and another solution is to read the Height of label1 before set the Top of label2.
For example:
label2.Top = label1.Top + label1.Height + 10;
A GridLayout with some rows might be a solution

Adding and removing dynamic controls Windows Forms using C#

I have three Tabs in my Windows Forms form. Depending on the selected RadioButton in the TabPages[0], I added few dynamic controls on the relevant TabPage. On the Button_Click event the controls are added, but the problem is I'm not able to remove the dynamically added controls from the other (irrelevant) TabPage.
Here's my code:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
Where am I making that mistake?
First of all, tbMax's name is not "tbMax", but "txtBoxNoiseMax". So for one, it won't be able to find the TextBox on RemoveByKey.
You're making new controls each time.
As lc already mentioned:
You named your TextBox variable tbMax, but you gave it the name txtBoxNoiseMax. If you take a look into the description of RemoveByKey, you'll see it works on the Name property. So you should change
tbMax.Name = "txtBoxNoiseMax";
into
tbMax.Name = "tbMax";

Categories

Resources