how to create dynamic link labels in window form application - c#

On clicking Go button the name given in textbox should be displayed as a link label and this should increment dynamically.
Here my code:
private void buttongo_Click(object sender, EventArgs e)
{
linkLabelName.Text = textBoxName.Text;
}

private void btnGo_Click(object sender, EventArgs e)
{
LinkLabel link = new LinkLabel();
link.Text = txtText.Text;
panTable.Controls.Add(link);
}

Related

C# - How to load text from text file in a listbox into a richTextBox?

Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}

How do I store first control in flowLayoutPanel c#?

I am new to C# and I am using windows forms.
I have flowLayoutPanel and I programmatically add some buttons to it .
What I am trying to do is: I want to save the first button located in the flowLayoutPanel into ButtonToSave object.
flowLayoutPanel1.FlowDirection= FlowDirection.LeftToRight
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button ButtonToSave = new Button();
ButtonToSave = "First button in flowLayoutPanel1"
}
Anyone knows how to save the first button located in flowLayoutPanel1 into ButtonToSave when StoreTheFirstButton event is raised?
Thank you
Try code below. You may want to look at my response at following posting (How to (create and) add components to a Table Layout?) :
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += new EventHandler(StoreTheFirstButton_Click);
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
Button ButtonToSave = button;
//ButtonToSave = "First button in flowLayoutPanel1";
}

how to connect panel with link label in C# windows forms

I am trying to open a panel using link label in C# windows form
application.
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("panel2");
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
panel2.Show(); // Show the panel if exist
}
OR
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Panel panel2 = new Panel(); // Create the panel
this.Controls.Add(panel2); // Add to form
panel2.Show(); // Show the panel
}

C# write text to textbox which adds written text to linklabel and when linklabel clicked goes to url?

I'm trying to do the following: I write link to textbox and it displays in a linklabel and after I click linklabel it goes to that url written in it? Everything goes well but pressing linklabel doesn't go to url.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
label2.Text = textBox1.Text;
linkLabel1.Text = textBox2.Text;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.checkbox = checkBox1.Checked;
Properties.Settings.Default.textbox = textBox1.Text;
Properties.Settings.Default.label = label2.Text;
Properties.Settings.Default.linkLabel = linkLabel1.Text;
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkbox;
textBox1.Text = Properties.Settings.Default.textbox;
label2.Text = Properties.Settings.Default.label;
linkLabel1.Text = Properties.Settings.Default.linkLabel;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
}
You have to start a process with the text of the LinkLabel, which should be a valid URL. This code will open the URL in the default browser:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(linkLabel1.Text);
}
More info on the Process Class.
If the default browser is IE and you want to open it in Chrome for example you'll have to provide the necessary info:
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Users\<UserName>\AppData\Local\Google\Chrome\chrome.exe";
p.StartInfo.Arguments = linkLabel1.Text;
p.Start();
}
Of course you'll have to do validation to check if the text of the LinkLabel is a valid URL.
You should use following code to add a link to LinkLabel as:
// Add a link to the LinkLabel.
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = textbox2.Text;
linkLabel1.Links.Add(link);

Textbox in apsx.cs file won't update a changed text

I have a web application consisting of an aspx-file.
On page load two textboxes are filled with data (a "username" and a "password"). This works.
On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).
Why is that? And how can I tell my program to regard my changes?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
protected void Button_Speichern_Click(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
}
EDIT:
It works with this improvement:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
For further information, see answers below. Thanks everyone!
Try checking for a postback -
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.
Try this,
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
You are assiging the value to the textboxes on every page load instead of firt page load.
Change the Page_Load method to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}

Categories