Search code examples
c#winformscrossword

Get number of blank textboxes


I am writing crossWord program First, the user must enter a number "n" , and create a n * n table with TextBoxes that are in white color and blank. After building the table, the user clicks on several houses and the house backcolor change to black.

My question is after this steps, how many TextBoxes are in the Black color, how many are white, How can I detect the maximum number of consecutive white text box without any black on them, in horizontally or vertically columns,to paste words that match to them!

enter image description here

In above table, form must detect that 5 is max of white consecutive textboxes in second horizontal line or in second vertical line, after user fill them must show 4 is max in first horizontal line, and go on to end...

Here is my code snippents:

private void CreateCrossTable()
{
    int count = Convert.ToInt32(textBox1.Text.Trim());
    if (count > 10)
       count = 10;

    int x = 100, y = 100;
    const int value = 100;

    for (int i = 1; i <= count; i++)
    {
        for (int j = 1; j <= count; j++)
        {
            x = value + (j * 20);
            TextBox tb = new TextBox();
            tb.Name = "txtbox" + i + "-" + j;
            tb.Location = new Point(x, y);
            tb.Size = new System.Drawing.Size(20, 20);
            tb.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.txtMouseDoubleClick);
            Controls.Add(tb);
        }

        y = value + (i * 20);
    }
}

private void txtMouseDoubleClick(object sender, MouseEventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.BackColor = Color.Black;
    tb.Enabled = false;
    tb.Text = "|";
}

so , now I use LINQ to get All white textbox like this:

IEnumerable<TextBox> FreeItems = frm.Controls.OfType<TextBox>().Where(I => I.BackColor != Color.Black);

how can I get items those who are white and diffrence of X location of them not more than 20!


Solution

  • Try following example

    private void findConsecutive()
    {
    
        var vertical = (from Control cnt in pnlCrossWord.Controls
                        where (cnt.GetType().Name.Equals("TextBox")) && (!cnt.BackColor.Equals(Color.Black))
                        orderby cnt.Top
                        select cnt.Top).Distinct().ToArray();
        var horizontal = (from Control cnt in pnlCrossWord.Controls
                        where (cnt.GetType().Name.Equals("TextBox")) && (!cnt.BackColor.Equals(Color.Black))
                        orderby cnt.Left
                          select cnt.Left).Distinct().ToArray();
    
        List<int> vList = new List<int>();
        int iIndex = 0;
    
        foreach (int top in vertical)
        {
            vList.Add(0);
            int vIndex = 0;
            int iConsecutive = 0;
            int iLastLeft = -1;
            var Item = (from Control cnt in pnlCrossWord.Controls
                        where (cnt.GetType().Name.Equals("TextBox")) && (!cnt.BackColor.Equals(Color.Black))
                        && (cnt.Top.Equals(top))
                        select (TextBox)cnt).ToArray();
            foreach (TextBox txt in Item)
            {
                if ((iLastLeft + txt.Width) < txt.Left && iLastLeft > -1)
                {
                    if (iConsecutive > vList[iIndex])
                        vList[iIndex] = iConsecutive;
    
                    iConsecutive = 0;
                }
    
                iConsecutive++;
    
                iLastLeft = txt.Left;
                vIndex++;
            }
            if (iConsecutive > vList[iIndex])
                vList[iIndex] = iConsecutive;
            iIndex++;
        }
    
        int MaxConsicutiveIndex = vList.IndexOf(vList.Max());           
    }
    

    EDITED Above code will retrieve the Line Index of Maximum Consicutive White box in Horizontal Line.