把生活写成首诗
2020-04-18 14:08
采纳率: 68.4%
浏览 414

c#winform如何实现一维数组转二维数组并保存在某处

怎样实现保存由winform的多个textBox获取的值组成的数组并写成int【,】的形式

比如第一次操作完窗体得到第一个数组{1,2,3,4},第二次再操作又得到了第二个数组{4,3,2,1},如何写成 int【,】array={{1,2,3,4},{4,3,2,1}}的形式,并保存在txt或者别的地方,并且下次可以提取出来array

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • threenewbee 2020-04-18 15:05
    已采纳
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace Q1064245
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private int[,] arr;
    
            int x = 0;
    
            int y = 0;
    
            private void arrToTextBox(int[,] arr)
            {
                panel1.Controls.Clear();
                x = arr.GetLength(0);
                y = arr.GetLength(1);
                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        TextBox txt = new TextBox();
                        txt.Name = string.Format("txt_{0}_{1}", i, j);
                        panel1.Controls.Add(txt);
                        txt.Top = i * 40;
                        txt.Left = j * 120;
                        txt.Text = arr[i, j].ToString();
                        txt.TextChanged += new EventHandler(txt_TextChanged);
                    }
                }
            }
    
            void txt_TextChanged(object sender, EventArgs e)
            {
                var arr = textBoxToArr();
                propertyGrid1.SelectedObject = arr;
            }
    
            private int[,] textBoxToArr()
            { 
                int[,] arr = new int[x, y];
                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        arr[i, j] = int.Parse((panel1.Controls["txt_" + i.ToString() + "_" + j.ToString()] as TextBox).Text);
                    }
                }
                return arr;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                int[] dim = textBox1.Text.Split('*').Select(x => int.Parse(x)).ToArray();
                arr = new int[dim[0], dim[1]];
                propertyGrid1.SelectedObject = arr;
                arrToTextBox(arr);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofn = new OpenFileDialog();
                if (ofn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var data = File.ReadAllLines(ofn.FileName).Select(x => x.Split(',').Select(y => int.Parse(y)).ToArray()).ToArray();
                    int[,] arr = new int[data.Count(), data[0].Count()];
                    for (int i = 0; i < data.Count(); i++)
                    {
                        for (int j = 0; j < data[0].Count(); j++)
                        {
                            arr[i, j] = data[i][j];
                        }
                    }
                    propertyGrid1.SelectedObject = arr;
                    arrToTextBox(arr);
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    var arr = textBoxToArr();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < x; i++)
                    {
                        if (i != 0) sb.Append("\r\n");
                        for (int j = 0; j < y; j++)
                        {
                            if (j != 0) sb.Append(',');
                            sb.Append(arr[i, j].ToString());
                        }
                    }
                    File.WriteAllText(sfd.FileName, sb.ToString());
                }
    
            }
        }
    }
    
    

    图片说明

    完整代码下载:https://download.csdn.net/download/caozhy/12337231

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • abcfgh 2020-04-18 14:28
    //用于保存二维数组
    List<int[]> lstArray= new List<int[]>();
    //保存第一个
    int[] arry1={1,2,3,4};
    lstArray.Add(arry1);
    //保存第二个
    int[] arry2={4,3,2,1};
    lstArray.Add(arry2);
    
    //写文件示意代码
    foreach(var line in lstArray)
    {
      file.WriteLine(line);
    }
    file.save();
    
    //加载文件
    file.load("array.txt");
    while(!file.EOF)
    {
    int[] temp=file.readline();
    lstArray.Add(temp);
    file.next();
    }
    
    评论
    解决 1 无用
    打赏 举报