Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can i draw a rectangle from a separate antoher winForm?

how can i draw a rectangle from a separate antoher winForm?

Scheduled Pinned Locked Moved C#
graphicsquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maifs
    wrote on last edited by
    #1

    i made 3 forms . 1.. frmMain(it is the mdiContainer) (form) 2........frmEditable (form) 3....... frmTools (form) now i just want that when i click on the rectangle button from frmTools form a rectangle must should be drawn on frmEditable form. namespace I_M_Editor { public partial class frmMain : Form { Bitmap m = new Bitmap(2, 2); double Zoom = 1.0f; // PictureBox pictureBox1=null; IDrawableImages img = null; // IDrawableShapes shp= null; public frmMain() { InitializeComponent(); img = new Images(); } private void frmMain_Load(object sender, EventArgs e) { frmToolBox frm = new frmToolBox(); frm.MdiParent = this; frm.Show(); frmEditable frm2 = new frmEditable(); frm2.MdiParent = this; frm2.Show(); } protected override void OnPaint(PaintEventArgs e) { } private void tsxOptionOpen_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (DialogResult.OK == openFileDialog.ShowDialog()) { m = (Bitmap)Bitmap.FromFile(openFileDialog.FileName); } } public void Rect() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 260, 260, 100, 100); } } } namespace I_M_Editor { public partial class frmEditable : Form { public frmEditable() { InitializeComponent(); } private void frmEditable_Load(object sender, EventArgs e) { } } } namespace I_M_Editor { public partial class frmToolBox : Form { public frmToolBox() { InitializeComponent(); } private void lblRect_Click(object sender, EventArgs e)

    Z D 2 Replies Last reply
    0
    • M maifs

      i made 3 forms . 1.. frmMain(it is the mdiContainer) (form) 2........frmEditable (form) 3....... frmTools (form) now i just want that when i click on the rectangle button from frmTools form a rectangle must should be drawn on frmEditable form. namespace I_M_Editor { public partial class frmMain : Form { Bitmap m = new Bitmap(2, 2); double Zoom = 1.0f; // PictureBox pictureBox1=null; IDrawableImages img = null; // IDrawableShapes shp= null; public frmMain() { InitializeComponent(); img = new Images(); } private void frmMain_Load(object sender, EventArgs e) { frmToolBox frm = new frmToolBox(); frm.MdiParent = this; frm.Show(); frmEditable frm2 = new frmEditable(); frm2.MdiParent = this; frm2.Show(); } protected override void OnPaint(PaintEventArgs e) { } private void tsxOptionOpen_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (DialogResult.OK == openFileDialog.ShowDialog()) { m = (Bitmap)Bitmap.FromFile(openFileDialog.FileName); } } public void Rect() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 260, 260, 100, 100); } } } namespace I_M_Editor { public partial class frmEditable : Form { public frmEditable() { InitializeComponent(); } private void frmEditable_Load(object sender, EventArgs e) { } } } namespace I_M_Editor { public partial class frmToolBox : Form { public frmToolBox() { InitializeComponent(); } private void lblRect_Click(object sender, EventArgs e)

      Z Offline
      Z Offline
      zafersavas
      wrote on last edited by
      #2

      Your problem is not with the drawing part, you want to pass data from one form to another form I think. i.e: You want to know that a rectangle was selected in the first form and draw a rectangle in the second form. So the easiest way to exchange data between forms is to pass the reference of one form to the other form that is:

      // Pass the reference of Form1 to the Form2 during Form2 construction:
      Form1 form1Ref;
      public Form2(Form1 fRef)
      {
      InitializeComponent();
      form1Ref = fRef;
      }

      After this you can reach the public member variables/function of form1 from form2. So if you keep the info of selected settings in a variable in form1 you can get them from form2. PS: I recognized that unnecessary white spaces and unindented codes really disturb me. Why don't you tidy your code :) Good luck zafer

      1 Reply Last reply
      0
      • M maifs

        i made 3 forms . 1.. frmMain(it is the mdiContainer) (form) 2........frmEditable (form) 3....... frmTools (form) now i just want that when i click on the rectangle button from frmTools form a rectangle must should be drawn on frmEditable form. namespace I_M_Editor { public partial class frmMain : Form { Bitmap m = new Bitmap(2, 2); double Zoom = 1.0f; // PictureBox pictureBox1=null; IDrawableImages img = null; // IDrawableShapes shp= null; public frmMain() { InitializeComponent(); img = new Images(); } private void frmMain_Load(object sender, EventArgs e) { frmToolBox frm = new frmToolBox(); frm.MdiParent = this; frm.Show(); frmEditable frm2 = new frmEditable(); frm2.MdiParent = this; frm2.Show(); } protected override void OnPaint(PaintEventArgs e) { } private void tsxOptionOpen_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (DialogResult.OK == openFileDialog.ShowDialog()) { m = (Bitmap)Bitmap.FromFile(openFileDialog.FileName); } } public void Rect() { Graphics g = this.CreateGraphics(); Pen p = new Pen(Color.Red); g.DrawRectangle(p, 260, 260, 100, 100); } } } namespace I_M_Editor { public partial class frmEditable : Form { public frmEditable() { InitializeComponent(); } private void frmEditable_Load(object sender, EventArgs e) { } } } namespace I_M_Editor { public partial class frmToolBox : Form { public frmToolBox() { InitializeComponent(); } private void lblRect_Click(object sender, EventArgs e)

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Inter-child communication should be done through the parent form. This can be done with events and/or delegates. Set up some events in frmTools, and after creating an instance of it in frmMain, subscribe to it's events. Now that frmMain is listening to frmTools - it can pass the instructions onto your frmEditable instance which you can find in frmMain's MdiChildren collection.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Expect everything to be hard and then enjoy the things that come easy. (code-frog)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups