简体   繁体   中英

Reading binary from a text file and turning them into buttons c#

I am currently trying to make a game similar to the game "solo Noble", in which you have a number of balls and you need to get the lowest score possible. I am currently trying to make an array of black & white buttons, which are shaped in the form of binary in an external text file. I currently have this:

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;
        }
     }
   }
}

But I am trying to get something like this:

byte[] fileBytes = File.ReadAllBytes(inputFilename);
StringBuilder sb = new StringBuilder();
foreach(byte b in fileBytes)
{
    sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));  
}
File.WriteAllText(outputFilename, sb.ToString());

I am not quite sure how to turn the binary array from the .txt file into buttons, for instance a 0 is no button, and a 1 is a button.

In case you want to serialize information about buttons and at than later add these buttons on your form, you have to understand, what you want to store in the file: position (x and y coordinates), size, text, Background?

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM