How can I delete from a text file in Winforms?
My aim is to delete a selected text from a text box and it can also be deleted in my text file.
Specifically, my need is that if the user delete the text from a text box it also be deleted from text file.
My code is:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(textBox1.Text);
textBox2.Text = sr.ReadToEnd();
sr.Close();
}
private void button3_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(textBox1.Text, true);
sw.WriteLine(textBox2.Text);
sw.Close();
}
private void button4_Click(object sender, EventArgs e)
{
textBox2.SelectedText = "";
string selectedText = "theTextYouWantToDelete";
string fileContent = File.ReadAllText(#"C:\demo\demo.txt");
File.WriteAllText(#"C:\demo\demo.txt",
fileContent.Replace(selectedText, ""));
}
private bool SelectedText(char arg)
{
throw new NotImplementedException();
}
}
As far as I understand it, you should simply be able to write
private void button4_Click(object sender, EventArgs e) {
File.WriteAllText(#"C:\demo.txt", textBox2.Text, ""));
}
This will replace the contents of your file with the current contents of the textbox. Assuming the user has already deleted the needed text from the textbox, it should work correctly.
See the documentation for File.WriteAllText fore more info.
This will replace the selectedText with an empty string
string selectedText = textBox2.Text;
string fileContent = File.ReadAllText(#"C:\demo.txt");
File.WriteAllText(#"C:\demo.txt", fileContent.Replace(selectedText, ""));
Related
I am creating a console application that will take names as an input and will store it in a text file and then i want to retrieve the names beginning with A.
The console application has two textboxes and two buttons. By hitting button1 the text entered in the textbox1 will be copied in the text file, and by clicking button2 U would like to retrieve the name beginning with A and display it in textbox2.
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("d:\\demo.txt");
txt.Write(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = File.ReadAllText("d:\\demo.txt");
}
I know how to retrieve the entire data of the text file by using
textBox2.Text = File.ReadAllText("d:\\demo.txt");
Please help me to retrieve the names beginning with A.
Change Write to WriteLine and then you can read each name as a separate line. When you do, check the first letter with StartsWith. If it starts with an a, append it to textbox2 using +=:
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("d:\\demo.txt");
txt.WriteLine(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
string line;
System.IO.StreamReader file = new System.IO.StreamReader(#"d:\\demo.txt");
while((line = file.ReadLine()) != null)
{
if (line.ToLower().StartsWith("a"))
{
textBox2.Text += " " + line;
}
}
}
You didn't provide required details but are you looking for something like that; (writing the texts as lines to a file and reading it)
private void button1_Click(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter(#"d:\\demo.txt", append: true);
txt.WriteLine(textBox1.Text);
txt.Close();
}
private void button2_Click(object sender, EventArgs e)
{
var lines = File.ReadAllLines(#"d:\\demo.txt").Where(x => x.StartsWith("A"));
textBox2.Text = string.Join(",", lines);
}
I am learning to program in C # so my question is how to call the method from the button3
Look for information on the web but it is not very clear to me why I turn to this site
private void button3_Click_1(object sender, EventArgs e)
{
}
private void export2File(ListView lv, string splitter)
{
string filename = "";
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "SaveFileDialog Export2File";
sfd.Filter = "Text File (.txt) | *.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
filename = sfd.FileName.ToString();
if (filename != "")
{
using (StreamWriter sw = new StreamWriter(filename))
{
foreach (ListViewItem item in lv.Items)
{
sw.WriteLine("{0}{1}{2}", item.SubItems[0].Text, splitter, item.SubItems[1].Text);
}
}
}
}
}
private void button3_Click_1(object sender, EventArgs e)
{
ListView listView1 = new ListView();
string splitter = ",";
export2File(listview1, splitter);
}
You need to pass a reference to the ListView on your Form, and the desired "splitter" into the method. Assuming listView1 and a comma:
private void button3_Click_1(object sender, EventArgs e)
{
export2File(listView1, ",");
}
I have a textbox. In that textbox I write Human. Then I click a button, and if the word is human, then on a richtextbox, the word human will appear.
Here's the code I've tried.
private void button3_Click(object sender, EventArgs e)
{
if (textBox4.Text)
{
richTextBox1.Text = "human";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
string value = textBox4.Text;
}
I tried making the textbox into a string so I could use it in the if statement, but it didn't work, so instead I used texbox4.text, but it is still wrong.
You could simply do with this piece of code,
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text =="human")
{
richTextBox1.Text = textBox1.Text;
}
}
I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";
I have this code but it's not working. I've tried several different versions but nothing is working. I'm a newbie and still don't understand everything.
OpenFileDialog filedialog = new OpenFileDialog();
private void button3_Click(object sender, EventArgs e)
{
filedialog.ShowDialog();
filedialog.FileOk += filedialog_FileOk;
}
void filedialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
using (StreamReader myStream = new StreamReader(filedialog.FileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = myStream.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}
I think there is a requirement for to much plain text in this editor.
You're adding the event handler after the call to ShowDialog() has returned. Move it to before and it might work.
According to the documentation, FileOK event occurs when Open or Save button is clicked.
You are attaching the event handler inside the click.
You might want to do it on page load or somewhere before the click occurs.
Eg :
OpenFileDialog filedialog = new OpenFileDialog();
protected void Page_Load(object sender, EventArgs e)
{
filedialog.FileOk += filedialog_FileOk;
}
private void button3_Click(object sender, EventArgs e)
{
filedialog.ShowDialog();
}
void filedialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
using (StreamReader myStream = new StreamReader(filedialog.FileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = myStream.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}