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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 k8s集群重启后,kubelet一直报systemctl restart kubelet.service "Failed to delete cgroup paths"
  • ¥15 微信小程序构建工具类发布到npm,install后报错
  • ¥15 amesim液压仿真问题
  • ¥15 python解析json文件时报错
  • ¥15 MATLAB优化设计
  • ¥15 ANN预测模型 200大米
  • ¥20 优秀学生的问题 oj java
  • ¥15 scratch-gui 如何集成进 react native
  • ¥15 把全局悬浮窗改为局内悬浮窗
  • ¥15 Excel vba编程调用word如何读取换行符