Answer the question
In order to leave comments, you need to log in
How to draw in PictureBox on second form?
Hello, I'm working with Form2 for the first time and as usual something went wrong.
There is a Form1 where textBoxes are located and a button button1 in which there is a code.
Form2 a = new Form2(new int[] { Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox6.Text), Convert.ToInt32(textBox7.Text), Convert.ToInt32(textBox8.Text), });
a.Show();
public Form2(int[] q)
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
Size size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
pictureBox1.Size = size;
Graphics g = pictureBox1.CreateGraphics();
g.Clear(System.Drawing.Color.FromArgb(q[2], q[3], q[4]));
for (int i = 0; i < pictureBox1.Width; i += q[0])
{
for (int j = 0; j < pictureBox1.Height; j += q[0])
{
g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(q[5], q[6], q[7])), i, j, q[1], q[1]);
}
}
}
Answer the question
In order to leave comments, you need to log in
The most convenient way to paint is in the Paint event handler . If at some point you need to update the display, then you should call a redraw.
An example, based on your code
readonly int[] q;
public Form2(int[] q)
{
InitializeComponent();
this.q = q;
this.WindowState = FormWindowState.Maximized;
Size size = new Size(Screen.PrimaryScreen.WorkingArea.Width,
Screen.PrimaryScreen.WorkingArea.Height);
pictureBox1.Size = size;
pictureBox1.Paint += PictureBox_Paint;
}
private void PictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.FromArgb(q[2], q[3], q[4]));
for (int i = 0; i < pictureBox1.Width; i += q[0])
{
for (int j = 0; j < pictureBox1.Height; j += q[0])
{
g.FillRectangle(new SolidBrush(Color.FromArgb(q[5], q[6], q[7])), i, j, q[1], q[1]);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question