M
M
mIka012021-03-25 18:35:13
C++ / C#
mIka01, 2021-03-25 18:35:13

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();


Form2 has code, it's in the constructor. There is also pictureBox1 on the form itself.
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]);
                }
            }
         }

According to the idea, a grid pattern should appear on the second form (Form2).
Everything works if it's all done in Form1 , but it doesn't work in Form2.
Explain what I'm doing wrong and if it's not difficult, then tell me if it's possible to make Form2 appear on the second monitor.

Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2021-03-25
@mIka01

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]);
        }
    }
}

A small comment - advice not directly related to the question:
Do not pass an array in the parameters, create a class with properties whose names will reflect the meaning of the values ​​and use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question