Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using custom controls 1

Status
Not open for further replies.

rdgerken

Technical User
Jul 8, 2002
108
I downloaded a custom component. Basically its a text label that allows you to orient the text in different directions. It is inherited from System.Windows.Forms.Label. I added it to my solution, but it doesn't show up in my toolbox. How do you add custom components so that you can drag them onto your forms? I notice that the IDE will automatically add the classes that inherit from System.Windows.Forms.UserControl to your toolbox, but I haven't figured out how custom components are added to your toolbox. Thanks.
 
rdgerken,

the IDE is sometimes unable to display the control in the toolbox. Not for any good reason... just because...

Try opening the control and compiling with the control open. It may show up under "My Usercontrols" in the toolbox.

Also: make sure the class is under the same namespace as your project.
 
Ok,

Thanks for the input. What I'm trying to do is instead of compiling the component as a dll, and adding that as a resource to my solution, I'm instead copying the source code for the component into my solution, and adding references to it at run time, I just don't know how to add it at design time, since I do not have the item in my toolbox. Is there a way to do what I'm asking, or do you have to have it available as an already compiled dll to make use of it at design time? I'd like to keep the number of external dll's to a minimum.
 
if you copy the code into your solution, you should be able to see it in the toolbox.

Just make sure that the namespace is the same as your project namespace. Check this by opening one of the forms already in your project. At the top you will see

using System;
using System.Windows.Forms;
//and a few others

namespace someNameSpaceName
{


at the top of the new component, put the same namespace name as your form.
Then rebuild (ctrl+shift+b)

Now look in your toolbox (under the "My User Controls" section)

If this doesn't work, you can send me your code.
 
Here is the custom component I'm using (obtained from - courtesy Chester Ragel)

I can only get this thing in my toolbox if I add its compiled dll as a reference to my solution. I would like to be able to just add the source code to my project but still be able to have the component in my toolbox. Maybe I can just use a reference at design time, then remove it when I'm done designing, and let it compile it with the solution? I'm not sure. I did make sure that the namespace was the same as the rest of my classes.

Thanks for taking the time to look at this for me.

using System;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace WindowsApplication3
{
/// <summary>
/// This is a label, in which you can set the text in any direction/angle
/// </summary>

#region Orientation

//Orientation of the text

public enum Orientation
{
Circle,
Arc,
Rotate
}

public enum Direction
{
Clockwise,
AntiClockwise
}


#endregion

public class OrientedTextLabel : System.Windows.Forms.Label
{

#region Variables

private double rotationAngle;
private string text;
private Orientation textOrientation;
private Direction textDirection;

#endregion

#region Constructor

public OrientedTextLabel()
{
//Setting the initial condition.
rotationAngle = 0d;
textOrientation = Orientation.Rotate;
this.Size = new Size(105,12);
}

#endregion

#region Properties

[Description("Rotation Angle"),Category("Appearance")]
public double RotationAngle
{
get
{
return rotationAngle;
}
set
{

rotationAngle = value;
this.Invalidate();
}
}

[Description("Kind of Text Orientation"),Category("Appearance")]
public Orientation TextOrientation
{
get
{
return textOrientation;
}
set
{

textOrientation = value;
this.Invalidate();
}
}

[Description("Direction of the Text"),Category("Appearance")]
public Direction TextDirection
{
get
{
return textDirection;
}
set
{

textDirection = value;
this.Invalidate();
}
}

[Description("Display Text"),Category("Appearance")]
public override string Text
{
get
{
return text;
}
set
{
text = value;
this.Invalidate();
}
}

#endregion

#region Method

protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.Trimming = StringTrimming.None;

Brush textBrush = new SolidBrush(this.ForeColor);

//Getting the width and height of the text, which we are going to write
float width = graphics.MeasureString(text,this.Font).Width;
float height = graphics.MeasureString(text,this.Font).Height;

//The radius is set to 0.9 of the width or height, in order not to
//hide any part of the text at any stage
float radius = 0f;
if(ClientRectangle.Width<ClientRectangle.Height)
{
radius = ClientRectangle.Width *0.9f/2;
}
else
{
radius = ClientRectangle.Height *0.9f/2;
}

//Set the text according to the selection
switch(textOrientation)
{
case Orientation.Arc :
{
//Arc angle must be obtained from the length of the text.
float arcAngle = (2*width/radius)/text.Length;
if(textDirection == Direction.Clockwise)
{
for(int i=0;i<text.Length;i++)
{

graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
(float)(radius*(1 - Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text.ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for(int i=0;i<text.Length;i++)
{

graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 - (float)rotationAngle - 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text.ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();

}
}
break;
}
case Orientation.Circle :
{
if(textDirection == Direction.Clockwise)
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 - Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
graphics.DrawString(text.ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 - (float)rotationAngle - (360/text.Length)*i);
graphics.DrawString(text.ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}

}
break;
}
case Orientation.Rotate :
{
//For rotation
double angle = (rotationAngle/180)*Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
(ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
graphics.RotateTransform((float)rotationAngle);
graphics.DrawString(text,this.Font,textBrush,0,0);
graphics.ResetTransform();

break;
}
}
}
#endregion

}
}
 
OK...

Found a solution for you... but you may want to know that this control does not seem to work very well anyways.

How are you trying to use this control?/What is this control being used in?




Anyways... to make it show up in the toolbox:

Step 1: open the custom control.

Step 2: Replace "OrientedTextLabel : System.Windows.Forms.Label" with "OrientedTextLabel : System.Windows.Forms.UserControl"

Step 3: Close All Forms/Controls/Classes
Step 4: Recompile
Step 5: Open your form and see the item in the toolbox.

Step 6: Replace "OrientedTextLabel : System.Windows.Forms.UserControl" with "OrientedTextLabel : System.Windows.Forms.Label"

Hope that works for you.
If I get time tonight, and you give me the context in which you are trying to use this control, I may re-write the control for you quickly.

-D
 
Thanks JurkMonkey!

All I really wanted to do with this control was to be able to put vertical text on my form. This control allowed me to specify an angle to accomplish this. Is there an easy straightforward way to do that?
 
I can create a vertical label for you tonight.

Please check back later!

-D
 
This is a Vertical/Horizontal optional label style custom control. Use it all you like in any applications.

You specify:
TextValue (the actual text)
Vertical (true or false)


Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for DirectionalLabel.
	/// </summary>
	public class DirectionalLabel : System.Windows.Forms.UserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string TXT="";
		private bool vertical = true;

		public DirectionalLabel()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			this.SetStyle(
				ControlStyles.AllPaintingInWmPaint |
				ControlStyles.UserPaint |
				ControlStyles.DoubleBuffer,true);
			// TODO: Add any initialization after the InitializeComponent call
			this.Paint+=new PaintEventHandler(DirectionalLabel_Paint);
		}

		//Specify if the text is displayed vertically or horizontally
		public bool Vertical
		{
			get
			{
				return vertical;
			}
			set
			{
				if (value != vertical)
				{
					int width = this.Width;
					int height = this.Height;

					this.Width = height;
					this.Height = width;

					vertical = value;
					this.Invalidate();
				}
			}
		}

		//The Text value of the label
		public string TextValue
		{
			get
			{
				return TXT;
			}
			set
			{
				TXT = value;
				this.Invalidate();
			}
		}


		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// DirectionalLabel
			// 
			this.Name = "DirectionalLabel";
			this.Size = new System.Drawing.Size(24, 128);

		}
		#endregion

		//Draw the label vertically
		private void DirectionalLabel_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
			g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

			StringFormat format = new StringFormat();

			if (vertical)
			{
				format.FormatFlags = StringFormatFlags.DirectionVertical;
			}
			
			g.DrawString(TXT, this.Font, new SolidBrush(this.ForeColor), e.ClipRectangle.X, e.ClipRectangle.Y, format);
		}
	}
}
 
Thanks man... appreciate your help today.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top