Results 1 to 6 of 6

Thread: Is it possible imply a command that affects all TextBoxes in the application?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    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...)
    Last edited by Hamsori; Aug 10th, 2011 at 05:58 AM.

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    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

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

    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:
    1. Array.ForEach(My.Application.OpenForms.Cast(Of Form).ToArray, Sub(frm) _
    2.     Array.ForEach(frm.Controls.OfType(Of TextBox).ToArray, Sub(tb) _
    3.             tb.Font = New Font(tb.Font, If(Val(tb.Text) > 0, FontStyle.Bold, FontStyle.Regular))))

  4. #4
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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!
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


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

    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:
    1. Public Class textboxEx
    2.     Inherits TextBox
    3.  
    4.     Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
    5.         MyBase.Font = New Font(MyBase.Font, If(Val(MyBase.Text) > 0, FontStyle.Bold, FontStyle.Regular))
    6.         MyBase.OnTextChanged(e)
    7.     End Sub
    8.  
    9. 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
    Last edited by .paul.; Aug 10th, 2011 at 06:48 AM.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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