Custom Control Help...

A

Adam Hoffman

OK, I'm trying to create a custom control for the Compact Framework
which can best be described as a container of other controls (that is,
it doesn't directly inherit from any single control type, but rather,
is a collection of multiple existing control types working in
harmony). Sort of like the following:

CustomControl.cs
---------------------
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Reflection;

namespace ControlTest
{
/// <summary>
/// Summary description for Line.
/// </summary>
public class ContainerControl : System.Windows.Forms.Control
{
// visual pieces...
private System.Windows.Forms.PictureBox picSliderBackground;
private System.Windows.Forms.PictureBox picSliderHandle;
private System.Drawing.Bitmap objImageSliderBackground;
private System.Drawing.Bitmap objImageSliderHandle;

private string m_strAppDirectory;

public ContainerControl ()
{
// load the application's directory...
GetApplicationDirectory();

this.Size = new Size(200,200);

// create the blank sub-components
this.picSliderHandle = new System.Windows.Forms.PictureBox();
this.picSliderBackground = new System.Windows.Forms.PictureBox();

// set their default positions
this.objImageSliderBackground = new
System.Drawing.Bitmap(this.m_strAppDirectory +
"slider_background.bmp");
this.picSliderBackground.Image =
((System.Drawing.Image)objImageSliderBackground);
this.picSliderBackground.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(this.picSliderBackground);

this.objImageSliderHandle = new
System.Drawing.Bitmap(this.m_strAppDirectory + "slider_handle.bmp");
this.picSliderHandle.Image =
((System.Drawing.Image)objImageSliderHandle);
this.picSliderHandle.Location = new System.Drawing.Point(20, 0);
this.Controls.Add(this.picSliderHandle);

// and make them draw themselves...
this.Invalidate();
}

private void GetApplicationDirectory()
{
string strFullAssemblyPathAndExe =
System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase;
string strFullAssemblyPath = strFullAssemblyPathAndExe.Substring(0,
strFullAssemblyPathAndExe.LastIndexOf("\\") + 1);
this.m_strAppDirectory = strFullAssemblyPath;
}

protected override void OnPaint(PaintEventArgs pe)
{
this.picSliderBackground.Refresh();
this.picSliderHandle.Refresh();
base.OnPaint(pe);
}
}
}

-----------------
OK, now if I instantiate one of these suckers, and expect to have 2
picture boxes show up, I find that only one ever shows up... if I
reverse the order of the create of the controls, and the order that I
add them to the Controls collection, the other one shows up. I can't
make both show up simultaneously.

Additionally, one of the bitmaps is tiny (15x15 pixels) and when that
shows up, it shows up nice and complete. The other one (the
background) is tall (15 x 120 pixels), and when it shows up, it's
truncated...

Anyway, does anybody have a working example of a custom control that
is simply a coordinated collection of other controls?

Thanks much in advance...
 
T

Tim

I don't see any Size properties set for your controls. Perhaps this could be
a contibuter.

You might try starting out building your control as a form, drag on your
controls using the designer and get basic functionality working, then simply
change the inheritance from System.Windows.Forms.Form to
System.Windows.Forms.Control - this has worked well for me. And you
shouldn't need the OnPaint() method like that.

Hope this helps,
Tim
 

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