-1

I've created a Sudoku Game and I have 23 Digits that are covered for the user and that he needs too solve. I'm wondering if it's possible that through a Label show the user how many textboxes that are left over without an digit in them.

I've created the game through WinForms and I've pretty much come to an hold. If anyone can throw me in the right direction it would be much appreciated!

I created the textboxes and used arrays, then I randomized a finished sudoku. The randomizer is a whole class for itself and after that I pretty much assigned arrays

private void assign_Char_Value(string[] s_Rem_Value)
{
    arr_Char_Value = (s_Rem_Value[0] + s_Rem_Value[1] + s_Rem_Value[2] 
        + s_Rem_Value[3] + s_Rem_Value[4] + s_Rem_Value[5] + s_Rem_Value[6] 
        + s_Rem_Value[7] + s_Rem_Value[8]).ToCharArray();
    int i_Cnt = 0;
    foreach (TextBox[] arr_txtBox in validate.arr_txtBox_Horiz)
    {
        foreach (TextBox txtBox in arr_txtBox)
        {
            txtBox.Text = arr_Char_Value[i_Cnt].ToString();
            i_Cnt++;
        }
    }
    Rondomize_Numbers();
}

After that I chose int number = 23;

I have 3 buttons one of them correct the game another randomizes a new game and the other shows the rules of sudoku.

Dour High Arch
  • 21,513
  • 29
  • 75
  • 90
  • 2
    Where is your code? In which part did you get stuck? What is your problem? – L.B Jan 06 '14 at 18:27
  • Just in case you're interested, [here](http://stackoverflow.com/a/15344546/643085) is my example of a Sudoku board created using current, relevant .Net Windows UI technologies, instead of deprecated ones. – Federico Berasategui Jan 06 '14 at 18:35
  • I would hardly call WinForm deprecated, HighCore. Ref.: http://stackoverflow.com/questions/913417/will-windows-forms-be-deprecated-in-favor-of-wpf – OnoSendai Jan 06 '14 at 19:04

1 Answers1

4

Let's assume for simplicity you have all your textboxes in a panel. and that the textboxes are the only items in the panel.

Now have your textboxes hookup to the same textBox_TextChanged event like this:

public Form1()
{
    InitializeComponent();
    foreach (TextBox txtBox in panel1.Controls)
    {
        txtBox.TextChanged += textBox_TextChanged;
    }
}

now for the event:

private void textBox_TextChanged(object sender, EventArgs e)
{
    var emptyCounter = 0;
    foreach (TextBox txtBox in panel1.Controls)
    {
        if (String.IsNullOrWhiteSpace(txtBox.Text))
        {
            emptyCounter++;
        }
    }

    label1.Text = emptyCounter.ToString();
}

you could also use a LINQ statement instead of the foreach above making the method look like this:

private void textBox_TextChanged(object sender, EventArgs e)
{
    var emptyCounter = panel1.Controls.OfType<TextBox>().Count(txtBox => String.IsNullOrWhiteSpace(txtBox.Text));

    label1.Text = emptyCounter.ToString();
}

Hope this helps.

c0deMonk3y
  • 486
  • 3
  • 7
  • `.Cast()` would throw exception if `panel1` contains controls other than TextBox. – L.B Jan 06 '14 at 18:51
  • +1 BTW: I would use `String.IsNullOrWhiteSpace` instead of `IsNullOrEmpty` not to skip textboxes containing only whitespaces(but not empty) – L.B Jan 06 '14 at 18:59
  • thanks again this one was actually new to me. I updated the code. However, i think that in this specific case the textboxes should only allow numbers as it is a sudoku game. – c0deMonk3y Jan 06 '14 at 19:04