Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从文本文件中读取二进制文件并将其转换为按钮c#_C#_Arrays_Winforms - Fatal编程技术网

从文本文件中读取二进制文件并将其转换为按钮c#

从文本文件中读取二进制文件并将其转换为按钮c#,c#,arrays,winforms,C#,Arrays,Winforms,我目前正在尝试制作一个类似于“solo Noble”的游戏,在这个游戏中,你有很多球,你需要得到尽可能低的分数。我目前正在尝试制作一个黑白按钮数组,这些按钮在外部文本文件中以二进制形式成形。我目前有: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Linq; using Sy

我目前正在尝试制作一个类似于“solo Noble”的游戏,在这个游戏中,你有很多球,你需要得到尽可能低的分数。我目前正在尝试制作一个黑白按钮数组,这些按钮在外部文本文件中以二进制形式成形。我目前有:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApplication9.Properties;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Marble();
        }
        public void Marble()
        {
            int ButtonWidth = 40;
            int ButtonHeight = 40;
            int Distance = 20;
            int start_x = 10;
            int start_y = 10;
            int y = 0;
            int x = 0;
            int delX = x + (y * 2);

            for (x = 0; x < 8; x++)
            {               
              for (y = 0; y < 8; y++)
              {
                GameButton tmpButton = new GameButton();
                tmpButton.BackColor = Color.Black;
                tmpButton.Top = start_x + (x * ButtonHeight + Distance);
                tmpButton.Left = start_y + (y * ButtonWidth + Distance);
                tmpButton.Width = ButtonWidth;
                tmpButton.Height = ButtonHeight;
                tmpButton.Text = "X: " + x.ToString() + " Y: " + y.ToString();
                tmpButton.MouseUp += TmpButton_MouseUp;
                tmpButton.Row = x;
                tmpButton.Column = y;
                tmpButton.Currentcolor = false;

                if (x == 4 && y == 6) {
                    tmpButton.BackColor = Color.White;
                }                               
                else
                {                      
                    this.Controls.Add(tmpButton);
                }                    
            }
        }
    }

    private void TmpButton_MouseUp(object sender, MouseEventArgs e)
    {
        GameButton Mygamebutton = (GameButton) sender;
        Mygamebutton.Currentcolor = !Mygamebutton.Currentcolor;
        if (Mygamebutton.Currentcolor == true)
        {
            Mygamebutton.BackColor = Color.Black;
        }
        else
        {
            Mygamebutton.BackColor = Color.White;
        }
     }
   }
}

我不太清楚如何将二进制数组从.txt文件转换为按钮,例如0是无按钮,1是按钮。

如果您想序列化有关按钮的信息,并在稍后将这些按钮添加到表单中,您必须了解要在文件中存储的内容:位置(x和y坐标)、大小,文本,背景

private void LoadButtonsInformation()
{
    using (var stream = new MemoryStream(File.ReadAllBytes(@"C:\Projects\info.bin")))
    {
        var serializer = new BinaryFormatter();
        var buttonInformations = (ButtonInformation[]) serializer.Deserialize(stream);

        var buttons= buttonInformations.Select(button => new Button
        {
            Location = new Point(button.X, button.Y),
            Text = button.Text,
            Width = button.Width,
            Height = button.Height
        }).ToArray();

        //add to form
        foreach (var button in buttons)
            Controls.Add(button);
    }
}


private void SaveButtonsInformation(params Button[] buttons)
{
    var buttonsInformation = buttons.Select(button => new ButtonInformation
    {
        X = button.Location.X,
        Y = button.Location.Y,
        Text = button.Text,
        Width = button.Width,
        Height = button.Height
    }).ToArray();

    using (Stream stream = new FileStream(@"C:\Projects\info.bin", FileMode.Create))
    {
        var serializer = new BinaryFormatter();
        serializer.Serialize(stream, buttonsInformation);
    }
}

[Serializable]
public class ButtonInformation
{
    public int X { get; set; }

    public int Y { get; set; }

    public string Text { get; set; }

    public int Width { get; set; }

    public int Height { get; set; }
}

你是如何创建这个二进制文件的?你把你的文件称为“二进制”和“文本”,这很容易混淆。您是否正在谈论仅包含
0
1
字符的文本文件?不管怎样,你到底有什么问题?读取文件?或者如何“连接”文件读取和按钮创建代码?
private void LoadButtonsInformation()
{
    using (var stream = new MemoryStream(File.ReadAllBytes(@"C:\Projects\info.bin")))
    {
        var serializer = new BinaryFormatter();
        var buttonInformations = (ButtonInformation[]) serializer.Deserialize(stream);

        var buttons= buttonInformations.Select(button => new Button
        {
            Location = new Point(button.X, button.Y),
            Text = button.Text,
            Width = button.Width,
            Height = button.Height
        }).ToArray();

        //add to form
        foreach (var button in buttons)
            Controls.Add(button);
    }
}


private void SaveButtonsInformation(params Button[] buttons)
{
    var buttonsInformation = buttons.Select(button => new ButtonInformation
    {
        X = button.Location.X,
        Y = button.Location.Y,
        Text = button.Text,
        Width = button.Width,
        Height = button.Height
    }).ToArray();

    using (Stream stream = new FileStream(@"C:\Projects\info.bin", FileMode.Create))
    {
        var serializer = new BinaryFormatter();
        serializer.Serialize(stream, buttonsInformation);
    }
}

[Serializable]
public class ButtonInformation
{
    public int X { get; set; }

    public int Y { get; set; }

    public string Text { get; set; }

    public int Width { get; set; }

    public int Height { get; set; }
}