locked
How to fill textboxes with "0.0" s RRS feed

  • Question

  • There are 45 textboxes to fill.

     

    Code Snippet

    Private Sub HECalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    TextBox1.Text = "0.0"

    TextBox2.Text = "0.0"

    TextBox3.Text = "0.0"

    TextBox4.Text = "0.0"

    TextBox5.Text = "0.0"

    TextBox6.Text = "0.0"

    TextBox7.Text = "0.0"

    TextBox8.Text = "0.0"

    TextBox9.Text = "0.0"

    .

    ...

    ....

    .....

    .......

    End Sub

     

     

    Tuesday, August 14, 2007 3:52 PM

Answers

  •  FalconDW wrote:

    I think what you're looking for is to write a simple loop that goes through all the textboxes on your form and fills them for you.  I've posted an example below that just looks for all textboxes on the form, but you could be more specific inside the loop to say something like, 'if the textbox has this property, then...'

     

    Code Snippet

    For Each t As TextBox In Me.Controls

    t.Text = "0.0"

    Next

     

     

    Hope that helps...

     

     

     

    Hi Cesar & FalconDW,

     

    You are close but you need to allow for other controls, so use TypeOf. >>>>

     

     

    Code Snippet

     

    For Each Ctrl As Control In Me.Controls

     

    If TypeOf(Ctrl) Is TextBox Then Ctrl.Text = "0.0"

     

    Next

     

     

     

     

     

    This should work.

     

     

     

    Regards,

     

    S_DS

     

    Wednesday, August 15, 2007 1:51 AM

All replies

  • I think what you're looking for is to write a simple loop that goes through all the textboxes on your form and fills them for you.  I've posted an example below that just looks for all textboxes on the form, but you could be more specific inside the loop to say something like, 'if the textbox has this property, then...'

     

    Code Snippet

    For Each t As TextBox In Me.Controls

    t.Text = "0.0"

    Next

     

     

    Hope that helps...

     

     

    Tuesday, August 14, 2007 4:17 PM
  • It looks brilliant.

    I got this error (?)

     

    Unable to cast object of type 'System.Windows.Forms.PictureBox' to type 'System.Windows.Forms.TextBox'.

    Wednesday, August 15, 2007 1:29 AM
  • This error means that you are trying to turn a picture box into a textbox.  Can you post a code sample of what caused the error?

    Wednesday, August 15, 2007 1:36 AM
  • Code Snippet

    Private Sub HECalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For Each t As TextBox In Me.Controls

    t.Text = "0.0"

    Next

    TextBox1.Text = "0.0"

    End Sub

     

     

    Yes. There are to PictureBoxes on the Form. Taking them out, the error message turns on the labels. It seems to me, that anything else oder than TextBoxes will cause an exeption. 

    Wednesday, August 15, 2007 1:42 AM
  •  FalconDW wrote:

    I think what you're looking for is to write a simple loop that goes through all the textboxes on your form and fills them for you.  I've posted an example below that just looks for all textboxes on the form, but you could be more specific inside the loop to say something like, 'if the textbox has this property, then...'

     

    Code Snippet

    For Each t As TextBox In Me.Controls

    t.Text = "0.0"

    Next

     

     

    Hope that helps...

     

     

     

    Hi Cesar & FalconDW,

     

    You are close but you need to allow for other controls, so use TypeOf. >>>>

     

     

    Code Snippet

     

    For Each Ctrl As Control In Me.Controls

     

    If TypeOf(Ctrl) Is TextBox Then Ctrl.Text = "0.0"

     

    Next

     

     

     

     

     

    This should work.

     

     

     

    Regards,

     

    S_DS

     

    Wednesday, August 15, 2007 1:51 AM
  • Well done !  Mesmerized.

    Wednesday, August 15, 2007 2:04 AM
  •  Cesar Francisco wrote:

    Well done !  Mesmerized.

     

    Hi Cesar,

     

    Please remember to mark my post

    as answer

     

    if it has helped you and fully answered your question.

     

     

     

    Regards,

     

    S_DS

     

    Wednesday, August 15, 2007 2:27 AM