Windows Develop Bookmark and Share   
 index > Windows Forms General > Why does setting textbox.borderstyle in events causes problems ?
 

Why does setting textbox.borderstyle in events causes problems ?

Can anyone offer an explanation or solution?

Im using VB.net Studio 2005 Pro on Windows XPP (3 similar workstations all do the same).

Simple to describe by way of example.

Add a new form.

Add TextBox1 and TextBox2

Add the following code to the forms class.

Code Block

Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter

TextBox1.BorderStyle = BorderStyle.Fixed3D

End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

TextBox1.BorderStyle = BorderStyle.None

End Sub

Run the form.

If you click between textboxes USING THE MOUSE it behaves as expected.

That is the border around TextBox1 disappears when you leave, and re-appears when you enter.

But, after you re-enter, you cant move out of TextBox1 using the Keyboard. Eg {TAB} - Why not ?

If you put the code into the corresponding GotFocus & LostFocus events,

Code Block

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus

TextBox1.BorderStyle = BorderStyle.Fixed3D

End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus

TextBox1.BorderStyle = BorderStyle.None

End Sub

you get an error on RE-ENTRY to TextBox1 in the gotfocus event.

System.ComponentModel.Win32Exception was unhandled
ErrorCode=-2147467259
Message="Error creating window handle."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.RecreateHandleCore()
at System.Windows.Forms.TextBoxBase.set_BorderStyle(BorderStyle value)......

Here's hoping someone has the answer (I want to hear)

Cheers

Geoff

GeoffInOzz  Tuesday, December 18, 2007 11:29 AM

I successfully reproduced your problem. I noticed that when you put the code on Leave event, and you change the BorderStyle to None, it will somehow Enter in the TextBox again. I think this is a bug, I don't know why this happens, I just know that When you leave, set the BorderStyle to None, the text will Re-Enter and set the BorderStyle to Fixed3D using that code, if you remove the line that sets the textbox's BorderStyle to Fixed3D you will notice that when you press tab, the border style will be set to none, but the focus will still be on this textbox, and when you press tab again, it will move to the next textbox.

As I said, I don't know why this happens, but I think it is a bug, and I managed to creat an workaround. Basically you will inherit the TextBox to create your own:

Code Block

ClassMyTextBox
InheritsTextBox

PublicShadowsPropertyBorderStyleAsBorderStyle
Get
ReturnMyBase
.BorderStyle
EndGet
Set
If
(value=BorderStyle.None)Then
MyBase
.Enabled=false
MyBase
.BorderStyle=value
MyBase.Enabled=true
Return
EndIf
MyBase
.BorderStyle=value
EndSet
EndProperty
EndClass

Build the application. The New TextBox class (MyTextBox)should be available from the ToolBox.

Add the textboxes to the form and subscribe the Enter and Leave events to these handlers (Basically the same as you wrote, but you could add these to every textboxes without having to create a new event handler for each textbox):

Code Block

PrivateSubtextBox_Enter(ByValsenderAsObject,ByValeAsEventArgs)
DimtxtboxAsMyTextBox=CType(sender,MyTextBox)
txtbox.BorderStyle
=BorderStyle.Fixed3D
EndSub

PrivateSub
textBox_Leave(ByValsenderAsObject,ByValeAsEventArgs)
DimtxtboxAsMyTextBox=CType(sender,MyTextBox)
txtbox.BorderStyle
=BorderStyle.None
EndSub

I hope this helps, any doubts, just ask

Regards,

Fábio

Fábio Franco  Tuesday, December 18, 2007 1:21 PM

Good thinking, you're a champ Fábio

I'm already using an extended textbox class, and adding your suggested property shadow did the trick.

I was beginning to think I would have to resort to painting my own frame

Your suggestion addresses my problem elegantly.

Cheers and thanks again.

Geoff

GeoffInOzz  Tuesday, December 18, 2007 1:55 PM

I think what happens is that the TextBox BorderStyle property calls the RecreateHandle() method to update its style, and while it is still Leaving, it still has the focus, and on RecreateHandle() breakes the code used by Leave and thus, entering the textbox again when the handle finishes recreating

Fábio Franco  Tuesday, December 18, 2007 2:01 PM

Thanks, and I'm glad to be of help.

Regards,

Fábio

Fábio Franco  Tuesday, December 18, 2007 2:04 PM

It seems what I thought was a solution is onlya partsolution.

Following on from the example I gave earlier.

Fábio's solution seemed to fix the problem.

But I must have been very tired (thats my excuse) when I tested thesolution.

It certainly allows you to{TAB} from control to control.

But

Add another text box (TextBox3 inherited from MyTextBox).

Run the form.

{TAB} from TextBox1 to TextBox2.

So farso good.

Now press{SHIFT}{TAB}to go back to TextBox1.

I land in TextBox3

Here's theoutput of DEBUG.Prints I placed in GotFocus,LostFocus, Enter & Leave Events when I pressed {SHIFT}{TAB}

GotFocus DgTextBox2

Leave DgTextBox2

Leave DgTextBox2

Enter DgTextBox3

LostFocus DgTextBox2

GotFocus DgTextBox3

Enter DgTextBox3

Im agreeing with Fábio, this surely must be a bug.

Anyone using .NETFramework 3 ?

Issetting TextBox.BorderStyle still a problem in FW 3 ?

Geoff

GeoffInOzz  Wednesday, December 19, 2007 12:21 AM

You can use google to search for other answers

Custom Search

More Threads

• Application causing win2000 server to crash?
• Passing data inbetween threads
• Where can I get MSPrpDE.DLL(Property Page Resource DLL)
• Can WinForms / Smart Client Applications interact with an sql server database on separate server?
• how can i get name of the parent class
• Check if a class is instanciated by Windows form or Web Form
• Reorder tabpages
• WebService Security
• Visual C++ 2005
• Just another printing bug