2

I want to draw a Line between 2 rows while using drag and drop. The function of this is simply visual, so that the user knows, where he is dropping the row. The line should look like the excel onces. Here my code:

        Pen _marqueePen = new Pen(Color.Gray, 2);
        float[] dashValues = {1f,1f};
        _marqueePen.DashPattern = dashValues;

But this looks like that

enter image description here

I want to look it like that:

enter image description here

I'm WinForms and the C1 Flexgrid control.

TaW
  • 53,122
  • 8
  • 69
  • 111
nixn
  • 1,337
  • 3
  • 16
  • 33
  • Can you give us some more context? E.g. what control are you going to draw an (GridView, ...), WinForms or WPF, etc.. – Yves Schelpe Oct 23 '14 at 09:28
  • WinForms. C1 Flexgrid row. I am not drawing any control. I simply drag and drop a row into another row. So that the sort changes. To see where the row would be dropped i need a line between 2 rows. I accomplished that. But as you see in the first picture the visual style of my drawed line by pen is not like that excel line. How do I get that style of line with the Pen class? – nixn Oct 23 '14 at 09:35
  • Edited your questions & tags so people can find your question. Make sure too look at the C1 community as well & ask on their forums: http://our.componentone.com/groups/. The product owners will be more knowledgable on how this can be achieved. – Yves Schelpe Oct 23 '14 at 09:39
  • Dear Schelpe, this is a .Net Problem. I am using the framework, as you see - System.Drawing.. My question is about how to manipulate the Pen objekt to draw a multiple dotted Line. This has nothing to do with C1 – nixn Oct 23 '14 at 09:43
  • But you are trying to draw on a control, at C1 they might have something inside that will let you manipulate the control without you haveing to draw onto the control. If not, then indeed you'll have to use System.Drawing indeed. – Yves Schelpe Oct 23 '14 at 09:45
  • 1
    You need to use the Pen(Brush, float) constructor and pass a TextureBrush. Creating the correct texture is your job. This is otherwise why it is hard to compete with control vendors that have a dozen programmers on staff just by yourself :) – Hans Passant Oct 23 '14 at 10:22
  • Did you resolve your problem? – TaW Oct 29 '14 at 11:25

3 Answers3

5

You can use a Custom Pen like this:

using (Pen pen = new Pen(Color.Gray, 4f) )
{
    pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom;
    pen.DashPattern = new float[] { 0.25F, 0.25F };
    // now draw your stuff.. 
}

Note the doc on MSDN:

The elements in the dashArray array set the length of each dash 
and space in the dash pattern. The first element sets the length of a dash, 
the second element sets the length of a space, the third element sets 
the length of a dash, and so on. Consequently, each element should be a 
non-zero positive number.

The length of each dash and space in the dash pattern is the product 
of the element value in the array and the width of the Pen.

You can pick any pen width and any dash&gap lengths as long as you keep their relation in mind.. So if you want the finest dashes, make sure they multiply to 1.0 pixels!

Here is the resulting line:

fine dashed line

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
1

Some options:

  • You could use a PNG graphic that mimics that excel behaviour and then draw it on the control (you'll have to tile your image vertically).
  • Draw three lines with your code, with offset of y-axis & x-axis one pixel.
Yves Schelpe
  • 3,343
  • 4
  • 36
  • 69
0

That looks to me more like a rectangle filed with HatchBrush having HatchStyle.Percent50 and height of 3.

You could try

Rectangle rect = new Rectangle(0, 0, 500, 3) //you will use the values here from your cursor but height will be 3
HatchBrush brush = new HatchBrush(HatchStyle.Percent50, Color.Black);
g.FillRectangle(brush, rect);
Stelios Adamantidis
  • 1,866
  • 21
  • 36