Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ParentForm Question

Posted on 2006-11-09
5
Medium Priority
?
208 Views
Last Modified: 2010-04-16
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(BlackPaint, BlackBoard);
        }
    }
0
Comment
Question by:REA_ANDREW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17905243
Why is it important for the one form to have the other as a parent ?
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);

0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17905259
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(BlackPaint, 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();
        }
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17905337
I'm not sure why it's not drawing.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17905749
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(BlackPaint, BlackBoard);
            g1.DrawRectangle(BlackPen, BlackBoard);
            _ParentForm.Invlidate()
     
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 17906844
CreateGraphics() creates a temporary graphics.  As soon as the Form repaints itself (which you have no control over) the drawing is erased.

Pass the "e.Graphics" from the Forms Paint() event into your Blackboards DrawBlackBoard() event and use that graphics instead.  Then whenever the form gets refreshed, your drawing also gets refreshed and therefore becomes "persistent"...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question