Click to See Complete Forum and Search --> : Small graphics problem


NCLSEA123
December 8th, 2008, 02:17 PM
O.K., so I was just messing around and now I have this letter grid which is highlighted when clicked and the letter is sown in a message. My problem is that both columns are highlighted when a click a letter. Problem code is about halfway down. Suggestions?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FormChecker
{
public partial class FormChecker : Form
{
public FormChecker()
{
InitializeComponent();

}

private bool [] chosen = new bool[27]; // true for selected box


private void painter()
// repaint grid
{
string str;
Graphics gr = CreateGraphics();
Pen blackPen = new Pen(Color.Black);
SolidBrush tealBrush = new SolidBrush(Color.Teal);
Font gridFont = new Font("Arial", 30);
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);


gr.DrawLine(blackPen, 50, 200, 700, 200);
gr.DrawLine(blackPen, 50, 250, 700, 250);
gr.DrawLine(blackPen, 50, 250, 700, 250);
gr.DrawLine(blackPen, 50, 300, 700, 300);
for (int i = 1; i <= 14; i++)
{
gr.DrawLine(blackPen, 50 * i, 200, 50 * i, 250);
}
for (int i = 1; i <= 14; i++)
{
gr.DrawLine(blackPen, 50 * i, 250, 50 * i, 300);
}

for (int i=0; i<13; i++)
{
if (chosen[i]) gr.FillRectangle(yellowBrush, i * 50 + 51, 201, 49, 49);
//THESE ARE THE PROBLEMS
}
for (int i = 0; i < 13; i++)
{
if (chosen[i]) gr.FillRectangle(yellowBrush, i * 50 + 51, 251, 49, 49);
//THESE ARE THE PROBLEMS
}


for (int i = 0; i < 13; i++)
{
str = "" + (char)('A' + i);
gr.DrawString(str, gridFont, tealBrush, i * 50 + 55, 210);
}
for (int i = 0; i < 13; i++)
{
str = "" + (char)('N' + i);
gr.DrawString(str, gridFont, tealBrush, i * 50 + 55, 260);
}

gridFont.Dispose();
tealBrush.Dispose();
blackPen.Dispose();
gr.Dispose();
}

private void FormChecker_Shown(object sender, EventArgs e)
// display grid when form shown
{
painter();
}

private void FormChecker_MouseClick(object sender, MouseEventArgs e)
// react to mouse click


{

Graphics gr = CreateGraphics();
SolidBrush yellowBrush = new SolidBrush(Color.Yellow);





int checker = (e.Y / 100);
if (checker != 2)
{
MessageBox.Show("Above or Below Grid");
return;
}

int choice = e.X / 50;
int choice2 = e.Y / 50;


if (choice < 1 || choice > 13)
{
MessageBox.Show("off to left or right");
return;
}
chosen[choice-1] = true;

painter();






if (choice < 14 && choice2 < 5 )
{
MessageBox.Show("choice was " + (char)('A' + choice - 1));


return;


}
else
{
if (choice < 14 && choice2 >= 5)
{
MessageBox.Show("choice was " + (char)('N' + choice - 1));
for (int i = 0; i < 13; i++)
{
if (chosen[i]) gr.FillRectangle(yellowBrush, i * 50 + 51, 201, 49, 49);
}

}
}


}

private void FormChecker_Load(object sender, EventArgs e)
// ensure nothing checked when form loaded
{
for (int i=0; i<14; i++) chosen[i] = false;


}

private void FormChecker_Paint(object sender, PaintEventArgs e)
// repaint entire form
{
painter();
}
}
}

BigEd781
December 8th, 2008, 04:56 PM
I would really recommend doing all of your painting in the OnPaint() method. Painting in a click event (or in a load event) is a bit sloppy. Throw all of your logic into OnPaint and then if something happens that requires a repaint you can Invalidate() the form (or region that needs painting). Have you stepped through your code on the second click? I bet something is not being cleared or repainted the proper color.