Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm confused on how to get the button.text information, for instance if the button.text is 1 2 or 3. I'm lost here no matter what I try because I want to write each button's text in the buttonArray to a file.I think the issue may be I have the wrong code to do this in the saving part. A blank file is always created so I know it is wrong. How would I change my code to write the buttonArray to a text file? Any help is great

C#
private int _col;
      public int Columns
      {
          get
          {
              return _col;
          }
          set
          {
              _col = value;
          }

      }
      private int _row;
      public int Rows
      {
          get
          {
              return _row;
          }
          set
          {
              _row = value;
          }
      }


      MapProperties mapProp = new MapProperties();
      public MapForm(String rows, String cols)
      {
          InitializeComponent();
           _col = int.Parse(cols);
          _row = int.Parse(rows);
          Begin();



      }

      Button[,] buttonArray;

      public void Begin()
      {
         int width = groupBox1.Width;
          int height = groupBox1.Height;
          int bW = width / _col;
          int bH = height / _row;

          buttonArray = new Button[_row, _col];


          for (int i = 0; i < _row; i++)
          {
              for (int j = 0; j < _col; j++)
              {
                  Button button = new Button();
                  buttonArray[i, j] = button;
                  button.Width = bW;
                  button.Height = bH;
                  button.Left = j * bW;
                  button.Top = i * bH;
                  button.Tag = 0;

                  button.Click += new EventHandler(button_Click);

                  groupBox1.Controls.Add(button);







              }
           }
      }

      void button_Click(object sender, EventArgs e)
      {
          Button button = (Button)sender;
          int count = (int)button.Tag;

             if ( count < 3)
             {
                 count++;
              if (count == 1)
              {

                  button.Text = "1";
                  button.BackColor = Color.LightBlue;

              }

              if (count == 2)
              {
                  button.Text = "2";
                  button.BackColor = Color.LightSlateGray;
              }
              if (count == 3)
              {
                  button.Text = "3";
                  button.BackColor = Color.LightSeaGreen;

              }
              if(count >= 3)
                 {
                     count = 0; //reset to 0 to restart if loop
                 }
             }

              button.Tag = count;  //store count to tag
          }

      private void Save_Click(object sender, EventArgs e) //save button event
      {
          Stream myStream;
          SaveFileDialog saveFileDialog1 = new SaveFileDialog();

          saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
          saveFileDialog1.FilterIndex = 2;
          saveFileDialog1.RestoreDirectory = true;

          if (saveFileDialog1.ShowDialog() == DialogResult.OK)
          {
              if ((myStream = saveFileDialog1.OpenFile()) != null)
              {


                  StringBuilder sb = new StringBuilder();

                  for (int i = 0; i < _col; i++)
                  {
                      for (int j = 0; j < _row; j++)
                      {
                          sb.Append(buttonArray[j, i].Text);
                          sb.Append(",");
                      }

                  }
                  sb.ToString();

                  myStream.Close();
              }
          }

      }
Posted

You are not getting any content written to the file because you are never writing to it.

Also, I suggest you study the 'using statement to wrap file operations: [^] so temporary file objects are efficiently disposed of.

I suggest you use a StreamWriter here since you are writing text to a file:
C#
// get the Desktop path
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

saveFileDialog1.InitialDirectory = filePath;
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    StringBuilder sb = new StringBuilder();

    // code to add content to the StringBuilder here ...

    using (StreamWriter theFile = new StreamWriter(saveFileDialog1.FileName))
    {
        theFile.Write(sb.ToString());
    }
}
 
Share this answer
 
Comments
angelandbuffy9 23-Mar-14 10:28am    
Ok thank you!
You have to try the WriteAllText function[^].
 
Share this answer
 
Comments
BillWoodruff 23-Mar-14 2:12am    
Voted #4 to counteract the absurd vote of #1
Abhinav S 23-Mar-14 2:19am    
Thank you BillWood ji.
angelandbuffy9 23-Mar-14 10:01am    
Okay thank you but I am a little confused what I would put for the string path?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900