Is it possible to add the same string typed in the texbox in the two lines of the listbox, as shown in the image below?
private void metroButton10_Click(object sender, EventArgs e)
{
listBox2.Items.Add("Next station: " + metroTextBox1.Text);
listBox2.Items.Add(Environment.NewLine)
metroTextBox1.Clear();
listBox2.SelectedIndex = 0;
}
Use the following code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Next Station: "+textBox1.Text);
listBox1.Items.Add("Station: " + textBox1.Text);
}
You Can't use new line. Because listbox can contain one item in a line.
Sure you can add same string typed in the textbox in the two lines of the listbox as;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("NextStation: " + textBox1.Text);
listBox1.Items.Add("Station: " +textBox1.Text);
}
Related
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());
}
}
I need to accept more than one value from the same textbox and store it into an arrya . I need something close to the below code :
string[] countries = new string[3];
private void accept_Click(object sender, EventArgs e)
{
countries[0] = textBox1.Text+" 1 ";
countries[1] = textBox1.Text + " 2 ";
countries[2] = textBox1.Text + " 3 ";
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);}
}
}
How do you split the different countries?
If you split it on each space you can do it like this:
string[] countries = textBox1.Text.Split(null);
If that is not a good solution maybe try and explain the expected output.
You can simply use a multiline textbox and split the input on newlines:
var countries = countriesTextBox.Text.Split(Environment.NewLine);
Why not just use a list? The following code will add the country's name entered in the textbox to the List<string> every time you click the accept button.
var countries = new List<string>();
private void accept_Click(object sender, EventArgs e)
{
countries.Add(textBox1.Text);
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);
}
If you want to allow the user to enter multiple countries at once, the following will work if you put a comma in between each country name when entering them into the textbox:
var countries = new List<string>();
private void accept_Click(object sender, EventArgs e)
{
var input = textBox1.Text.Split(',');
countries.AddRange(input);
}
private void finish_Click(object sender, EventArgs e)
{
foreach (string coun in countries)
MessageBox.Show("You have entered " + coun);
}
I have a Textbox with MultiLine enabled, in my application this Textbox controller used to insert some text.
All I want to do is to jump to a new line if the user clicks enter.
All I have tried is to find the write command inside my controller Enter event:
private void tbc_Enter(object sender, EventArgs e)
{
}
Is this what you want?
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "This" + Environment.NewLine + "A" + Environment.NewLine + "Multiline" + Environment.NewLine + "Textbox.";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(Environment.NewLine);
textBox1.Focus();
}
I'd like to ask the following question: when I write in a TextBox, I want the text that I write to automatically be written to another TextBox (like a preview of what I write).
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text
}
...But if the textBox2 contains text, the following code is a disaster:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text += textBox1.Text
}
How can I insert and delete one letter at a time?
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
And yes textChanged is useful for this and exactly what you needed.
Update:
Define a variable for store your static text in textBox2:
string staticText = textBox2.Text;
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = staticText + textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
This assumes that the other TextBox is named textBox2 and that the first is named textBox1.
If you're working in WPF/XAML, you can use binding.
In your XAML:
<TextBox x:Name="textBox1" />
<TextBox Text="{Binding ElementName=textBox1, Path=Text}" />
I'm trying to make a program that, when a button is pressed, takes the words typed in the text box and adds it to a text file. This is what I have so far:
private void textBox1_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path, string());
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path, string());
}
The String keeps coming up with error code CS1525 ("invalid expression"). What am I doing wrong?
You will want to use the string from the TextBox.Text property
for example
File.WriteAllText(path, textBox1.Text);
or
File.WriteAllText(path, (sender as TextBox).Text);
And it sounds like you want to create a Button and assign a Click event and use that to save the Text from the TextBox to the file, and for that AppendAllText may be a better option.
private void button1_Click(object sender, EventArgs e)
{
File.AppendAllText(path, textBox1.Text);
}
Try this:-
using (StreamWriter sw1 = new StreamWriter("abc.txt"))
{
sw1.WriteLine(textBox1.Text);
}
or
private void textBox1_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path,textBox1.Text);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
File.WriteAllText(path,textBox2.Text);
}