Ok I tried several codes. I just can't figure out what to do here.. I am doing a quiz. I save the questions and answers like that.
private void btnsave_Click(object sender, EventArgs e)
{
//checking if question or answer textbox are empty. If they are not then the question is saved
if (txtquestion.Text != "" & txtanswer.Text != "")
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt", true))
{
file.WriteLine(txtquestion.Text);
}
//saves the answer in the answers text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\User\Desktop\Assignment 2 Solo part\Answers.txt", true))
{
file.WriteLine(txtanswer.Text);
}
MessageBox.Show("Question and Answer has been succesfully added in the Quiz!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.None);
//cleaning the textboxes for a new question and answer
txtanswer.Text = "";
txtquestion.Text = "";
}
//checks if the question textbox is empty and shows the corresponding message
else if (txtquestion.Text == "")
MessageBox.Show("Please enter a question", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else //checks if the answer textbox is empty and shows the corresponding message
if (txtanswer.Text == "")
MessageBox.Show("Please enter an answer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
until here everything is fine. The problem is after I delete a question there are empty lines in the file and I tried lots of stuff. I changed 2 times the way i save them. still I can make it work. Now I am also getting an out of bounds error when i delete a question because of the empty spaces that are created. Here is the delete code:
private void fmrdelete_Load(object sender, EventArgs e)
{//declaration
int i=0;
//loading values to array from file
string[] qlines = System.IO.File.ReadAllLines(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt").ToArray();
foreach (string line in qlines)
{
Console.WriteLine(line);
//saving each line to array possition
questions[i] = line;
i++;
lstboxquestions.Items.Add(line);
}
i = 0;
//loading values to array from file
string[] alines = System.IO.File.ReadAllLines(@"C:\Users\User\Desktop\Assignment 2 Solo part\Answers.txt").ToArray();
foreach (string line in alines)
{
Console.WriteLine(line);
//saving each line to array possition
answers[i] = line;
i++;
}
}
private void btndelete_Click(object sender, EventArgs e)
{
//declarations
int line_to_delete;
int count;
int i=0;
//changing the value given from textbox to integer and then saving it
line_to_delete = Convert.ToInt32(txtans.Text);
//checking to find the question we want to delete
for (count = 0; count < 20; count++)
{
if (count == (line_to_delete-1))
{
questions[count]= "";
}
}
//clearing file
System.IO.File.WriteAllText(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt",string.Empty);
//saving to file
for (count = 0; count < 20; count++)
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt", true))
{
file.WriteLine(questions[count]);
}
}
//clearing the listbox
lstboxquestions.Items.Add(" ");
//loading from file back to array
string[] qlines = System.IO.File.ReadAllLines(@"C:\Users\User\Desktop\Assignment 2 Solo part\Questions.txt").ToArray();
foreach (string line in qlines)
{
line.Replace("\n\n", "\n");
Console.WriteLine(line);
//saving each line to array possition
questions[i] = line;
i++;
lstboxquestions.Items.Add(line);
}
//checking to find the answers we want to delete
for (count = 0; count < 20; count++)
{
if (count == (line_to_delete - 1))
{
answers[count] = "";
}
}
//clearing file
System.IO.File.WriteAllText(@"C:\Users\User\Desktop\Assignment 2 Solo part\Answers.txt", string.Empty);
//saving to file
for (count = 0; count < 20; count++)
{
//saves the question in the questions text
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\User\Desktop\Assignment 2 Solo part\Answers.txt", true))
{
file.WriteLine(answers[count]);
}
}
string[] alines = System.IO.File.ReadAllLines(@"C:\Users\User\Desktop\Assignment 2 Solo part\Answers.txt").ToArray();
foreach (string line in alines)
{
Console.WriteLine(line);
//saving each line to array possition
answers[i] = line;
i++;
}
}
}
So how to remove the empty lines ?