Home » VB.Net

Textbox readonly but want it to have focus

VS 2008 SP1

Hi, I've got two textboxes 1 and 2. Textbox2 is readonly. When I click Textbox2 I want Textbox1 to execute it's validating event, so I can trap for incorrect user input. It seems that the readonly status of Textbox2 doesn't allow Textbox2 to receive focus and trigger the validation event of Textbox1.

To me this doesn't seem logical, because I'm selecting Textbox2 even though I'm not able to enter text into it. It's also detrimental to the user experience because they only get alerted to input errors in Textbox1 when another control gets focus, and yet to the user it 'seems' like Textbox2 has got focus.

Any thoughts/ suggestions?  Thanks   


6 Answers Found


Answer 1

You could try using UpdateSourceTrigger=PropertyChanged in your TextBox binding, that'll cause it to validate on each keystroke instead of LostFocus.


Warm regards,





Answer 2

Thanks for the suggestion Matt,

It's not really what I want to do though. The validation of the entry in Textbox1 needs to happen when the full text is entered, not per keystroke. For example I'm validating against a number of custom data type formats, like for example 'Hex' (example "#4A2F"), or 'IP Address' (example ""), so validating on a partial entry wouldn't make sense.

Any more suggestions welcome !



Answer 3

Hi Zippy,

I cannot reproduce your problem here. The validaton works fine in the scenario you describe. I double checked that the ReadOnly textbox  did get focus.

Have you explicitly made your ReadOnly textbox unfocusable?

By the way, the validation won't happen if the text didnt change at all when the textbox lost focus. This is because its a binding validation so it is the binding who triggers the validation. As a result, the default value of the textbox will never trigger the validation. If this is your case, you can call BindingExpression.UpdateSource() to explicitly force a validation to happen in the LostFocus event handler of the textbox.

If neither of the above assumptions meet your situation, pleave kindly elaborate your problem so we can help you. A snippet of code that can reproduce the problem will be very helpful.

If you still have any doubts or concerns about this issue, please feel free to let me know.

Best regards,



Answer 4

Hi, thanks for taking the time to look at this.

I did make a mistake because I had set CausesValidation to false on my Textbox2.  I hadn't understood this properly.

But I've still got an issue, because my readonly textboxes are firing validation events.  What do I need to do to stop this? 

My specific situation is this: Textboxes are generated dynamically. They represent values that can either hold user-editable values or calculated values. The textboxes are colour-coded and have their readonly  property set appropriately (user can click on calculated textbox  but can't edit the value - the code generates the value). A handler is also generated to handle the validation events. At the moment my code generates a handler regardless of whether the textbox is readonly or not - on my assumption that the validation won't fire if the textbox is readonly.

What I'm witnessing is that the validation event on the readonly Textboxes IS firing when I move away from the cell.

Some code ...

For RowIndex = 0 To _ValueDefinitionData.Rows.Count - 1

    Dim ValueBox AsNew TextBox

    With ValueBox
    	.Name = ValueDefinitionRow.NAME
 		If ValueDefinitionRow.CALCULATED = TrueThen
			.BackColor = Color.PowderBlue
			.ReadOnly = TrueElse
			.BackColor = Color.LightGray
			.ReadOnly = FalseEndIfEndWithAddHandler ValueBox.Validating, AddressOf ValueBox_TextValidating


Answer 5

Hi Zippy,

You can check the readonly  status in the Validation event handler and ignore the validation if the textbox  is readonly.

This is WPF forum. It seems that you are not using WPF. You'll need to post your questions at VB Forums or Windows Forms Forums for quicker and better responses.

Thank you and have a nice day!

Best regards,



Answer 6

Thanks for that Min, especially as I've posted to the wrong place !! (sorry).  Checking Readonly in the validation is the obvious thing to do but when I was looking at it again at 2am I didn't think of it :)





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure