Reafidy

Hi Everyone,

I am using visual basic 2005 express in visual studio express.

When I add a rich textbox to a form it has a "sunken" look to it. But the standard textbox's and comboboxss dont.

How do I get the sunken look for the combobox's and textbox's

Thanks In Advance.



Re: Visual Basic Express Edition Sunken Textbox, Combobox

js06

set the borderstyle to fixed3d






Re: Visual Basic Express Edition Sunken Textbox, Combobox

Reafidy

Hi Js06,

Thanks for the reply.

I had tried that but it doesnt have the same effect as the rich textbox. It looks quite different.

It isnt "sunken".





Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

You might place the textbox inside a panel set to Fixed3D





Re: Visual Basic Express Edition Sunken Textbox, Combobox

js06

I must admit i am confused on this one. I tried it on a form here and they both look the same to me.

(textbox and richtextbox)

I know that combobox does not have the same property.

Anyway, i am curious to see how this works out.






Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

Hi js06,

I am not sure why, but I see a clear difference between how a default multiline textbox and a richtextbox render their borders. On my test system (vista VS 2008b2) a rtb has a heavy top and left border giving a clear sunken impression (as does a panel). The plain old multiline textbox has a light top border that starts to suggest a sunken effect though much more subtle than the RTB. The remaining 3 textbox borders render like the bottom border of the default RTB.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

Reafidy

Hi Js06,

Well that is confusing, are you using VS express perhaps I need the standard version

Is there any way I can upload an image, I dont see how on this forum





Re: Visual Basic Express Edition Sunken Textbox, Combobox

Reafidy

Hi JGalley,

Sorry I missed your first post.

Um setting the textbox into a panel doesnt seem to do anything either unfortunately. Thanks for your reply.

But I see what you mean about the panel it has the same sunken effect as the rich textbox.

Im also using windows vista.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

Sorry, I should have been more clear.

Since the panel has the UI look we want, I suggest acaling the panel and textbox to the same size and place and anchor the textbox within the panel. then turn off the textbox borders and then the panel borders will give you the end effect you seek.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

Reafidy

No I should have realised what you meant. Yes that works and is a solution.

It kind of seems a bit mickey mouse though dont you think Please no offence intended! Smile Its just I have a lot of textboxes on quite a few userforms which I want all of them to have the sunken effect.

The textboxs in VB for Application (excel access etc) they all have the sunken effect as an option.

Can anyone tell me if it is available in VS Standard or Pro





Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

An alternative might be to derive a class from TextBox and override OnPaint and draw the border yourself. Sounds like an interesting little task. I'm going to try it as well.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

-------------------------

Ugg.

Well, this turned out to be MUCH harder than I thought. If you know how to do this "properly" please feel free to step in an correct things.

-------------------------

I thought, "I'll just interecept OnPaint and draw my own border". That sounded not too bad...

Code Block

Partial Class TextBox_WithBorder
Inherits System.Windows.Forms.TextBox

Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
ControlPaint.DrawBorder( _

e.Graphics, e.ClipRectangle, _

Color.DarkSlateGray, ButtonBorderStyle.Inset)

End Sub

End Class

This (of course) did not work at all.

Firstly, the border draws inside the textbox rather than well, in the border region. Secondly, the control really more or less becomes totally broken with perma-bold fonts, scrolling problems and phantom text.

Clearly this was going to be harder than I expected.

I was going to just give up, but I thought "What if I blended my fist panel solution with this one" and that got me to this:

If the textbox border appears to draw on the parent rather than on itself, why not do the same thing. That would appear to get me past the clipping region problem so I tried:

Code Block

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)

Dim myPen As Pen
Dim PenWidth As Single = 2.0
Dim HalfPenWidth As Single = PenWidth / 2.0

Using myGraphics As Graphics = Graphics.FromHwnd(Me.Parent.Handle)

' Draw Top and Left Borders

myPen = New Pen(Color.FromArgb(255, 105, 105, 105), PenWidth)
myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top - HalfPenWidth)

myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left - HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myPen.Dispose()

' Draw Bottom and Right Borders

myPen = New Pen(Color.FromArgb(255, 227, 227, 227), PenWidth)
myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myGraphics.DrawLine(myPen, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myPen.Dispose()

End Using

End Sub

This seems to look ok, but the functionality of the textbox was still all borked. How could I allow the form to draw the control, yet also manually draw the border when there was a request to paint Thinking about how this might be done took me back to the days of yor... I could intercept the message pump and listen for a paint request, then pass it on to the control for it to do it's magic. I figured that this would also fail, but it turned out to mostly work. I also discovered that in addition to the paint message there is a paint border message so I chose to listen in on it.

This time in our New() I am not going to tell the control that we will draw it ourselves so, let's take the SetStyle out. I did ultimately put it back with some other sytles to support double buffering and you can if you want as well.

That left me with

Code Block

Public Class TextBox_WithBorder
Inherits System.Windows.Forms.TextBox

Private Const WM_PAINT As Integer = 15
Private Const WM_NCPAINT As Integer = 133 'redraw the border

Public Sub New()

Me.SetStyle( _

ControlStyles.DoubleBuffer Or _

ControlStyles.AllPaintingInWmPaint, True)

End Sub

Protected Overloads Overrides Sub WndProc(ByRef myMessage As Message)
' Call the base Window Message Proc:
MyBase.WndProc(myMessage )

If myMessage.Msg.Equals(WM_NCPAINT) Then

Dim myPen As Pen
Dim PenWidth As Single = 2.0
Dim HalfPenWidth As Single = PenWidth / 2.0

Using myGraphics As Graphics = Graphics.FromHwnd(Me.Parent.Handle)

' Draw Top and Left Borders

myPen = New Pen(Color.FromArgb(255, 105, 105, 105), PenWidth)
myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top - HalfPenWidth)

myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left - HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myPen.Dispose()

' Draw Bottom and Right Borders

myPen = New Pen(Color.FromArgb(255, 227, 227, 227), PenWidth)
myGraphics.DrawLine(myPen, _

Me.Left - HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myGraphics.DrawLine(myPen, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top - HalfPenWidth, _

Me.Left + Me.Width + HalfPenWidth, _

Me.Top + Me.Height + HalfPenWidth)

myPen.Dispose()

End Using

End If

End Sub

End Class

I am certain that people will laugh at this as I imagine there is a one line solution. There are still some problems with getting the border to redraw correctly. I tried invalidating the appropriate region on the parent but that worked as well as my first attempt at this problem (ie not at all). I would go back and properly implement the choices for border properties. I think I would also use a gradient brush to draw each of the borders rather than a solid pen but this works for now.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

Reafidy

Hi JGalley,

Wow thats some code youve got there.

Thanks you for your effort in getting me a solution.

I think I may have found an alternative. I went to add items on the toolbox then selected com component - Microsoft Forms 2.0 Textbox. and Comobox.

These both have the sunken effect.

Can any body tell me if this is a bad/good idea. Im not to sure what the consequences will be of using these controls.

THanks Everyone.





Re: Visual Basic Express Edition Sunken Textbox, Combobox

Martin Xie - MSFT

jgalley wrote:

Hi js06,

I am not sure why, but I see a clear difference between how a default multiline textbox and a richtextbox render their borders. On my test system (vista VS 2008b2) a rtb has a heavy top and left border giving a clear sunken impression (as does a panel). The plain old multiline textbox has a light top border that starts to suggest a sunken effect though much more subtle than the RTB. The remaining 3 textbox borders render like the bottom border of the default RTB.

On Windows 2003 and Xp, they both look the same. (textbox and richtextbox)

I just try it on windows Vista. I find It behaves like what jgalley said.

You can post a suggestion or an issue to our Connect feedback portal. http://connect.microsoft.com/VisualStudio/

Reafidy wrote:

Is there any way I can upload an image, I dont see how on this forum

At present, we cannot upload an image on MSDN forums. But we can place an image on a post using a pair of [img] markers.

Reafidy wrote:

I think I may have found an alternative. I went to add items on the toolbox then selected com component - Microsoft Forms 2.0 Textbox. and Comobox.

These both have the sunken effect.

Can any body tell me if this is a bad/good idea. Im not to sure what the consequences will be of using these controls.

Take TextBox control for example:

.NET control is named TextBox, while the COM control (Microsoft Forms 2.0 Textbox) is named AxTextBox. They both are wrappers of the same underlying component. The COM controls are more appropriate for COM developing environment, the .NET controls are more appropriate for .NET developing environment. Here the .NET control TextBox is preferred.

Thanks,

Martin






Re: Visual Basic Express Edition Sunken Textbox, Combobox

jgalley

Hi Martin,

Did you see the manual draw border code I posted I was just wondering why the panel solution was marked the answer and not the custom border draw