I am making a windows from using c# and am stumped as to how to count a button click. Any pointers or information would be appreciated.

Create a private int within your Form1 class (or could be public, if you needed that) and increment it in your button_Click method.

Edited 6 Years Ago by jonsca: n/a

Thanks but now i am facing another problem, i am trying to find out what file the user opened and save the path so that on the next click the user does not have to pick where to save the file, it just writes it to the file he opened. But i am getting an error "use of unassigned local variable 'path'".

private void Open_File_Click(object sender, EventArgs e)
        {
            clickcounter++; //to count button clicks

            //Settings of open file dialog
            OpenFileDialog ofDialog = new OpenFileDialog();
            ofDialog.Title = "Open Text File";
            ofDialog.Filter = "Text Files|*.txt";
            ofDialog.InitialDirectory = @"C:\";
            ofDialog.CheckFileExists = true;
            ofDialog.CheckPathExists = true;
            string path;

            if (clickcounter % 2 == 0) //If the number of clicks is even open the file and output to textbox
            {
                if (ofDialog.ShowDialog() == DialogResult.OK)
                {
                    if (Open_File.Text == "Open File")
                    {
                        Open_File.Text = "Save File";
                    }
                    StreamReader stream1 = File.OpenText(ofDialog.FileName);
                    StreamReader stream2 = File.OpenText(ofDialog.FileName);
                    path = stream2.ToString();

                    string string1 = stream1.ReadLine();
                    StringBuilder stringbuild1 = new StringBuilder();
                    while (string1 != null)
                    {
                        stringbuild1.Append(string1);
                        string1 = stream1.ReadLine();
                    }
                    stream1.Close();
                    textBox1.Text = stringbuild1.ToString();
                }
            }

            else //If it is not even do the following on the next click
            {
                if (Open_File.Text == "Save File")
                {
                    Open_File.Text = "Open File";
                }
                File.WriteAllText(path, textBox1.Text);
            }
            
            
        }

Since not all routes through your if/else structure assign "path" a value (as your dialog result could theoretically be another value and there is not an else clause for that if), path go unassigned and have no value. In addition, the compiler is complaining that if your clickcounter is odd you'll have an empty value for path on line 44.

Some rearrangement of the structure is necessary as initializing the value will only serve to keep the compiler at bay and cause more problems later.

Edited 6 Years Ago by jonsca: n/a

You do it a bad way. If you really want them to end up in the same location, which I think Windows does automatically, go into your program settings, add a setting called defaultFile. The code would be:

OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = Properties.Settings.Default.defaultFile;
if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    Properties.Settings.Default.defaultFile =
        System.IO.Path.GetDirectoryName(openFile.FileName);
    // Do your code here
}

thanks bbman but what is the .defualFile at the end of the line here? Is there a using i need to add?

openFile.InitialDirectory = Properties.Settings.Default.defaultFile;

Edited 6 Years Ago by BlackNinja: n/a

All i really need now is a way to grab the path of the file from StreamReader stream1 = File.OpenText(ofDialog.FileName); or where ever it would be most convenient and pass it back as a string to the variable path. Any help would be appreciated.

You need to go to Project/YourProject Properties/Settings and add in the defaultFile setting. bbman has it holding the directory name instead so you can skip his line 2 and change his line 5 to Properties.Settings.Default.defaultFile = openFile.FileName; . Now you can use the filename whenever you need it.

You may need to redo some of the code in your routine as stream2.ToString() is only going to give you "System.IO.StreamReader" as a result.

All that being said...

All i really need now is a way to grab the path of the file from StreamReader stream1 = File.OpenText(ofDialog.FileName); or where ever it would be most convenient and pass it back as a string to the variable path. Any help would be appreciated.

If you have ofDialog.FileName just save it as "path" (or using a default setting as above) and then pass "path" to File.OpenText . Just make sure it's getting assigned before it is being used.

thanks bbman but what is the .defualFile at the end of the line here? Is there a using i need to add?

openFile.InitialDirectory = Properties.Settings.Default.defaultFile;

No no. Go into your project settings, and there is a tab called "settings". Add a string called defaultFile.

This article has been dead for over six months. Start a new discussion instead.