How to append a link label to an existing label? - c#

I have a label inside a panel to display simple text. Now I have a link label that is added dynamically that displays some more information. How can I show this link label right next to the label text at run time? For example, the lable displays
A record is added.
I need to show with a link label "View Additional Details" next to the label text.
A record is added. View Additional Details
I have the code as below but it overlaps the existing label text. Thanks for any help!
LinkLabel details = new LinkLabel();
details.Text = "View Additional Details";
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = infoDetails;
details.Links.Add(link);
details.LinkClicked += new LinkLabelLinkClickedEventHandler(details_LinkClicked);
//Adding the link label control to the existing label control
lblInfo.Visible = true;
lblInfo.AutoSize = true;
lblInfo.Controls.Add(details);

Why are you trying to add a LinkLabel to a label? Add the LinkLabel to the same form as the label, and set the location of the LinkLabel appropriately.
In the example below, I'm assuming the code is being called from the form's class (or to a panel if you are using one). If not, replace this with your form instance. I'm setting the Y location as the same as lblInfo so the LinkLabel appears next to it. Adjust lblInfo.Margin.Right and details.Margin.Left as desired.
details.Margin.Left = 5;
details.Location = new Point(
lblInfo.Location.X + lblInfo.Width + lblInfo.Margin.Right + details.Margin.Left,
lblInfo.Location.Y
);
this.Controls.Add(details);
Update: changed padding to use Margin (thanks Anthony).

Related

Adding buttons programmatically to a TabControl (TabPage)

I read this topic (Adding buttons to a TabControl Tab in C#) but I don't figure out why my code below add one button only to the tabpage.
I've obviously debugged that the foreach works properly.
foreach (string line in File.ReadAllLines(#"C:\quicklauncher.ini"))
{
TabPage page = new TabPage(foldername);
DirectoryInfo d = new DirectoryInfo(line);
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button();
button.Text = file.Name;
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
}
tabControl.TabPages.Add(page); //add our tab page to the tab control
}
Thanks,
Steve
You thought it added only 1 button for you but in fact it did not, it added all the buttons for you but those buttons had the same Location (which is (0,0) by default). That's why you did think there was only 1 button (because you saw only 1 last button on top of others).
You added buttons automatically to your tabpage, so you should have some rule to locate them, I'm not sure what that rule is but I suppose you want to line them up vertically (just an example), I'm going to correct your code to achieve such a thing, at least you will see it work, and in fact all the buttons are added normally:
//you need some variable to save the next Top for each new button:
//let's call it nextTop:
int nextTop = 0;
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button { Top = nextTop,
Text = file.Name };
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
nextTop += button.Height + 5; //it's up to you on the
//Height and vertical spacing
}
//...
You can also try using some layout control like FlowLayoutPanel and TableLayoutPanel to contain all the buttons, they can help arrange your buttons in some way you may want, just try it.

How to retrieve text from a TextBox that was created programatically

Alright, so I have a form set up that contains a label and a button. When the button is pressed it creates several labels and and two textfields in a specific area.
I cannot for the life of me, figure out how to retrieve the text from those textfields and store it in a public string.
Any help would be wonderful and greatly appreciated.
Edit: As per request.
TextBox playertextbox = new TextBox();
playertextbox.Location = new Point(460, 200);
this.Controls.Add(playertextbox);
You can assign a name to the textbox and later use ControlCollection.Find to retrieve it
Try this
TextBox playertextbox = new TextBox();
playertextbox.Location = new Point(460, 200);
playertextbox.Name = "playertxtBox"; // Add some name
this.Controls.Add(playertextbox);
Then use the name in the button click handler or similar :
//Use that name to search here
TextBox playertextbox = ((TextBox) this.Controls.Find("playertxtBox",true)[0]);
string text = playertextbox.Text;

How to match default TabPage styles with programaticlaly created tab pages?

When inserting tabpages programmatically, the TabPages that are created do not have the same type of color-scheme as one would expect when using the designer. As you can see from the screenshot image below, the "Active" tab's background color has remained unchanged. How would I programmatically add TabPages to a tab-control so it would behave as if I had added them using the designer?
I have the following code that programmatically inserts tab-pages onto a tab control I have created using WinForms (it is in a foreach loop).
TabPage tPage = new TabPage();
tPage.Text = item.DisplayName + " Options";
FlowLayoutPanel flowPanel = new FlowLayoutPanel();
Label lblLocationField = new Label();
lblLocationField.Text = "Insert into location field:";
CheckBox chkLocationField = new CheckBox();
chkLocationField.Name = "locationField";
flowPanel.Controls.Add(lblLocationField);
flowPanel.Controls.Add(chkLocationField);
tPage.Controls.Add(flowPanel);
this.tabControlConfiguration.Controls.Add(tPage);
It turns out, a new instance of TabPage sets the UseVisualStyleBackColor property to false by default, whereas the designer sets it to "true."
Therefore, by simply adding the one line below to my code, I was able to get it to render properly!
tPage.UseVisualStyleBackColor = true;

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

how to change the name of the tabcontrol

I am using a Tab Control in a C# WinForms application. I want to change the title of the tabs. By default they are tabPage1, tabPage2, etc.
A lazy way to do it without code:
Select the tab control
Go to properties, use F4 to do so
Select TabPages property, use F4 to do so
Click the ... after the (Collection).
Edit the name and any tabPage properties associated with it.
You can change it pretty simply in the designer; click on a blank area in the tab page contents and use the property view to set the Text property. Also through code via:
tabPage1.Text = #"Something Meaningful";
Where tabPage1 would be the reference to whichever TabPage you wanted to set the Text of.
tabCtrl.TabPages[0].Text = "some text";
tabCtrl.TabPages[1].Text = "some other text";
Going into the designer.cs you can change the tab Text like so this.tabPage3.Text = 'Your Title'
Method
//Component Designer generated code
private void InitializeComponent()
{
...
this.tabControl1 = new System.Windows.Forms.TabControl();
...
//
// tabPage3
//
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
...
this.tabPage3.Text = "tabPage3";//change the tab name
...
}
It will auto refresh the Design view.

Categories

Resources