单击按钮时将数据保存到文件中
本文关键字:存到文件 数据 按钮 单击 | 更新日期: 2023-09-27 17:51:01
我正在用c#制作一个ledcube应用程序。它现在所做的就是允许用户点击一些按钮来改变所需led的颜色。我试图暗示一个功能,允许用户将当前配置(颜色)保存到一个文件,以后可以通过编程板读取。保存的文件必须包含每个led的二进制代码(led是否亮,以及哪种颜色:关、红、绿、橙)。我在想1个led可以包含一个值在00 - 11之间(所以0和3)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] laag_1 = new Button[8, 8];
Button[,] laag_2 = new Button[8, 8];
Button[,] laag_3 = new Button[8, 8];
Button[,] laag_4 = new Button[8, 8];
Button[,] laag_5 = new Button[8, 8];
Button[,] laag_6 = new Button[8, 8];
Button[,] laag_7 = new Button[8, 8];
Button[,] laag_8 = new Button[8, 8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_1[x, y] = new Button();
laag_1[x, y].SetBounds((50 * x) + 100, (50 * y) + 30, 40, 40);
laag_1[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_1[x, y]);
laag_1[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_2.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_2[x, y] = new Button();
laag_2[x, y].SetBounds((50 * x) + 520, (50 * y) + 30, 40, 40);
laag_2[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_2[x, y]);
laag_2[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_3.GetLength(0); x++)
{
for (int y = 0; y < laag_3.GetLength(1); y++)
{
laag_3[x, y] = new Button();
laag_3[x, y].SetBounds((50 * x) + 940, (50 * y) + 30, 40, 40);
laag_3[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_3[x, y]);
laag_3[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_4.GetLength(0); x++)
{
for (int y = 0; y < laag_4.GetLength(1); y++)
{
laag_4[x, y] = new Button();
laag_4[x, y].SetBounds((50 * x) + 1360, (50 * y) + 30, 40, 40);
laag_4[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_4[x, y]);
laag_4[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_5.GetLength(0); x++)
{
for (int y = 0; y < laag_5.GetLength(1); y++)
{
laag_5[x, y] = new Button();
laag_5[x, y].SetBounds((50 * x) + 100, (50 * y) + 520, 40, 40);
laag_5[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_5[x, y]);
laag_5[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_6.GetLength(0); x++)
{
for (int y = 0; y < laag_6.GetLength(1); y++)
{
laag_6[x, y] = new Button();
laag_6[x, y].SetBounds((50 * x) + 520, (50 * y) + 520, 40, 40);
laag_6[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_6[x, y]);
laag_6[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_7.GetLength(0); x++)
{
for (int y = 0; y < laag_7.GetLength(1); y++)
{
laag_7[x, y] = new Button();
laag_7[x, y].SetBounds((50 * x) + 940, (50 * y) + 520, 40, 40);
laag_7[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_7[x, y]);
laag_7[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_8.GetLength(0); x++)
{
for (int y = 0; y < laag_8.GetLength(1); y++)
{
laag_8[x, y] = new Button();
laag_8[x, y].SetBounds((50 * x) + 1360, (50 * y) + 520, 40, 40);
laag_8[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(laag_8[x, y]);
laag_8[x, y].BackColor = Color.Black;
}
}
this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
LoadFromFile();
}
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Green;
break;
case "Black":
ctrl.BackColor = Color.Red;
break;
case "Green":
ctrl.BackColor = Color.Yellow;
break;
case "Yellow":
ctrl.BackColor = Color.Black;
break;
default:
ctrl.BackColor = Color.Black;
break;
}
}
void SaveEventHandler(object sender, EventArgs e)
{
SaveToFile();
}
private const string filePath = @"C:'testmap'laag_1.txt";
private void LoadFromFile()
{
if (!System.IO.File.Exists(filePath))
return;
byte[] data = System.IO.File.ReadAllBytes(filePath);
if (data == null || data.Length != laag_1.GetLength(0) * laag_1.GetLength(1) * 2)
return;
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
int position = (y * laag_1.GetLength(0) + x);
string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
Color color;
switch (value)
{
case "01":
color = Color.Red;
break;
case "00":
color = Color.Black;
break;
case "10":
color = Color.Green;
break;
case "11":
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
laag_1[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
Dictionary<Form1, int> d = new Dictionary<Form1, int>();
byte[] data = new byte[laag_1.GetLength(0) * laag_1.GetLength(1) * 2];
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
int position = (y * laag_1.GetLength(0) + x);
string value;
switch (laag_1[x, y].BackColor.Name)
{
case "Red":
value = "01";
break;
case "Black":
value = "00";
break;
case "Green":
value = "10";
break;
case "Yellow":
value = "11";
break;
default:
value = "00";
break;
}
data[2 * position] = (byte)value[0];
data[2 * position + 1] = (byte)value[1];
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
private void button1_Click(object sender, EventArgs e)
{
for (int x = 0; x < laag_1.GetLength(0); x++)
{
for (int y = 0; y < laag_1.GetLength(1); y++)
{
laag_1[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_2.GetLength(0); x++)
{
for (int y = 0; y < laag_2.GetLength(1); y++)
{
laag_2[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_3.GetLength(0); x++)
{
for (int y = 0; y < laag_3.GetLength(1); y++)
{
laag_3[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_4.GetLength(0); x++)
{
for (int y = 0; y < laag_4.GetLength(1); y++)
{
laag_4[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_5.GetLength(0); x++)
{
for (int y = 0; y < laag_5.GetLength(1); y++)
{
laag_5[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_6.GetLength(0); x++)
{
for (int y = 0; y < laag_6.GetLength(1); y++)
{
laag_6[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_7.GetLength(0); x++)
{
for (int y = 0; y < laag_7.GetLength(1); y++)
{
laag_7[x, y].BackColor = Color.Black;
}
}
for (int x = 0; x < laag_8.GetLength(0); x++)
{
for (int y = 0; y < laag_8.GetLength(1); y++)
{
laag_8[x, y].BackColor = Color.Black;
}
}
}
}
}
您可以查看一下文件。WriteAllBytes和File。ReadAllBytes方法用于保存和加载文件。
还可以看看OpenFileDialog控件来选择要打开的文件。
此外,没有必要吝啬值,您可以(并且应该)为每个按钮使用一个完整的字节,只需循环按钮并将值插入字节[],然后将它们写入文件
我假设您想使用对象序列化。
XMLSerialization : http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization BinarySerialization : http://msdn.microsoft.com/en-us/library/72hyey7b (v = vs.71) . aspx
如果你想保存数据,然后通过另一个应用程序读取它,我建议你序列化,因为你不必担心从文件中读取
试试这个:
private const string filePath = @"d:'test.txt";
private void LoadFromFile()
{
byte[] data = System.IO.File.ReadAllBytes(filePath);
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (x * btn.GetLength(1) + y) * 2;
int index = position / 8;
int shift = position % 8;
byte value = (byte)((data[index] >> shift) % 4);
Color color;
switch (value)
{
case 1:
color = Color.Red;
break;
case 0:
color = Color.Black;
break;
case 2:
color = Color.Green;
break;
case 3:
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
btn[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
byte[] data = new byte[(7 + btn.GetLength(0) * btn.GetLength(1) * 2) / 8];
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (x * btn.GetLength(1) + y) * 2;
byte value;
switch (btn[x, y].BackColor.Name)
{
case "Red":
value = 1;
break;
case "Black":
value = 0;
break;
case "Green":
value = 2;
break;
case "Yellow":
value = 3;
break;
default:
value = 0;
break;
}
int index = position / 8;
int shift = position % 8;
data[index] = (byte)(data[index] | (value << shift));
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
(更新):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] btn = new Button[8, 8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
btn[x, y] = new Button();
btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
btn[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(btn[x, y]);
btn[x, y].BackColor = Color.Black;
}
}
this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
if (System.IO.File.Exists(filePath))
LoadFromFile();
}
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Green;
break;
case "Black":
ctrl.BackColor = Color.Red;
break;
case "Green":
ctrl.BackColor = Color.Yellow;
break;
case "Yellow":
ctrl.BackColor = Color.Black;
break;
default:
ctrl.BackColor = Color.Black;
break;
}
}
void SaveEventHandler(object sender, EventArgs e)
{
SaveToFile();
}
private const string filePath = @"d:'test.txt";
private void LoadFromFile()
{
byte[] data = System.IO.File.ReadAllBytes(filePath);
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (x * btn.GetLength(1) + y) * 2;
int index = position / 8;
int shift = position % 8;
byte value = (byte)((data[index] >> shift) % 4);
Color color;
switch (value)
{
case 1:
color = Color.Red;
break;
case 0:
color = Color.Black;
break;
case 2:
color = Color.Green;
break;
case 3:
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
btn[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
byte[] data = new byte[(7 + btn.GetLength(0) * btn.GetLength(1) * 2) / 8];
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (x * btn.GetLength(1) + y) * 2;
byte value;
switch (btn[x, y].BackColor.Name)
{
case "Red":
value = 1;
break;
case "Black":
value = 0;
break;
case "Green":
value = 2;
break;
case "Yellow":
value = 3;
break;
default:
value = 0;
break;
}
int index = position / 8;
int shift = position % 8;
data[index] = (byte)(data[index] | (value << shift));
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
}
}
[更新#2]将每个led的存储空间改为全字节:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] btn = new Button[8, 8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
btn[x, y] = new Button();
btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
btn[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(btn[x, y]);
btn[x, y].BackColor = Color.Black;
}
}
this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
LoadFromFile();
}
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Green;
break;
case "Black":
ctrl.BackColor = Color.Red;
break;
case "Green":
ctrl.BackColor = Color.Yellow;
break;
case "Yellow":
ctrl.BackColor = Color.Black;
break;
default:
ctrl.BackColor = Color.Black;
break;
}
}
void SaveEventHandler(object sender, EventArgs e)
{
SaveToFile();
}
private const string filePath = @"d:'test.txt";
private void LoadFromFile()
{
if (!System.IO.File.Exists(filePath))
return;
byte[] data = System.IO.File.ReadAllBytes(filePath);
if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1))
return;
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (y * btn.GetLength(0) + x);
char value = (char)data[position];
Color color;
switch (value)
{
case '1':
color = Color.Red;
break;
case '0':
color = Color.Black;
break;
case '2':
color = Color.Green;
break;
case '3':
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
btn[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1)];
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (y * btn.GetLength(0) + x);
char value;
switch (btn[x, y].BackColor.Name)
{
case "Red":
value = '1';
break;
case "Black":
value = '0';
break;
case "Green":
value = '2';
break;
case "Yellow":
value = '3';
break;
default:
value = '0';
break;
}
data[position] = (byte)value;
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
}
}
更新# 3:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Button[,] btn = new Button[8, 8];
public Form1()
{
InitializeComponent();
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
btn[x, y] = new Button();
btn[x, y].SetBounds((50 * x) + 30, (50 * y) + 30, 40, 40);
btn[x, y].Click += new EventHandler(this.btnEvent_click);
Controls.Add(btn[x, y]);
btn[x, y].BackColor = Color.Black;
}
}
this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
LoadFromFile();
}
void btnEvent_click(object sender, EventArgs e)
{
Control ctrl = ((Control)sender);
switch (ctrl.BackColor.Name)
{
case "Red":
ctrl.BackColor = Color.Green;
break;
case "Black":
ctrl.BackColor = Color.Red;
break;
case "Green":
ctrl.BackColor = Color.Yellow;
break;
case "Yellow":
ctrl.BackColor = Color.Black;
break;
default:
ctrl.BackColor = Color.Black;
break;
}
}
void SaveEventHandler(object sender, EventArgs e)
{
SaveToFile();
}
private const string filePath = @"d:'test.txt";
private void LoadFromFile()
{
if (!System.IO.File.Exists(filePath))
return;
byte[] data = System.IO.File.ReadAllBytes(filePath);
if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1) * 2)
return;
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (y * btn.GetLength(0) + x);
string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
Color color;
switch (value)
{
case "01":
color = Color.Red;
break;
case "00":
color = Color.Black;
break;
case "10":
color = Color.Green;
break;
case "11":
color = Color.Yellow;
break;
default:
color = Color.Black;
break;
}
btn[x, y].BackColor = color;
}
}
}
private void SaveToFile()
{
Dictionary<Form1, int> d = new Dictionary<Form1, int>();
byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1) * 2];
for (int x = 0; x < btn.GetLength(0); x++)
{
for (int y = 0; y < btn.GetLength(1); y++)
{
int position = (y * btn.GetLength(0) + x);
string value;
switch (btn[x, y].BackColor.Name)
{
case "Red":
value = "01";
break;
case "Black":
value = "00";
break;
case "Green":
value = "10";
break;
case "Yellow":
value = "11";
break;
default:
value = "00";
break;
}
data[2 * position] = (byte)value[0];
data[2 * position + 1] = (byte)value[1];
}
}
System.IO.File.WriteAllBytes(filePath, data);
}
}
}