Can I draw a straight line on a UserForm?

MTBer

Board Regular
Joined
Apr 29, 2004
Messages
207
As the the title really, I need to draw a straight line on a userform can it be done?

cheers,
Paul
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

I've seen this asked a few times. As far as I know, you can't do that. One workaround might be to insert a picture box, setting the background/border colors to be transparent on your userform background. You could draw a pictrue in Paint or something of a line and insert that into your picture box. It's not the best solution, but it works.
 
Upvote 0
Hi, you could create a label and resize the width to the length you need then insert consecutive underscore characters as the caption ... _________________________

You can overlay other controls over the top half portion of the label if the label itself is too high, as it needs to be a minimum height to reveal its contents.

hth
 
Upvote 0
The drawing of lines requires API's

Code:
Option Explicit

Private Declare Function LineTo _
    Lib "gdi32" ( _
        ByVal hdc As Long, _
        ByVal x As Long, _
        ByVal y As Long) _
As Long

Private Declare Function MoveToEx _
    Lib "gdi32" ( _
        ByVal hdc As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        lpPoint As POINTAPI) _
As Long

Private Declare Function AngleArc _
    Lib "gdi32" ( _
        ByVal hdc As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        ByVal dwRadius As Long, _
        ByVal eStartAngle As Single, _
        ByVal eSweepAngle As Single) _
As Long

Private Declare Function GetDC _
    Lib "user32" ( _
        ByVal hWnd As Long) _
As Long

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Type POINTAPI
  x As Long
  y As Long
End Type

Private hdc As Long
Private m_X1 As Single, m_Y1 As Single
Private m_X2 As Single, m_Y2 As Single
Dim pt As POINTAPI

Private Sub DrawLine(ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long)
  
  MoveToEx hdc, X1, Y1, pt
  LineTo hdc, X2, Y2
  
End Sub

Private Sub DrawHorLine(ByVal sX1 As Long, ByVal sY1 As Long, ByVal lng As Long) ', ByVal Y2 As Long)
  
  MoveToEx hdc, sX1, sY1, pt
  LineTo hdc, lng, sY1
  
End Sub
Private Sub DrawVerLine(ByVal sX1 As Long, ByVal sY1 As Long, ByVal lng As Long) ', ByVal Y2 As Long)
  
  MoveToEx hdc, sX1, sY1, pt
  LineTo hdc, sX1, lng
  
End Sub

and to use..........just plug in the values like

Code:
Private Sub CommandButton2_Click()
    DrawLine TextBox1, TextBox2, TextBox3, TextBox4
End Sub

where the Textboxs are just values ... this is just a snipet from a bigger project.....
 
Upvote 0
Thanks Zack. :biggrin:

Thats cool Ivan. Seems a lot of work just to draw a simple line though. What would you need to do to make this a control - create a DLL? Would you have to create a DLL? ie so you could select Tools|References and pick the control.
 
Upvote 0
Hi Parry, you know, I used to think that some codes used ie APIs were a lot to use.... but once you have these defined it isn't really that much work (certainly not by the system) and depending on what you want to do it can really offer you control that you can't get from any other method. eg Whilst the other methods proposed do the trick (and I have used the methods you have proposed too) you may need to draw a diagonal line or perhaps a simple graph. Thats when this maybe useful. As far as making a Dll, or ocx, I'd just leave as is or make a class module. making a control means that other users will need the control. Using a class module dosen't require that over head in terms of supplying the control.
 
Upvote 0

Forum statistics

Threads
1,215,836
Messages
6,127,173
Members
449,368
Latest member
JayHo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top