toolbox and usercontrol problem

  • Thread starter Thread starter steve bull
  • Start date Start date
S

steve bull

I have created a UserControl and added it to my toolbox. It appears in
the toolbox with the icon I created for it. The problem is that when I
drag the control onto a form no code is generated. If I manually add
the code for creating the control to the form everything works fine. I
have created other controls w/o problems and cannot see anything I am
doing different in this case.

Does anyone have any idea what I could be doing wrong?

Thanks
 
Can you post the code for your UserControl?


well, this is the whole thing. I took the control from the colorpicker control on the MSDN website. I have changed it
very little so far. The original came with a form demo. I just moved the logic for creating the gradient into the
constructor from the form, added the Toolbox statements and moved the Event handler into the main module.

The control is a Photoshop style color slider taken from :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/colorpicker.asp

The color slider was just one of the controls inside the colorpicker control. It is basically a rectangle with a
gradient fill and 2 triangles on either side of it that shows the color level. Of red at the moment.

Thanks



using System;
using System.Data;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;



namespace NewControls
{
public delegate void ValueChangedHandler(object sender, ValueChangedEventArgs e);


public class ValueChangedEventArgs : EventArgs
{
private int m_value;

public int Value
{
get { return m_value; }
}

public ValueChangedEventArgs( int newValue )
{
m_value = newValue;
}
}




[ToolboxItem(true)]
[ToolboxBitmap(typeof(ColorSlider),@"D:\Development\NewControls\ColorSlider.bmp")]

public class ColorSlider : System.Windows.Forms.UserControl
{
public event ValueChangedHandler ValueChanged;

// private data members

private bool m_isLeftMouseButtonDown;
private bool m_isLeftMouseButtonDownAndMoving;
private int m_currentArrowYLocation;
private Rectangle m_leftArrowRegion;
private Rectangle m_rightArrowRegion;


// readonly

private readonly Rectangle m_colorRegion = new Rectangle( 13, 7, 18, 256 );
private readonly Rectangle m_outerRegion = new Rectangle( 10, 4, 26, 264 );
private readonly Bitmap m_colorBitmap = new Bitmap( 18, 256 );


// constants

private const int POINTER_HEIGHT = 10;
private const int POINTER_WIDTH = 6;


/// <summary>
/// Constructor.
/// </summary>

public ColorSlider() : base()
{
m_currentArrowYLocation = m_colorRegion.Top;

using ( Graphics g = Graphics.FromImage(ColorBitmap ) )
{
Color startColor = Color.FromArgb( 0, 0, 0 );
Color endColor = Color.FromArgb( 255, 0, 0 );

Rectangle region = new Rectangle( 0, 0, 18, 300 );
using (LinearGradientBrush lgb = new LinearGradientBrush(region,

startColor,

endColor,

270f))
{
g.FillRectangle( lgb, region );
}
}

} /* End of ColorSlider() */



/// <summary>
/// Gets or sets the color slider bitmap.
/// </summary>


public Bitmap ColorBitmap
{
get { return m_colorBitmap; }


} /* End of ColorBitmap */



protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);

using ( Graphics g = e.Graphics )
{
CreateLeftTrianglePointer( g, m_currentArrowYLocation );
CreateRightTrianglePointer( g, m_currentArrowYLocation );


if ( m_colorBitmap != null )
{
g.DrawImage( m_colorBitmap, m_colorRegion );
}


ControlPaint.DrawBorder3D( g, m_outerRegion );
g.DrawRectangle( Pens.Black, m_colorRegion );

}

} /* End of OnPaint(PaintEventArgs) */



protected override void OnMouseDown(MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
{
m_isLeftMouseButtonDown = true;
CheckCursorYRegion( e.Y );
}

base.OnMouseDown (e);

} /* End of OnMouseDown(MouseEventArgs) */



protected override void OnMouseMove(MouseEventArgs e)
{
if ( m_isLeftMouseButtonDown )
{
m_isLeftMouseButtonDownAndMoving = true;
CheckCursorYRegion( e.Y );
}

base.OnMouseMove (e);

} /* End of OnMouseMove(MouseEventArgs) */



protected override void OnMouseUp(MouseEventArgs e)
{
m_isLeftMouseButtonDown = false;
m_isLeftMouseButtonDownAndMoving = false;

base.OnMouseUp (e);

} /* End of OnMouseUp(MouseEventArgs) */




/// <summary>
/// Calculates the points needed to draw the left triangle pointer for
/// the value strip.
/// </summary>
/// <param name="g">Graphics object.</param>
/// <param name="y">Current cursor y-value.</param>


private void CreateLeftTrianglePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRegion.Left - POINTER_WIDTH - 1, y - (POINTER_HEIGHT / 2)),
new Point(m_outerRegion.Left - 2, y),
new Point(m_outerRegion.Left - POINTER_WIDTH - 1,
y + (POINTER_HEIGHT / 2))};

g.DrawPolygon( Pens.Black, points );

}


/// <summary>
/// Calculates the points needed to draw the right triangle pointer for
/// the color slider.
/// </summary>
/// <param name="g">Graphics object.</param>
/// <param name="y">Current cursor y-value.</param>

private void CreateRightTrianglePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRegion.Right - 1 + POINTER_WIDTH, y - (POINTER_HEIGHT / 2)),
new Point(m_outerRegion.Right, y),
new Point(m_outerRegion.Right - 1 + POINTER_WIDTH,
y + (POINTER_HEIGHT/2))};

g.DrawPolygon( Pens.Black, points );

}

/// <summary>
/// Determines the color slider left triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">Current cursor y-value.</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>


private Rectangle GetLeftTrianglePointerInvalidationRegion(int arrowY)
{
int leftPadding = POINTER_WIDTH + 2;
int x = m_outerRegion.Left - leftPadding;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;

return new Rectangle(x, y, width, height);


} /* End of GetLeftTrianglePointerInvalidationRegion(int) */



/// <summary>
/// Determines the color slider right triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">Current cursor y-value</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>

private Rectangle GetRightTrianglePointerInvalidationRegion( int arrowY )
{
int x = m_outerRegion.Right;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;

return new Rectangle( x, y, width, height );


} /* End of GetRightTrianglePointerInvalidationRegion(int) */




private void CheckCursorYRegion( int y )
{
int mValue = y;

if ( m_isLeftMouseButtonDown && !m_isLeftMouseButtonDownAndMoving )
{
if ( y < m_colorRegion.Top || y > m_colorRegion.Bottom )
return;
}
else if ( m_isLeftMouseButtonDownAndMoving )
{
if ( y < m_colorRegion.Top )
mValue = m_colorRegion.Top;
else if ( y >= m_colorRegion.Bottom )
mValue = m_colorRegion.Bottom - 1;

}
else
return;

m_currentArrowYLocation = mValue;

InvalidateArrowRegions( mValue );


if ( ValueChanged != null )
ValueChanged(this, new ValueChangedEventArgs(255 - (mValue - m_colorRegion.Top)));

} /* End of CheckCursorYRegion(int) */


private void InvalidateArrowRegions(int y)
{
this.Invalidate( m_leftArrowRegion );
this.Invalidate( m_rightArrowRegion );

m_leftArrowRegion = this.GetLeftTrianglePointerInvalidationRegion( y );
m_rightArrowRegion = this.GetRightTrianglePointerInvalidationRegion( y );

this.Invalidate(m_leftArrowRegion);
this.Invalidate(m_rightArrowRegion);

} /* End of InvalidateArrowRegions(int) */


} /* End of public class ColorSlider */





} /* End of namespace NewControls
 
I included the following line to get the code to compile:
using System.ComponentModel;

I then added the UserControl to the ToolBox by right-clicking the ToolBox,
choosing the "Add/Remove Items..." option, and locating the assembly.

When I drag and drop the control onto the Form it shows up fine and
generates the proper code in the InitializeComponent() method. So it seems
to work ok at my end.

--
Tim Wilson
..Net Compact Framework MVP

steve bull said:
well, this is the whole thing. I took the control from the colorpicker
control on the MSDN website. I have changed it
very little so far. The original came with a form demo. I just moved the
logic for creating the gradient into the
constructor from the form, added the Toolbox statements and moved the
Event handler into the main module.
The control is a Photoshop style color slider taken from :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/colorpicker.asp

The color slider was just one of the controls inside the colorpicker
control. It is basically a rectangle with a
gradient fill and 2 triangles on either side of it that shows the color level. Of red at the moment.

Thanks



using System;
using System.Data;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;



namespace NewControls
{
public delegate void ValueChangedHandler(object sender, ValueChangedEventArgs e);


public class ValueChangedEventArgs : EventArgs
{
private int m_value;

public int Value
{
get { return m_value; }
}

public ValueChangedEventArgs( int newValue )
{
m_value = newValue;
}
}




[ToolboxItem(true)]
[ToolboxBitmap(typeof(ColorSlider),@"D:\Development\NewControls\ColorSlider.
bmp")]

public class ColorSlider : System.Windows.Forms.UserControl
{
public event ValueChangedHandler ValueChanged;

// private data members

private bool m_isLeftMouseButtonDown;
private bool m_isLeftMouseButtonDownAndMoving;
private int m_currentArrowYLocation;
private Rectangle m_leftArrowRegion;
private Rectangle m_rightArrowRegion;


// readonly

private readonly Rectangle m_colorRegion
= new Rectangle( 13, 7, 18, 256 );
private readonly Rectangle m_outerRegion
= new Rectangle( 10, 4, 26, 264 );
private readonly Bitmap m_colorBitmap = new Bitmap( 18, 256 );


// constants

private const int POINTER_HEIGHT = 10;
private const int POINTER_WIDTH = 6;


/// <summary>
/// Constructor.
/// </summary>

public ColorSlider() : base()
{
m_currentArrowYLocation = m_colorRegion.Top;

using ( Graphics g =
Graphics.FromImage(ColorBitmap ) )
{
Color startColor = Color.FromArgb( 0, 0, 0 );
Color endColor = Color.FromArgb( 255, 0, 0 );

Rectangle region = new Rectangle( 0, 0, 18, 300 );
using (LinearGradientBrush lgb = new LinearGradientBrush(region,

startColor,

endColor,

270f))
{
g.FillRectangle( lgb, region );
}
}

} /* End of ColorSlider() */



/// <summary>
/// Gets or sets the color slider bitmap.
/// </summary>


public Bitmap ColorBitmap
{
get { return m_colorBitmap; }


} /* End of ColorBitmap */



protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);

using ( Graphics g = e.Graphics )
{
CreateLeftTrianglePointer( g, m_currentArrowYLocation );
CreateRightTrianglePointer( g, m_currentArrowYLocation );


if ( m_colorBitmap != null )
{
g.DrawImage( m_colorBitmap, m_colorRegion );
}


ControlPaint.DrawBorder3D( g, m_outerRegion );
g.DrawRectangle( Pens.Black, m_colorRegion );

}

} /* End of OnPaint(PaintEventArgs) */



protected override void OnMouseDown(MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
{
m_isLeftMouseButtonDown = true;
CheckCursorYRegion( e.Y );
}

base.OnMouseDown (e);

} /* End of OnMouseDown(MouseEventArgs) */



protected override void OnMouseMove(MouseEventArgs e)
{
if ( m_isLeftMouseButtonDown )
{
m_isLeftMouseButtonDownAndMoving = true;
CheckCursorYRegion( e.Y );
}

base.OnMouseMove (e);

} /* End of OnMouseMove(MouseEventArgs) */



protected override void OnMouseUp(MouseEventArgs e)
{
m_isLeftMouseButtonDown = false;
m_isLeftMouseButtonDownAndMoving = false;

base.OnMouseUp (e);

} /* End of OnMouseUp(MouseEventArgs) */




/// <summary>
/// Calculates the points needed to draw the left triangle pointer for
/// the value strip.
/// </summary>
/// <param name="g">Graphics object.</param>
/// <param name="y">Current cursor y-value.</param>


private void CreateLeftTrianglePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRegion.Left -
POINTER_WIDTH - 1, y - (POINTER_HEIGHT / 2)),
Point(m_outerRegion.Left - 2, y),
Point(m_outerRegion.Left - POINTER_WIDTH - 1,
y + (POINTER_HEIGHT / 2))};

g.DrawPolygon( Pens.Black, points );

}


/// <summary>
/// Calculates the points needed to draw the right triangle pointer for
/// the color slider.
/// </summary>
/// <param name="g">Graphics object.</param>
/// <param name="y">Current cursor y-value.</param>

private void CreateRightTrianglePointer( Graphics g, int y )
{
Point[] points = {new Point(m_outerRegion.Right - 1
+ POINTER_WIDTH, y - (POINTER_HEIGHT / 2)),
new Point(m_outerRegion.Right, y),
new
Point(m_outerRegion.Right - 1 + POINTER_WIDTH,
y + (POINTER_HEIGHT/2))};

g.DrawPolygon( Pens.Black, points );

}

/// <summary>
/// Determines the color slider left triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">Current cursor y-value.</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>


private Rectangle
GetLeftTrianglePointerInvalidationRegion(int arrowY)
{
int leftPadding = POINTER_WIDTH + 2;
int x = m_outerRegion.Left - leftPadding;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;

return new Rectangle(x, y, width, height);


} /* End of
GetLeftTrianglePointerInvalidationRegion(int) */
/// <summary>
/// Determines the color slider right triangle pointer invalidation
/// region.
/// </summary>
/// <param name="arrowY">Current cursor y-value</param>
/// <returns>A rectangle object representing the area to be
/// invalidated.</returns>

private Rectangle
GetRightTrianglePointerInvalidationRegion( int arrowY )
{
int x = m_outerRegion.Right;
int y = arrowY - ( POINTER_HEIGHT / 2 ) - 1;
int width = POINTER_WIDTH + 2;
int height = POINTER_HEIGHT + 3;

return new Rectangle( x, y, width, height );


} /* End of
GetRightTrianglePointerInvalidationRegion(int) */
private void CheckCursorYRegion( int y )
{
int mValue = y;

if ( m_isLeftMouseButtonDown &&
!m_isLeftMouseButtonDownAndMoving )
{
if ( y < m_colorRegion.Top || y > m_colorRegion.Bottom )
return;
}
else if ( m_isLeftMouseButtonDownAndMoving )
{
if ( y < m_colorRegion.Top )
mValue = m_colorRegion.Top;
else if ( y >= m_colorRegion.Bottom )
mValue = m_colorRegion.Bottom - 1;

}
else
return;

m_currentArrowYLocation = mValue;

InvalidateArrowRegions( mValue );


if ( ValueChanged != null )
ValueChanged(this, new
ValueChangedEventArgs(255 - (mValue - m_colorRegion.Top)));
} /* End of CheckCursorYRegion(int) */


private void InvalidateArrowRegions(int y)
{
this.Invalidate( m_leftArrowRegion );
this.Invalidate( m_rightArrowRegion );

m_leftArrowRegion =
this.GetLeftTrianglePointerInvalidationRegion( y );
m_rightArrowRegion =
this.GetRightTrianglePointerInvalidationRegion( y );
 
I just deleted all my manual coding and tried it again.this time it worked. I have no idea why it didn't work yesterday.
I tried it quite a few times.

Thanks for your help.

Steve
 
Back
Top