views:

54

answers:

2

Hi,

I have written a very simple control. C# Visual Studio 2008. Its output should be, and is a dll. I have added a reference to the dll within the project that I intend to use it with. The msdn article about how to write a control states that it should appear in the 'Add reference / projects' list, which it doesn't but I simply navigated to it under the 'browse' tab, went to the /bin folder and added the reference that way. I dragged it over to my toolbox, but it shows up as a 'Text:xhair_tool' and when i try and add it to a form, it won't, so what have I done wrong? It was created as a 'Windows forms control' project. It should export the one method which is 'Target' which return an array, as shown below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace xhair_tool
{
public partial class xhair : UserControl
{
    public xhair()
    {
        InitializeComponent();
    }

    private void xhair_Load(object sender, EventArgs e)
    {

    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Pen pen = new Pen(Color.Black, 1);
        SolidBrush redBrush = new SolidBrush(Color.Red);
        g.DrawLine(pen, 8, 0, 8, 7);
        g.DrawLine(pen, 8, 9, 8, 16);
        g.DrawLine(pen, 0, 8, 7, 8);
        g.DrawLine(pen, 9, 8, 16, 8);

        //ControlPaint.DrawReversibleLine(start, end, backColor)
    }
    /// <summary>
    /// Returns the point at the center of the crosshair
    /// </summary>
    /// <returns>int[x,y]</returns>
    public int[] Target
    {
        get
        {
            int[] _xy = new int[2];
            _xy[0] = this.Left + 8;
            _xy[1] = this.Top + 8;
            return _xy;
        }
    }

}

}

Thanks, R.

A: 

Right-click the toolbox, click Choose Items, and add your control.

Some recommendations for the control:

  • Target should be a Point, not an int[].
  • UserControl is designed for controls that contain other controls. Since yours does not, you should inherit from Control. See here.
  • You should set the following styles:

        SetStyle(ControlStyles.AllPaintingInWmPaint
               | ControlStyles.UserPaint
               | ControlStyles.Opaque
               | ControlStyles.OptimizedDoubleBuffer
               | ControlStyles.ResizeRedraw,
                 true);
    
SLaks
Where do I set the style?
flavour404
In the constructor.
SLaks
Ok thanks, it still acting funky in that it is not repainting the background on each move, instead the background is set, which is odd and annoying.
flavour404
One thing is that Control does not support transparent backgrounds, however this is causing my control not to update until it is dropped. How do you get your control to repaint its background to reflect that of its container, in effect creating a transparent background?
flavour404
Controls don't really support transparent backgrounds. The most effective way to do this is to create a transparent click-through form.
SLaks
Would you like to expand on that subject a little?Thanks.
flavour404
A: 

The "drag it over to my toolbox" part cannot work. Right-click the toolbox and select "Choose Items", use the Browse tab.

You are much better off just adding the UserControl to your project instead of putting it in a DLL by itself. When you do, it automatically appears at the top of the toolbox after you compile.

Hans Passant
Or if you put it in a different project in the same solution.
SLaks
@SLaks, I frequently do this for my "Utilities" library that has a lot of functions and controls I use frequently. It also helps when you need to debug and the error is happening inside the control because something you passed it.
Scott Chamberlain
@Scott: What do you mean?
SLaks
I was just commenting that what you explained is what I do too for my projects.
Scott Chamberlain