-3

I have made a simple filehandler in C# and I have a problem with unsaved changes. When I've changed the text in my textfield, I want it to show with a * in the Formwindow name. I can't use a textfield1_TextChanged because then there would be lots of * in the formWindow name. My question is how can I make this happen and make sure that if my boolean "Changed" is true there can only be one *. If i save my boolean will be false and the * will be removed from my FormWindow name.

Don't mind the comments, they are in swedish and are for self explanation.

Boolean hej = false;
string filenamet;

public void öppnaaaa(){

    // här sätter jag stringen öppnafil till den filen som jag valt i öppna fil dialogen.
    string öppnafil = openFileDialog1.FileName; 

    // här använder jag ReadAlltext för att läsa av texten som ligger i filen.
    File.ReadAllText(öppnafil);

     // här sätter jag texten i textBox1 till texten som ligger i filnamnet / stringen öppnafil
     textBox1.Text = File.ReadAllText(öppnafil);

     // denna kod sätter fönsternamnet till filnamnets extension.
     this.Text = openFileDialog1.SafeFileName;

      // här sätter jag filnamnet som värde till min string filnamnet
      öppnafil = filenamet;
}

public void sparatill_fil()
{
        // sätter filnamnet till en variabel name
       string name = saveFileDialog1.FileName;

        // Skriv informationen i textbox1 till en fil med filnamnet från saveFileDialog1
        File.WriteAllText(name, textBox1.Text);

        // här sätter jag filnamnet som värde till min string filnamnet
         filenamet = name;

        // sätter fönsternamnet till fil extension.
        this.Text = System.IO.Path.GetFileNameWithoutExtension(saveFileDialog1.FileName);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    hej = true;  
}

private void button1_Click(object sender, EventArgs e)
{
    if (this.Text == "dok1*")
    {
        saveFileDialog1.ShowDialog();
    }
    else
    {
        File.WriteAllText(filenamet, textBox1.Text);
    }
}

private void button3_Click(object sender, EventArgs e)
{
 // Visar öppna fil dialog
    openFileDialog1.ShowDialog();

}

private void button2_Click(object sender, EventArgs e)
{
    this.Close();
}

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
    // metod anrop till metoden sparatill_fil()
    sparatill_fil();
}


private void SparaSom_Click(object sender, EventArgs e)
{
    // När en användare klickar på denna kommer en SparaFil dialog ruta öppnas.
    saveFileDialog1.ShowDialog();
}

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
   // metod anrop till metoden öppna() 
    öppnaaaa();

}

private void saveFileDialog2_FileOk(object sender, CancelEventArgs e)
{
    sparatill_fil();
}

private void Nydok_Click(object sender, EventArgs e)
{
    textBox1.Text = " ";
    this.Text = "dok1";
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
  • 1
    Why not use the TextChanged event to put the * next to the textbox and also check if it's already there. If it is, don't add another one. Just leave it. Then when the form is saved remove the * if it is there. – eddie_cat Jun 09 '14 at 21:21
  • Note: I edited your post heavily to improve formatting and remove unnecessary code. Proper formatting and sentence structure can go a long way in helping others help you. – John Koerner Jun 10 '14 at 01:27

1 Answers1

1

Instead of appending the * to the title each time in the textBox1_TextChanged, simply set the value to dok1*, also, when checking for the save state, check your boolean instead of comparing the text to dok1*, this will save you future headaches. So something like this:

private string Title = "dok1";
private void textBox1_TextChanged(object sender, EventArgs e)
{
    hej = true;
    this.Text = Title + "*";
}

Then to check the boolean, do this:

private void button1_Click(object sender, EventArgs e)
{
    if (hej)
    {
        saveFileDialog1.ShowDialog();
    }
    else
    {
        File.WriteAllText(filenamet, textBox1.Text);
    }

    // Reset after a save.
    hej = false;
    this.Text = Title;
}
John Koerner
  • 37,428
  • 8
  • 84
  • 134