Why does this code generate a CS1525 ("invalid expression") compiler error? - c#

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);
}

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());
}
}

Add the same string from textbox in two lines listbox?

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);
}

Combobox selectedValue crashes ControlBox in C#

When ever i assign the selected value from my combo box my Winforms application wont close via the controlBox (Minimise, Maximise work but close does not!)
If i comment out the following code it seems to work:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSet1.NewSelectCommand' table. You can move, or remove it, as needed.
this.newSelectCommandTableAdapter.Fill(this.dataSet1.NewSelectCommand);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string databaseName = string.Empty;
databaseName = comboBox1.SelectedItem.ToString();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string databaseName = string.Empty;
if(comboBox1.SelectedItem != null) databaseName = comboBox1.SelectedItem.ToString();
}

C# Write in a TextBox and preview in another

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}" />

Displaying corresponding text relating to check box names in Word 2010 and visual Studio 2010 that is removed when box is unchecked?

The code im currently using in Visual Studio to populate a text field when check boxes are clicked on is:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.displayText.Text += "Internal Use";
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
this.displayText.Text += "Company Confidential";
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
this.displayText.Text += "Customer Confidential";
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
this.displayText.Text += "Strictly Confidential";
}
This is fine but i'd like the text to be removed when the boxes are not checked. Does anybody have any insight into the specific code that I might need to make this requirement functional?
Many thanks in advance.
Do you mean something like
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
this.displayText.Text = "Internal Use";
else
this.displayText.Text = String.Empty;
}
And are you sure this isn't C# you're talking about?

Categories