vendredi 6 mai 2016

How DataBindings to connect a checkbox to a variable from a different class in Windows Forms (C#)

I'm pretty new to C# and Windows Forms, but for a university work I have to build a simulator for the PIC Processor.

Right now I created some classes and created an int array for my register.

Since some addresses have a special meaning in the register (like when the value on address x is 1, than ...), I added some checkboxes to my Form.

So now i could implement a Checkbox_CheckedChanged-Method for each of my checkboxes, but imho that would be pretty ugly (and i would have to pay attention to change the checkbox state, when the code changes the value).

Because of that I would like to use DataBindings to bind the specific int value of the register-array to the concerning checkbox. Is this possible?

I know that there are at least two problems which may appear:

  1. Create a binding which will resolve the boolean checkbox value to a 0 or 1 (there are no other values for those special function registers) and which will set this value to the RegisterSet-Array.
  2. Find a way to modify the private int-array through the binding from the Form. (When this is way too complicated, i could also change it to public..).

Here is an extraction of the RegisterSet class:

namespace PicSimulator
{
  class RegisterSet
  {
    private int[] register = new int[0xFF];

    public RegisterSet()
    {
        this.InitializeRegister();
    }

    // here we set the register initialy (don't be confused by the different values, I'll only need 0 or 1's)
    public void InitializeRegister()
    {
        Array.Clear(register, 0, register.Length);
        register[(int)RegisterType.INDF]        = 0x00;
        register[(int)RegisterType.TMR0]        = 0x00;
        register[(int)RegisterType.PCL]         = 0x00;
        register[(int)RegisterType.STATUS]      = 0x18;
        register[(int)RegisterType.FSR]         = 0x00;
        register[(int)RegisterType.PORTA]       = 0x00;
        register[(int)RegisterType.PORTB]       = 0x00;
        register[(int)RegisterType.EEDATA]      = 0x00;
        register[(int)RegisterType.EEADR]       = 0x00;
        register[(int)RegisterType.PCLATH]      = 0x00;
        register[(int)RegisterType.INTCON]      = 0x00;
        register[(int)RegisterType.OPTION_REG]  = 0xFF;
        register[(int)RegisterType.TRISA]       = 0x1F;
        register[(int)RegisterType.TRISB]       = 0xFF;
        register[(int)RegisterType.EECON1]      = 0x00;
        register[(int)RegisterType.EECON2]      = 0x00;
    }

    public int[] GetRegister()
    {
        return register;
    }
  }
}

And my Form so far:

namespace PicSimulator
{
  public partial class Form1 : Form
  {
    private PicSimulator picSimulator; // Contains a RegisterSet-Object
    private Log log = null;
    private int test = 1;

    public Form1()
    {
        InitializeComponent();
        // Binding comes here? (as said..I'm pretty new to UI programming..)

    }

    // Some more buttons and stuff
    /* ... */
  }
}

I appreciate every help! Many thanks in advance!

Dominik Müller

Aucun commentaire:

Enregistrer un commentaire