473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

toolbox and usercontrol problem


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
Nov 16 '05 #1
4 2125
Can you post the code for your UserControl?

--
Tim Wilson
..Net Compact Framework MVP

"steve bull" <bu****@comcast.net> wrote in message
news:up********************************@4ax.com...

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

Nov 16 '05 #2


On Mon, 21 Mar 2005 22:19:06 -0500, "Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote:
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/de...olorpicker.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

Nov 16 '05 #3
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" <bu****@comcast.net> wrote in message
news:97********************************@4ax.com...


On Mon, 21 Mar 2005 22:19:06 -0500, "Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote:
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/de...olorpicker.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

Nov 17 '05 #4

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

On Tue, 22 Mar 2005 08:26:27 -0500, "Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote:
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.

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Smith | last post by:
If i derive from UserControl my Control apears in the toolbox. But I want to extend existing Controls like Button , treecontrol . I found some hints which attributes to set but i didnt succeed till...
2
by: Chien Lau | last post by:
I frequently define internal UserControl-derived classes in my WinForms apps: internal class MyUserControl:UserControl{ ... } I'll often need to embed these controls in a Form, whose class...
3
by: BluDog | last post by:
Hi I have a component that inherits from TreeView, my project is a standard exe. How do i get the component into the toolbox for use within the project? I find that only controls based on...
2
by: Noozer | last post by:
This is a stupid question, I know, but I can't figure it out... Someone please embarass me. I have a solution that has a number of usercontrols included. When I add a usercontrol project to...
10
by: Nak | last post by:
Hi there, I'm having problems with the Toolbox in VB.NET 2002 Standard. I have 2 class libraries, 1 contains licensing classes and references nothing but standard namespaces and the other...
3
by: b747_440 | last post by:
Dear Newsgroup, I'm an old VB6.0 developper who switched some time ago to VB.NET 2005. I really like that new Visual Studio. However, something is going wrong now and I can't figure out, what it...
2
by: sklett | last post by:
I'm trying to add an assembly that has a couple UserControls (among other things) to the toolbar. When I browse to it, I get a message that it's can't find an assembly that it needs...
4
by: Jules Winfield | last post by:
I'm using VS2005. I have a solution consisting of twelve projects. All projects are console/service apps except for one which is a WinForms app. There are no web projects. I'd say that I'm at...
0
by: =?Utf-8?B?TWFyayBDb2xsYXJk?= | last post by:
I've developed VS 2003 usercontrol and I've also created a Setup project for it. I did a search on the internet and found some code to create a .NET 1.1 application to add the usercontrol into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.