按钮数组到文本文件? [英] Button array to text file?

查看:74
本文介绍了按钮数组到文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何获取button.text信息感到困惑,例如,如果button.text是1 2或3.我在这里丢失,无论我尝试什么,因为我想写每个按钮的文本buttonArray to a file。我认为问题可能是我在保存部分有错误的代码。始终会创建一个空白文件,因此我知道它是错误的。如何更改我的代码以将buttonArray写入文本文件?任何帮助都很棒



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

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();
              }
          }

      }

推荐答案

你不是将任何内容写入文件,因为你从来没有写过它。



另外,我建议你学习'using语句来包装文件操作:[ ^ ]所以临时文件对象被有效处理。



我建议您在这里使用StreamWriter,因为您正在将文本写入文件:
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:
// 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());
    }
}


你必须尝试 WriteAllText 功能 [ ^ ]。


这篇关于按钮数组到文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆