Is it possible imply a command that affects all TextBoxes in the application?
Hi!
I'm wondering if there is a way to write a code that affects every single TextBox in my application... I have about 12 Forms that are all inherited from my BaseForm. I want to affect all of the TextBoxes in those 12 forms
The idea is, that every single TextBox would change the text in it to Bold, if the text is not 0 or 0,0 or 0,00.
Let's say there is a Form that has about 100 textboxes to indicate different values. Every TextBox that shows the value 0 should be in normal font, but every TextBox that shows any other value should be bolded...
Or should I just do a public function that is called every time a textbox changes value? (Not quite sure how to do this either...)
Re: Is it possible imply a command that affects all TextBoxes in the application?
One method would be to use IExtenderProvider where you create a class that handles you validation logic. Take a look at the three links (which have good examples) where the first one is closest to what you are looking for but needs to be converted to VB.NET which should be easy to do with the many converters on the web.
In short you can simply drop the control onto your form and use it if all logic is contained within the control's underlying class or you might want to add a property to not work on specific TextBox. Best to study working examples prior to writing your own. BTW the ToolTip control is a good example of IExtenderProvider in use.
http://msdn.microsoft.com/en-us/magazine/cc164063.aspx
http://www.vb-helper.com/howto_net_i..._provider.html
http://www.vb-helper.com/howto_net_r..._provider.html
Re: Is it possible imply a command that affects all TextBoxes in the application?
assuming you're using vb2010, you could change fontstyles for all textboxes in all of your open forms like this:
vb Code:
Array.ForEach(My.Application.OpenForms.Cast(Of Form).ToArray, Sub(frm) _
Array.ForEach(frm.Controls.OfType(Of TextBox).ToArray, Sub(tb) _
tb.Font = New Font(tb.Font, If(Val(tb.Text) > 0, FontStyle.Bold, FontStyle.Regular))))
Re: Is it possible imply a command that affects all TextBoxes in the application?
Hamsori, i hav some knwledge in vb6, new to .net, may be this code helps u,
Code:
Dim myform As Form
Dim ctrl As Control
For Each myform In Application.OpenForms
For Each ctrl In myform.Controls
If TypeOf ctrl Is TextBox Then
If Val(ctrl.Text) > 0 And ctrl.Text <> "" Then
ctrl.Font = New Font(ctrl.Font, FontStyle.Bold)
Else
ctrl.Font = New Font(ctrl.Font, FontStyle.Regular)
End If
End If
Next
Next
edited: sorry! Paul already posted a simplified code!
Re: Is it possible imply a command that affects all TextBoxes in the application?
this is what kevin meant:
edit: ok it's not what kevin meant, but this is what i'd use. it's simple enough to open each form's designer.vb file + change your standard textboxes to textboxEx
vb Code:
Public Class textboxEx
Inherits TextBox
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.Font = New Font(MyBase.Font, If(Val(MyBase.Text) > 0, FontStyle.Bold, FontStyle.Regular))
MyBase.OnTextChanged(e)
End Sub
End Class
it's an extended textbox. add the class to your project + rebuild, then you'll find the control at the top of your toolbox
Re: Is it possible imply a command that affects all TextBoxes in the application?
and then I'd add the extended textbox to the base form... and fugeddaboutit.
-tg