Click here to Skip to main content
15,883,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted
Updated 17-Nov-11 7:06am
v3
Comments
BillWoodruff 17-Nov-11 11:08am    
Do you wish this rectangle drawn in Form1's PictureBox to remain visible always, once it is drawn ? You are speaking of drawing inside the picturebox: not drawing a border around the picturebox ?
Un_NaMeD 17-Nov-11 13:08pm    
Hi sir.
I updated my question.
Could you please have look?

Assuming you got your handlers right and you actually signalled the event, and handled it in the correct instance of Form1, then I assume your code was something along the lines of:
C#
void Form1_DrawIt(object sender, EventArgs e)
   {
   using (Graphics g = Graphics.FromImage(pictureBox1.Image))
       {
       g.DrawLine( new Pen(Color.Red, 3.0F), new Point(0,0), new Point(100,100));
       }
   }
but it didn't draw anything on the PictureBox?
Try invalidating the picture box - that forces a re-draw and will show the line:
C#
void Form1_DrawIt(object sender, EventArgs e)
   {
   using (Graphics g = Graphics.FromImage(pictureBox1.Image))
       {
       g.DrawLine( new Pen(Color.Red, 3.0F), new Point(0,0), new Point(100,100));
       }
   pictureBox1.Invalidate();
   }
 
Share this answer
 
Comments
Un_NaMeD 17-Nov-11 13:07pm    
Hi sir.
I updated my question.
Could you please have look?
Handle the PictureBox's Paint event and use the Graphics object to draw directly on it.
 
Share this answer
 
Comments
Un_NaMeD 17-Nov-11 10:48am    
Hey sir.
in Which form should I handle PictureBox Paint event?
Could you please give a code work or more explanation?
Thank you.
Nish Nishant 17-Nov-11 10:51am    
The form that contains the PictureBox control. I don't have any sample code right now.
Un_NaMeD 17-Nov-11 13:08pm    
Hi sir.
Thank you for time sharing
I updated my question.
Could you please have look?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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