Results 1 to 3 of 3

Thread: [RESOLVED] conditional formatting of textbox font on a form

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2020
    Location
    UNITED KINGDOM
    Posts
    59

    Resolved [RESOLVED] conditional formatting of textbox font on a form

    Hi,

    Hope you are safe and well

    I have a form which contains number of textboxes, among which 32 are populated by data grid values, which is turn is populated by a sql query. All this works fine, however what I would like is some kind of loop that goes through the text boxes and if the value is <0 then make that textbox font colour red and green if >= 0.

    There are other textboxes on the form for example the title which I believe you wont be able to convert to integer if that's what is needed.

    This will save me doing an if statement for every text box I would like to check.

    Thank you in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: conditional formatting of textbox font on a form

    Firstly, if you're not already, you should be binding your data to both the grid and the TextBoxes, e.g.
    vb.net Code:
    1. myDataAdapter.Fill(myDataTable)
    2.  
    3. BindingSource1.DataSource = myDataTable
    4. DataGridView1.DataSource = BindingSource1
    5.  
    6. TextBox1.DataBindings.Add("Text", BindingSource1, "SomeColumn")
    The TextBoxes will then update automatically when you select a row in the grid and any changes you make in the TextBoxes will be automatically pushed to the data source and, therefore, reflected in the grid.

    As for the question, forget about using a loop. Select all the desired TextBoxes in the designer, open the Properties window, click the Events button and then double-click the TextChanged event. That will create a single method to handle that event for all those controls. You can then add code that will be executed for that TextBox each time the Text in one changes, e.g.
    vb.net Code:
    1. Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged,
    2.                                                                             TextBox2.TextChanged,
    3.                                                                             TextBox1.TextChanged
    4.     Dim tb = DirectCast(sender, TextBox)
    5.     Dim number As Integer
    6.  
    7.     If Integer.TryParse(tb.Text, number) Then
    8.         If number < 0 Then
    9.             tb.ForeColor = Color.Red
    10.         Else
    11.             tb.ForeColor = Color.Green
    12.         End If
    13.     Else
    14.         tb.ForeColor = SystemColors.WindowText
    15.     End If
    16. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2020
    Location
    UNITED KINGDOM
    Posts
    59

    Re: conditional formatting of textbox font on a form

    Exactly what I wanted mate - your a star. Thank you ever so much, that was excellent

    I am posting another similar question with regards to buttons - take a look if you have the time (grit teeth!!)

    Thanks once again

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