how can i draw a rectangle from a separate antoher winForm?
-
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)
-
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)
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 thesecond 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
-
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)
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)