REA_ANDREW
asked on
ParentForm Question
Hi,
I am really looking for the best way to achieve this as I cannot undertsnad how I would approach it.
In my Form1 Code, I create a class, which is NOT nested. In this class I use ParentForm, thinking that upon Initializing the class in the Form Class OnLoad that it would know that Form1 is the ParentForm. Obviously I found that this did not work. So my question is,
Is passing the Form itself as an overload the only way to reference the form I want? Or is there a way where the class can adapt and realise that the parent form is the one whichg intialized it.
Here is my class, and how I have tried to get the parentForm
public class BlackBoard : Form
{
private Form _ParentForm;
public BlackBoard()
{
_ParentForm = ParentForm;
}
public void DrawBlackBoard()
{
Graphics g1 = CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, _ParentForm.Width - 20, _ParentForm.Height - 160);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
g1.FillRectangle(BlackPain t, BlackBoard);
}
}
I am really looking for the best way to achieve this as I cannot undertsnad how I would approach it.
In my Form1 Code, I create a class, which is NOT nested. In this class I use ParentForm, thinking that upon Initializing the class in the Form Class OnLoad that it would know that Form1 is the ParentForm. Obviously I found that this did not work. So my question is,
Is passing the Form itself as an overload the only way to reference the form I want? Or is there a way where the class can adapt and realise that the parent form is the one whichg intialized it.
Here is my class, and how I have tried to get the parentForm
public class BlackBoard : Form
{
private Form _ParentForm;
public BlackBoard()
{
_ParentForm = ParentForm;
}
public void DrawBlackBoard()
{
Graphics g1 = CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, _ParentForm.Width - 20, _ParentForm.Height - 160);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
g1.FillRectangle(BlackPain
}
}
ASKER
Thank you. I now have this but it does not draw the image. If I take it out the class and phycially code the graphic creation in the form load it works but not this way. Could you tell me where I am going wrong please
CLASS:
************************** ********** ********** ********** **
public class BlackBoard : Form
{
private Form _ParentForm;
public BlackBoard(Form FormTarget)
{
this._ParentForm = FormTarget;
}
public void DrawBlackBoard()
{
Graphics g1 = CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, 150, 150);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
Pen BlackPen = new Pen(Color.Black);
BlackPen.DashPattern = new float[] { 4.0F, 2.0F, 1.0F, 3.0F };
g1.FillRectangle(BlackPain t, BlackBoard);
g1.DrawRectangle(BlackPen, BlackBoard);
}
}
FORM LOAD
************************** ********** ********** *********
//This does not draw the rectangle
private void Form1_Load(object sender, EventArgs e)
{
BlackBoard b = new BlackBoard(this);
b.DrawBlackBoard();
}
CLASS:
**************************
public class BlackBoard : Form
{
private Form _ParentForm;
public BlackBoard(Form FormTarget)
{
this._ParentForm = FormTarget;
}
public void DrawBlackBoard()
{
Graphics g1 = CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, 150, 150);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
Pen BlackPen = new Pen(Color.Black);
BlackPen.DashPattern = new float[] { 4.0F, 2.0F, 1.0F, 3.0F };
g1.FillRectangle(BlackPain
g1.DrawRectangle(BlackPen,
}
}
FORM LOAD
**************************
//This does not draw the rectangle
private void Form1_Load(object sender, EventArgs e)
{
BlackBoard b = new BlackBoard(this);
b.DrawBlackBoard();
}
I'm not sure why it's not drawing.
where d'u want the to draw the black board in parent for rt? then
Graphics g1 = _ParentForm .CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, 150, 150);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
Pen BlackPen = new Pen(Color.Black);
BlackPen.DashPattern = new float[] { 4.0F, 2.0F, 1.0F, 3.0F };
g1.FillRectangle(BlackPain t, BlackBoard);
g1.DrawRectangle(BlackPen, BlackBoard);
_ParentForm.Invlidate()
Graphics g1 = _ParentForm .CreateGraphics();
Rectangle BlackBoard = new Rectangle(10, 150, 150, 150);
SolidBrush BlackPaint = new SolidBrush(Color.Black);
Pen BlackPen = new Pen(Color.Black);
BlackPen.DashPattern = new float[] { 4.0F, 2.0F, 1.0F, 3.0F };
g1.FillRectangle(BlackPain
g1.DrawRectangle(BlackPen,
_ParentForm.Invlidate()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could add your BlackBoard instance to your main for as an owned form :
BlackBoard b = new BlackBoard();
this.AddOwnedForm(b);
Or create a constructor in yout BlackBoard class like this :
public BlackBoard(Form parentForm) : base(){
this._ParentForm = parentForm;
}
and the call it like this :
BlackBoard b = new BlackBoard(this);