public void GridCreate()
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brushBlack = new SolidBrush(Color.Black);
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i <= block.GetLength(0) - 1; i++)
{
for (int n = 0; n <= block.GetLength(0) - 1; n++)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
g.FillRectangle(brushBlack, block[n, i]);
}
}
data.block = block;
}
private void Form1_Shown(object sender, EventArgs e)
{
GridCreate();
}
I'm trying to make a grid in WindowsForms using PictureBox, but related code is not working correctly.This data.block = block; part works, but this g.FillRectangle(brushBlack, block[n, i]); doesn't work at all. I think the problem is in the Form1_Shown event, because this:
private void Form1_Click(object sender, EventArgs e)
{
GridCreate();
}
executes perfectly fine.
Override protected override void OnShown(EventArgs e) gives the same result as Form1_Shown.
The problem is CreateGraphics(), which is a temporary surface that gets erased when the PictureBox refreshes itself.
Just create the grid once, then draw the data in the Paint() event:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
GridCreate();
pictureBox1.Paint += pictureBox1_Paint;
}
private void GridCreate()
{
Rectangle[,] block = new Rectangle[16, 16];
for (int i = 0; i < block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
block[n, i] = new Rectangle(i * blockSize, n * blockSize, 20, 20);
}
}
data.block = block;
}
void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics; // use the SUPPLIED graphics, NOT CreateGraphis()!
for (int i = 0; i < data.block.GetLength(1); i++) // this is the 2nd dimension, so GetLength(1)
{
for (int n = 0; n < data.block.GetLength(0); n++) // this is the 1st dimension, so GetLength(0)
{
g.FillRectangle(Brushes.Black, data.block[n, i]);
}
}
}
Related
How would I create a 6x7 grid inside of a panel box in windows form C# on visual studio. I have tried using DrawLine, and Graphics but it is not working.
May someone assist me?
When running the program I was hoping it would look like this:
Hopefully your code looked something like:
private void Form1_Load(object sender, EventArgs e)
{
panel1.SizeChanged += Panel1_SizeChanged;
panel1.Paint += Panel1_Paint;
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int cols = 7;
int rows = 6;
int width = panel1.Width / cols;
int height = panel1.Height / rows;
for(int col=1; col<cols; col++)
{
e.Graphics.DrawLine(Pens.Black, new Point(col * width, 0), new Point(col * width, panel1.Height));
}
for(int row=1; row<rows; row++)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, row * height), new Point(panel1.Width, row * height));
}
}
private void Panel1_SizeChanged(object sender, EventArgs e)
{
panel1.Invalidate();
}
I need to pass the value for maxColumns to a method that will be used repeatedly by other items. How can I go about passing maxColumns in this scenario?
public partial class ValResults : Form
{
public ValResults()
{
InitializeComponent();
this.Table1Requirements();
}
private void Table1Requirements()
{
int maxColumns = 6;
this.table1LayoutPanelPrime.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPaint);
}
void tableLayoutPaint(object sender, TableLayoutCellPaintEventArgs e)
{
for (int i = 0; i < maxColumns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
}
If its in the same class, just define the variable at the class level.
private int _maxColumns;
public ValResults()
{
InitializeComponent();
this.Table1Requirements();
SetColumnCount();
}
private void SetColumnCount(){
_maxColumns= 6;
}
void tableLayoutPaint(object sender, TableLayoutCellPaintEventArgs e)
{
for (int i = 0; i < _maxColumns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
you can essentially pre build a function using lambdas...
private void Table1Requirements()
{
int maxColumns = 6;
this.table1LayoutPanelPrime.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPaint(maxColumns));
}
Action<object,TableLayoutCellPaintEventArgs> tableLayoutPaint(int columns)
return (sender, e) =>
{
for (int i = 0; i < columns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
that way you can generate different event handles with different max column settings
I am thinking of finding an easy way to put numbers into an array using a for loop. I have made the easy design with text showing how the name system works!
My code is like this:
double[,] kast = new double[3, 8];
string[] navn = new string[8];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSorter_Click(object sender, EventArgs e)
{
for(int i = 1; i > 4; i++)
{
for(int x = 1; i > 9; i++)
{
kast[i, x] = Convert.ToDouble(txtKast + i + x + .Text);
}
}
}
I don't know how I will make it work with the Convert.ToDouble part since "kast" is a double array.
I would like to automatically create a PictureBox. How to change this in the code:
private void button1_Click(object sender, EventArgs e)
{
PictureBox[] box = new PictureBox[textBox1.Text.Length];
for(int j=0;j<textBox1.Text.Length;j++)
box[0] = pictureBox1;
box[1] = pictureBox2;
box[2] = pictureBox3;
for (int i = 0; i < textBox1.Text.Length; ++i)
box[i].Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
}
You should probably use a FlowLayoutPanel control to hold your PictureBox controls. Then the code would look something like this:
void button1_Click(object sender, EventArgs e) {
while (flowLayoutPanel1.Controls.Count > 0) {
flowLayoutPanel1.Controls[0].Dispose();
}
for (int i = 0; i < textBox1.Text.Length; ++i) {
PictureBox pb = new PictureBox();
pb.Image = Image.FromFile(string.Format(#"c:\obrazki\{0}.jpg",textBox1.Text[i]));
flowLayoutPanel1.Controls.Add(pb);
}
}
I am trying to change the BackColor of a PictureBox in a grid. The PictureBox is part of an array and the array has a chared event handler. I am having difficulty changing different PictureBox's depending on which one is clicked.
This is what I have so far:
private PictureBox[,] GameGrid = new PictureBox[20, 20];
public frmGame()
{
int x = 10;
int y = 10;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
GameGrid[i, j] = new System.Windows.Forms.PictureBox();
setUpPicBox(x, y, i, j);
x += 11;
}
y += 11;
x = 10;
}
InitializeComponent();
}
public void setUpPicBox(int x, int y, int i, int j)
{
this.GameGrid[i, j].Location = new System.Drawing.Point(x, y);
this.GameGrid[i, j].Size = new System.Drawing.Size(10, 10);
this.GameGrid[i, j].BackColor = Color.Black;
this.GameGrid[i, j].Name = "btnGrid" + i + "-" + j;
this.GameGrid[i, j].Visible = true;
this.GameGrid[i, j].CreateGraphics();
this.GameGrid[i, j].Click += new System.EventHandler(this.picturebox_Click);
this.Controls.Add(GameGrid[i, j]);
}
private void picturebox_Click(object sender, EventArgs e)
{
}</code>
Any help would be appreciated
The event handler's sender parameter is the instance that raised the event. Here it is the PictureBox instance that the user has clicked. If you want to change its BackColor, you just cast the sender object to correct type and set the new color.
private void picturebox_Click(object sender, EventArgs e)
{
var pictureBox = sender as PictureBox;
if (pictureBox != null) {
pictureBox.BackColor = Color.Blue;
}
}
On your event handler, sender contains the object that caused the event handler to fire. So by casting it to the correct type, we can then access all the properties as per this example:
private void picturebox_Click(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender;
MessageBox.Show(pic.Name);
}
Note: code untested, not got access to VS to test