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());
}
}
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());
}
}