0

I have the next piece of code:

    Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
        TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged,
        TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged, TextBox13.TextChanged, TextBox14.TextChanged,
        TextBox15.TextChanged, TextBox18.TextChanged, TextBox19.TextChanged, TextBox20.TextChanged

    Double.TryParse(TextBox1.Text, Component.Methane.Mole)
    Double.TryParse(TextBox2.Text, Component.Ethane.Mole)
    Double.TryParse(TextBox3.Text, Component.Propane.Mole)
    Double.TryParse(TextBox4.Text, Component.iButane.Mole)
    Double.TryParse(TextBox5.Text, Component.nButane.Mole)
    Double.TryParse(TextBox6.Text, Component.neoPentane.Mole)
    Double.TryParse(TextBox7.Text, Component.nHexane.Mole)
    Double.TryParse(TextBox8.Text, Component.nHeptane.Mole)
    Double.TryParse(TextBox9.Text, Component.N2.Mole)
    Double.TryParse(TextBox10.Text, Component.CO2.Mole)
    Double.TryParse(TextBox13.Text, Component.iPentane.Mole)
    Double.TryParse(TextBox14.Text, Component.nPentane.Mole)
    Double.TryParse(TextBox15.Text, Compress.Avol)
    Double.TryParse(TextBox18.Text, Compress.AF)
    Double.TryParse(TextBox19.Text, Compress.AP)
    Double.TryParse(TextBox20.Text, Compress.AT)

End Sub

Is there any possibility to make this procedure more compact, beatiful and smarter ? Use some kind of control, loop or something else? It would be greate to make it shorter and avoid typing. I am quite new in programming, any help is very appreciated!

Thanks in advance!

badscrool
  • 1
  • 4
  • You could do a `For Each` loop, looping through all of the TextBox controls in a particular container (such as the form), but this would only get you part of the way. You'd need a way to associate the TextBox with its Component value. Honestly, the way I'd do it would be to write a custom subclass of the TextBox control, implementing this logic in the custom control's `TextChanged` method and exposing a property to set its associated `Component`. That would be better object-oriented design, respecting separation of concerns. – Cody Gray - on strike May 28 '17 at 10:38

1 Answers1

0

If your range of textboxes should only contain numbers, create a new Control that inherits from Textbox and alter the Textbox to only accept valid numbers. See an example here: https://www.tigraine.at/2008/10/28/decimaltextbox-for-windows-forms/

The other thing I would point you is to look into Databinding. Databinding is something where a WinForm or WPF Control will "push" the value contained within the Control back to a property on an object. Windows has made a nice and quick write up on it here: https://msdn.microsoft.com/en-us/library/2b4be09b.aspx

Finally, for "beautiful code" give things meaningful names. Currently your textbox have the default name and this can result in some very confusing problems later on in life. A quick and easy naming convention I follow is , for example, instead of "TextBox15" you could have TextboxCompressAvol (Or, txbxCompressAvol).

Gorexxar
  • 1
  • 1