2 Controls overlap

M

Martijn Mulder

I want 2 UserControls with Dock=DockStyle.Fill share the same space in a
Form, ie Paint them on top of each other.

I made the UserControls keep the Background intact, but only one shows up.
See the code below:


//using...
using System.Drawing;
using System.Windows.Forms;


//class Circle
class Circle: UserControl
{

//constructor
public Circle()
{
Dock= DockStyle.Fill;
}

//method OnPaint
override protected void OnPaint( PaintEventArgs e)
{
e.Graphics.FillEllipse( Brushes.Yellow, new Rectangle(5, 5, 50, 50));
}

//method OnPaintBackground
override protected void OnPaintBackground( PaintEventArgs e)
{
//Do nothing. Leave Background intact
}
}


//class Square
class Square: UserControl
{

//constructor
public Square()
{
Dock= DockStyle.Fill;
}

//method OnPaint
override protected void OnPaint( PaintEventArgs e)
{
e.Graphics.FillRectangle( Brushes.Red, new Rectangle( 25, 25, 50, 50));
}

//method OnPaintBackground
override protected void OnPaintBackground( PaintEventArgs e)
{
//Do nothing. Leave Background intact
}
}


//class MainForm
class MainForm: Form
{

//constructor
MainForm()
{
Text="Controls Share Space (NOT!)"; // Only the Circle shows!
Controls.Add( new Circle());
Controls.Add( new Square());
}

//method Main
static void Main()
{
Application.Run(new MainForm());
}
}
 
J

Jeff Gaines

I want 2 UserControls with Dock=DockStyle.Fill share the same space in a
Form, ie Paint them on top of each other.

I made the UserControls keep the Background intact, but only one shows up.
See the code below:

[code snipped]

That is the behaviour I would expect. If I put a plate on a table then put
another plate on top of the first plate I would only be able to see the
top plate when looking down at them.
 
M

Martijn Mulder

That is the behaviour I would expect. If I put a plate on a table then put
another plate on top of the first plate I would only be able to see the
top plate when looking down at them.

So it is by design? It is not like stacking glass plates, one with green
eggs, the other with ham. So that when I look from above, I see green eggs
and ham?
 
J

Jeff Gaines

So it is by design? It is not like stacking glass plates, one with green
eggs, the other with ham. So that when I look from above, I see green eggs
and ham?

The plate analogy is making me feel sick now :)

I would say it is by design, the top most item covers what is underneath.
Can you set the transparency on the controls? I've never done it but it
may give you the 'glass plate' effect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top