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

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 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 目标跟踪,计算机视觉
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡
  • ¥20 基于MATLAB的TDOA
  • ¥15 为啥输入字体突然变了