Click here to Skip to main content
15,884,043 members

How to draw a rectangle on a form picturebox from another form[Updated]

Un_NaMeD asked:

Open original thread
Hey everyone.

I have a quesiton.

I have 2 forms.

form = form1
form = form2

When pressing a button in form1, form2 shows up. What I want is pressing a button in form2 draws a rectangle on to form1 picturebox.

I Wrote an event handler function for button clicked in Form2 to be handled in Form1, but in handler function I can't draw anything in form1's picturebox.

How can I achieve it?

My best regards...

Version 2:

Ok. The real thing I want to do is this.
I'm trying to draw a map which is written from a text box.
In map there'are lines, forbidden areas, boxes and such things.
I can read the map succesfully and draw the map from a button which is in form1.
What I want to do is, draw the map from another form(form2) by clickling a button on form2.

The Brief codework:

This is the form2 calling in form1:

private void tlstrpbtnAkil_Click(object sender, EventArgs e)
{
Form2 objform = new Form2();
objform.Activate();
objform.Visible = true;

objform.btnOrtamYukle.Click += new EventHandler(objButton_Click);
}


Here the codes under the form2:
public Form1 _form1;
public Button f2Button { get; set; }


public struct Ortam
{
      public int OrtamKB;
      public string OrtamAdi;
      public string OrtamHaritasi;
      public string HucreDosyasi;
}

public Ortamlar(Form1 form1)
{
     this._form1 = form1;
}

C#
private void Form2_Load(object sender, EventArgs e)
{
           // set the public property when Form2 is created
           f2Button = this.btnOrtamYukle;
           // assign a click handler
           f2Button.Click += new EventHandler(f2Button_Click);
}

C#
private void f2Button_Click_Click(object sender, EventArgs e)
{
          // here is where the handling is done
            _form1 = new Form1();

            Ortam ortam = new Ortam();

            int count = dataGridView1.SelectedRows[0].Index;

            ortam.OrtamKB = Convert.ToInt32(ortamTbl.Rows[count]["OrtamKB"]);
            ortam.OrtamAdi = ortamTbl.Rows[count]["OrtamAdi"].ToString();
            ortam.OrtamHaritasi = ortamTbl.Rows[count]["OrtamHaritasi"].ToString();
            ortam.HucreDosyasi = ortamTbl.Rows[count]["HucreDosyasi"].ToString();

            ORTAMHaritasi = ortam.OrtamHaritasi;
            _form1.ORTAMHARITASI = ortam.OrtamHaritasi;

            //_form1.KontrolleriAc();

            _form1.p_box_map.Width = 1000;
            _form1.p_box_map.Height = 600;

            _form1.bmp = new Bitmap(_form1.p_box_map.Width, _form1.p_box_map.Height);

            ImOrtamCizdirVeritabani ortamcizdir = new ImOrtamCizdirVeritabani(_form1.p_box_map, _form1.bmp, _form1.ZoomKontrolCarpan,_form1.panel1);
            

            _form1.bmp.RotateFlip(RotateFlipType.Rotate180FlipX);

            _form1.bmp2 = _form1.bmp; // Boş Form.

            _form1.lbxKucultmeOranlari.Items.Add("scale_factor_x :" + ortamcizdir.ScaleFactorY);
            _form1.lbxKucultmeOranlari.Items.Add("scale_factor_y :" + ortamcizdir.ScaleFactorX);
            _form1.lbxKucultmeOranlari.Items.Add(ortamcizdir.ScaleFactor);
}


Map Drawing part:

class ImOrtamCizdirVeritabani : ImOrtamOkuVeritabanı
    {
        public static Image genelBmpNesnesi;

        public Graphics g;
        Pen Kalem = new Pen(Brushes.Blue, 5);

        // Default Constructer
        public ImOrtamCizdirVeritabani()
        {

        }

        public ImOrtamCizdirVeritabani(PictureBox picturebox1, Image bmp, int ZoomKontrolCarpan, Panel panel1)
        {
            g = Graphics.FromImage(bmp);

            for (int z = 0; z < base.ForbiddenAreaNumber; z++)
            {
                Rectangle rect = new Rectangle((base.ForbiddenArea[z].x1 / (int)(base.ScaleFactor))
                    , (base.ForbiddenArea[z].y1 /
                    (int)base.ScaleFactor),
                    ((base.ForbiddenArea[z].x2 / (int)base.ScaleFactor) - (base.ForbiddenArea[z].x1 /
                    (int)base.ScaleFactor)),
                    ((base.ForbiddenArea[z].y2 / (int)base.ScaleFactor) -
                    base.ForbiddenArea[z].y1 / (int)base.ScaleFactor));

                g.DrawRectangle(Kalem, rect);
                g.FillRectangle(Brushes.Blue, rect);
            }

            for (int z = 0; z < base.LineNumber; z++)
            {
                g.DrawLine(Kalem, (base.Lines[z].x1 / (int)base.ScaleFactor) * ZoomKontrolCarpan,
                    (base.Lines[z].y1 / (int)base.ScaleFactor) * ZoomKontrolCarpan,
                    (base.Lines[z].x2 / (int)base.ScaleFactor) * ZoomKontrolCarpan,
                    (base.Lines[z].y2 / (int)base.ScaleFactor) * ZoomKontrolCarpan);
            }

            panel1.AutoScrollPosition = new Point(bmp.Height, bmp.Width);
            picturebox1.Image = bmp;
            genelBmpNesnesi = bmp;
        }
    }

There is no need for ImOrtamOkuVeritabanı, it does its job well..

So, How can I trigger from form 2 to form1 picturebox to draw my map.

My best regards...
Tags: C#, Windows Forms, Controls, Event

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900