Results 1 to 10 of 10

Thread: drawborder and Style

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    21

    drawborder and Style

    Hey because i have some Border Issues (on my Richtextbox), when i change my Background Image (they are gone). I found a Code from this Forum to draw a Border. All works fine, I only like to ask, what i need to change, to become the fixed3d Borderstyle and not only a normal Line.

    https://www.vbforums.com/showthread....=1#post4434707


    Here is the Code.

    Code:
    Public Class Form1
    
    	Private showBorder As Boolean
    
    	Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    		If showBorder Then DrawBorder(e.Graphics, TextBox1, Color.Red, 2)
    	End Sub
    
    	Private Sub DrawBorder(parentGraphics As Graphics, ctrl As Control, color As Color, width As Integer)
    		Dim r As Rectangle = ctrl.Bounds
    		r.Inflate(1, 1)
    		Using pn As New Pen(color, width)
    			parentGraphics.DrawRectangle(pn, r)
    		End Using
    	End Sub
    
            'just to test the code above:
    	Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click	
    		showBorder = Not showBorder
    		Me.Invalidate()	'make sure the form repaints
    	End Sub
    
    End Class
    Thank you very much

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: drawborder and Style

    There's no magic to a 3D border. It's really just an optical illusion. You draw two or more lines and you make them different colours on different sides so it looks like the light is hitting them differently. Take a close look at a 3D border drawn by a control and you should be able to easily work out the logic. Assuming a width of two pixels, it will be eight calls to DrawLine, two for each side. You just need to do a little bit of calculation to determine the end points of the lines. With a bit of thought and some trial and error, you should be able to work it out easily enough. I would probably recommend putting the functionality into its own method to which you can pass a Rectangle that you want to draw the border around. You can then use that same code anywhere.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: drawborder and Style

    Instead of using the method jm suggested, which is the traditional method in vb, you could use the ControlPaint.DrawBorder method…

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: drawborder and Style

    Quote Originally Posted by .paul. View Post
    Instead of using the method jm suggested, which is the traditional method in vb, you could use the ControlPaint.DrawBorder method…
    Nice. I'd forgotten about that option. Never actually had cause to use it. That method would presumably do what I described internally.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: drawborder and Style

    https://docs.microsoft.com/en-us/dot...owsdesktop-6.0

    There's no option for custom colors though...

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: drawborder and Style

    I get the impression that Johanna isn't looking for coloured or otherwise fancy border effects, just the standard Fixed3D border. If that's the case, then you don't need the code quoted in post #1 above: just comment it out or delete it.

    There are only two effective choices for a Forms RichTextBox: Fixed3D (default) or None. Note: the FixedSingle option doesn't work for a RichTextBox -- see Remarks in the documentation.

    Normally you set the BorderStyle in the Designer (Properties window). But if you need to change the border while the program is running, you can do it in one line of code, for example:
    Code:
    RichTextBox1.BorderStyle = BorderStyle.Fixed3D
    .

    BB

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: drawborder and Style

    Quote Originally Posted by boops boops View Post
    I get the impression that Johanna isn't looking for coloured or otherwise fancy border effects, just the standard Fixed3D border. ...
    BB
    Perhaps you missed the current code he is using which is drawing a flat border in red, but he would want it to be 3d.
    Code:
    If showBorder Then DrawBorder(e.Graphics, TextBox1, Color.Red, 2)
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  8. #8
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: drawborder and Style

    No, I wrote the quoted code myself, 9 years ago. The red colour was just an example of a custom border appropriate to that particular thread. It seems to me that the OP is asking for a standard Fixed3D border style and was unaware of the property setting. Of course, I might be mistaken.... Let us know, Johanna!

    BB

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: drawborder and Style

    I'm a bit confused too. After .paul.'s posts, I was going to post that the desired border seems akin to that of a RichTextBox, but then I noticed that it was for a RichTextBox. From the information provided, it seems that the OP is trying to manually redraw the very border that the control should be drawing itself, which seems to suggest that there's another problem that could be solved to render this question moot. Maybe that has already been tried unsuccessfully, but maybe not.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2022
    Posts
    21

    Re: drawborder and Style

    Hey all, iam sorry i answer this late. Well yes i only like the normal 3dfixed Border. As i postet in first Post, bc iam using the GlasrichTextbox, i become the error, that the complete Border is lost when i change the Background Image of the Form in runtime. Refresh and redraw visible.false/true dont bring the Border back, so i found that old Code and get a Border. well but not 3dFixed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width