If Auwahl mit Combobox

  • VB.NET

Es gibt 35 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Ich bastele das gerade neu ,jedoch schritt für schritt und schaue wann das Programm aussteigt .Hoffe den fehler dann zu finden.
    Suche gerade einen Befehl ,und zwar beim Anklicken der COmbobox will ich alle Textboxen und Label leeren.
    Währe ja unsinnig die alle aufzuzählen zum löschen oder?
    Hallo,
    Also derzeit klappt es wunderbar wenn ich in der Combobox eine auswahl treffe !
    WIe gesagt suche ich gerade eine lösung zum löschen aller Textboxen und Label.
    Dan sehe ich es noch besser.
    Haltepunkt muß ich noc mehr zeit verbringen .
    Habe ihn an verschieden stellen gesetzt ,medlung Haltepunkt erreicht:Combobox1

    Derzeit habe ich das so gemacht .

    Visual Basic-Quellcode

    1. Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    2. Dim Auswahl As String = ComboBox1.Text
    3. If Auswahl = "111" Then : TextBox1.Text = "37" : TextBox2.Text = "9"
    4. TextBox3.Text = "28" : TextBox4.Text = "8"
    5. TextBox5.Text = "34" : TextBox6.Text = "18"
    6. TextBox7.Text = "33" : TextBox8.Text = "25"
    7. TextBox9.Text = "30" : TextBox10.Text = "29"
    8. TextBox11.Text = "29" : TextBox12.Text = "36"
    9. TextBox13.Text = "26" : TextBox14.Text = "38"
    10. TextBox15.Text = "38" : TextBox16.Text = "11"
    11. End If
    12. If Auswahl = "222" Then : TextBox1.Text = "6" : TextBox2.Text = "7"
    13. TextBox3.Text = "28" : TextBox4.Text = "8"
    14. TextBox5.Text = "34" : TextBox6.Text = "18"
    15. TextBox7.Text = "33" : TextBox8.Text = "25"
    16. TextBox9.Text = "30" : TextBox10.Text = "29"
    17. TextBox11.Text = "29" : TextBox12.Text = "36"
    18. TextBox13.Text = "26" : TextBox14.Text = "38"
    19. TextBox15.Text = "38" : TextBox16.Text = "11"
    20. End If
    21. Dim a = Double.Parse(TextBox1.Text)
    22. Dim b = Double.Parse(TextBox2.Text)
    23. Dim c = Double.Parse(TextBox3.Text)
    24. Dim d = Double.Parse(TextBox4.Text)
    25. Dim h = Double.Parse(TextBox5.Text)
    26. Dim f = Double.Parse(TextBox6.Text)
    27. Dim g = Double.Parse(TextBox7.Text)
    28. Dim i = Double.Parse(TextBox8.Text)
    29. Dim j = Double.Parse(TextBox9.Text)
    30. Dim k = Double.Parse(TextBox10.Text)
    31. Dim l = Double.Parse(TextBox11.Text)
    32. Dim m = Double.Parse(TextBox12.Text)
    33. Dim n = Double.Parse(TextBox13.Text)
    34. Dim o = Double.Parse(TextBox14.Text)
    35. Dim p = Double.Parse(TextBox15.Text)
    36. Dim q = Double.Parse(TextBox16.Text)
    37. Dim res1 = a / b
    38. Dim res2 = c / d
    39. Dim res3 = h / f
    40. Dim res4 = g / i
    41. Dim res5 = j / k
    42. Dim res6 = l / m
    43. Dim res7 = n / o
    44. Dim res8 = p / q
    45. Dim res10 = res1 * res2
    46. Dim res11 = res1 * res3
    47. Label9.Text = res1.ToString("0.###")
    48. Label10.Text = res10.ToString("0.###")
    49. Label11.Text = res11.ToString("0.###")
    50. Label12.Text = CStr(CDbl(res1.ToString) * CDbl(res4.ToString("0.#")))
    51. Label13.Text = CStr(CDbl(res1.ToString) * CDbl(res5.ToString("0.###")))
    52. Label14.Text = CStr(CDbl(res1.ToString) * CDbl(res6.ToString("0.###")))
    53. Label15.Text = CStr(CDbl(res1.ToString) * CDbl(res7.ToString("0.###")))
    54. Label16.Text = CStr(CDbl(res1.ToString) * CDbl(res8.ToString("0.###")))
    55. End Sub
    56. End Class

    brembo00 schrieb:

    VB.NET-Quellcode

    1. Label11.Text = res11.ToString("0.###")
    2. Label12.Text = CStr(CDbl(res1.ToString) * CDbl(res4.ToString("0.#")))
    3. Label13.Text = CStr(CDbl(res1.ToString) * CDbl(res5.ToString("0.###")))
    4. Label14.Text = CStr(CDbl(res1.ToString) * CDbl(res6.ToString("0.###")))
    5. Label15.Text = CStr(CDbl(res1.ToString) * CDbl(res7.ToString("0.###")))
    6. Label16.Text = CStr(CDbl(res1.ToString) * CDbl(res8.ToString("0.###")))
    Das ist ja grauenhaft. :cursing:
    Die Einzel-Resultate liegen Dir doch als Double-Werte vor.
    Rechne gefälligst mit ihnen :!:

    VB.NET-Quellcode

    1. Label12.Text = (res1 * res4).ToString
    2. Label13.Text = (res1 * res5).ToString
    3. Label14.Text = (res1 * res6).ToString
    4. Label15.Text = (res1 * res7).ToString
    5. Label16.Text = (res1 * res8).ToString

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Wird nur noch übertroffen von der Grauenhaftigkeit seiner Kommunikation hier im Forum:

    brembo00 schrieb:

    Habe ihn an verschieden stellen gesetzt ,medlung Haltepunkt erreicht:Combobox1
    Die Rechtschreibung ist mir ja schnuppe, aber was bedeutet es inhaltlich: "Meldung Haltepunkt erreicht: Combobox1" ? ?(

    (Was immer es bedeutet - Sieht mir zumindest nicht so aus, als hätte er inzwischen gelernt, einen Haltepunkt zu setzen, und seinen Code durchzusteppen.)
    Das ganze klappt jetzt.
    Nun habe ich noch ein Problem !

    Ich will wenn in Textbox2 ein wert der höher als 0 ist das eine andere Formel vewerwendet wird !
    geht um res5 /res6 / res7
    Was mache ich da falsch ?

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    3. Dim Auswahl As String = ComboBox1.Text
    4. If Auswahl = "VW Golf VII 1.2TSI 86PS/ Schalt" Then : TextBox1.Text = "4,056" : TextBox2.Text = "4"
    5. TextBox3.Text = "3,769" 'Gang1
    6. TextBox4.Text = "1,955" 'Gang2
    7. TextBox5.Text = "1,281" 'Gang3
    8. TextBox6.Text = "0,881" 'Gang4
    9. TextBox7.Text = "0,673" 'Gang5
    10. TextBox8.Text = "0" 'Gang6
    11. TextBox9.Text = "0" 'Gang7
    12. End If
    13. If Auswahl = "VW Golf VII 1.2TSI 105PS/Schalt" Then : TextBox1.Text = "4,056" : TextBox2.Text = "0"
    14. TextBox3.Text = "3,615" 'Gang1
    15. TextBox4.Text = "1,947" 'Gang2
    16. TextBox5.Text = "1,281" 'Gang3
    17. TextBox6.Text = "0,973" 'Gang4
    18. TextBox7.Text = "0,778" 'Gang5
    19. TextBox8.Text = "0,646" 'Gang6
    20. TextBox9.Text = "0" 'Gang7
    21. End If
    22. If Auswahl = "VW Golf VII 1.2TSI 110PS/Schalt" Then : TextBox1.Text = "4,056" : TextBox2.Text = "0"
    23. TextBox3.Text = "3,615" 'Gang1
    24. TextBox4.Text = "1,947" 'Gang2
    25. TextBox5.Text = "1,281" 'Gang3
    26. TextBox6.Text = "0,973" 'Gang4
    27. TextBox7.Text = "0,778" 'Gang5
    28. TextBox8.Text = "0,646" 'Gang6
    29. TextBox9.Text = "0" 'Gang7
    30. End If
    31. If Auswahl = "VW Golf VII 1.4TSI 122PS/Schalt" Then : TextBox1.Text = "3,647" : TextBox2.Text = "0"
    32. TextBox3.Text = "3,778" 'Gang1
    33. TextBox4.Text = "2,063" 'Gang2
    34. TextBox5.Text = "1,455" 'Gang3
    35. TextBox6.Text = "1,1073" 'Gang4
    36. TextBox7.Text = "0,875" 'Gang5
    37. TextBox8.Text = "0,733" 'Gang6
    38. TextBox9.Text = "0" 'Gang7
    39. End If
    40. If Auswahl = "VW Golf VII 1.4TSI 125PS/Schalt" Then : TextBox1.Text = "3,647" : TextBox2.Text = "0"
    41. TextBox3.Text = "3,778" 'Gang1
    42. TextBox4.Text = "2,063" 'Gang2
    43. TextBox5.Text = "1,455" 'Gang3
    44. TextBox6.Text = "1,1073" 'Gang4
    45. TextBox7.Text = "0,875" 'Gang5
    46. TextBox8.Text = "0,733" 'Gang6
    47. TextBox9.Text = "0" 'Gang7
    48. End If
    49. '###############################################################################################
    50. '###############################################################################################
    51. Dim a = Double.Parse(TextBox1.Text) 'Achsantrieb1
    52. Dim b = Double.Parse(TextBox2.Text) 'Achsantrieb2
    53. Dim c = Double.Parse(TextBox3.Text) 'Gang1
    54. Dim d = Double.Parse(TextBox4.Text) 'Gang2
    55. Dim h = Double.Parse(TextBox5.Text)
    56. Dim f = Double.Parse(TextBox6.Text)
    57. Dim g = Double.Parse(TextBox7.Text)
    58. Dim i = Double.Parse(TextBox8.Text)
    59. Dim j = Double.Parse(TextBox9.Text)
    60. If TextBox2.Text <= "0" Then
    61. Dim res1 = a * c
    62. Dim res2 = a * d
    63. Dim res3 = a * h
    64. Dim res4 = a * f
    65. Dim res5 = a * g '2 Achsübersetzung
    66. Dim res6 = a * i '2 Achsübersetzung
    67. Dim res7 = a * j '2 Achsübersetzung
    68. If TextBox2.Text >= "0" Then
    69. Dim res1 = a * c
    70. Dim res2 = a * d
    71. Dim res3 = a * h
    72. Dim res4 = a * f
    73. Dim res5 = b * g '2 Achsübersetzung
    74. Dim res6 = b * i '2 Achsübersetzung
    75. Dim res7 = b * j '2 Achsübersetzung
    76. End If
    77. TextBox17.Text = res1.ToString(CInt("0.###"))
    78. TextBox16.Text = res2.ToString("0.###")
    79. TextBox15.Text = res3.ToString("0.###")
    80. TextBox14.Text = res4.ToString("0.###")
    81. TextBox13.Text = res5.ToString("0.###")
    82. TextBox12.Text = res6.ToString("0.###")
    83. TextBox11.Text = res7.ToString("0.###")
    84. End Sub
    85. End Class

    ganz klappt es leider noch nicht,weis einer warum ?

    Visual Basic-Quellcode

    1. If TextBox2.Text <= "0" Then
    2. Dim res1 = a * c
    3. Dim res2 = a * d
    4. Dim res3 = a * h
    5. Dim res4 = a * f
    6. Dim res5 = a * g '2 Achsübersetzung
    7. Dim res6 = a * i '2 Achsübersetzung
    8. Dim res7 = a * j '2 Achsübersetzung
    9. ElseIf TextBox2.Text >= "0" Then
    10. Dim res1 = a * c
    11. Dim res2 = a * d
    12. Dim res3 = a * h
    13. Dim res4 = a * f
    14. Dim res5 = b * g '2 Achsübersetzung
    15. Dim res6 = b * i '2 Achsübersetzung
    16. Dim res7 = b * j '2 Achsübersetzung
    17. TextBox17.Text = res1.ToString("0.###")
    18. TextBox16.Text = res2.ToString("0.###")
    19. TextBox15.Text = res3.ToString("0.###")
    20. TextBox14.Text = res4.ToString("0.###")
    21. TextBox13.Text = res5.ToString("0.###")
    22. TextBox12.Text = res6.ToString("0.###")
    23. TextBox11.Text = res7.ToString("0.###")
    24. TextBox19.Text = res3.ToString("0.###")
    25. End If
    Warum rechnet der nicht richtig in einem Code ?

    Mit

    Visual Basic-Quellcode

    1. If TextBox2.Text <= "1" Then
    sage ich ihm doch wenn der Wert in Textbox 2 kleiner als "1" ist soll er anwenden

    ist der wert höher als "1" soll er anders rechnen .
    So währe es logisch für mich.
    Zeigt keinen Fehler an ,aber rechnet falsch :(
    Zeigt keine Fehler an weil Option Strict auf Off steht. Und ein String kann niemals kleiner oder gleich ein anderer String sein.
    Integer.Parse(TextBox2.Text) <= 1

    Und für deine "Ich will alle Leeren"-Funktion, wäre eine Schleife hilfreich.
    Siehe auch Me.Controls in dem Zusammenhang.
    Ich habe das versucht kam nicht ganz zurecht,
    was haltest du von dem code

    Visual Basic-Quellcode

    1. Dim res1 = a * c
    2. Dim res2 = a * d
    3. Dim res3 = a * h
    4. Dim res4 = a * f
    5. Dim res5 = a * g '2 Achsübersetzung
    6. Dim res6 = a * i '2 Achsübersetzung
    7. Dim res7 = a * j '2 Achsübersetzung
    8. If b <= 0 Then
    9. res5 = a * g
    10. res6 = a * i
    11. res7 = a * j
    12. ElseIf d >= 0 Then
    13. res5 = b * g
    14. res6 = b * i
    15. res7 = 8 * j
    16. End If

    ichduersie schrieb:

    Schalt mal Option Strict On!
    Hier nochmal der Link: msdn.microsoft.com/de-de/library/zcd4xwzs.aspx

    Allein das späte binden

    VB.NET-Quellcode

    1. ​Dim res1 = a * c


    verschlechtert die Lesbarkeit um ein Vielfaches und kann Fehler verursachen. Echt nur ein gut gemeinter Tipp auch wenn die Hälfte deines Quellcodes dann erstmal voll mit Fehlern ist. Option Strict On verhindert viele (deiner) Fehler schon während der Entstehung und meckert sofort rum, sodass viele (dumme) Fehler richtig einfach vermieden werden können.

    Hier nochmal 'ne echt gute Website von @ErfinderDesRades: home.arcor.de/eckardahlers/Pro…/Blogs/WarumStrictOn.html

    LG :)
    So ,
    ich habe derzeit über 100 Azwahlmöglichkeiten in das Programm eingefplegt und ist wählbar über die Combobox.
    Da das einpflegen der werte sehr müsam ist habe ich eine Frage an euch !

    Kann man die ganzen werte über eine Excel Datei einlesen ?
    In der Spalte A solte sich dann die If Auswahl für die Combpbox befinde.
    Z.b. A2 = Auswahl
    C2 Textbox1
    D2 Textbox2
    E2 Textbox4 usw....
    Nächste Combobox auswahl währe dann eine Zeile tiefer also A3
    C3 wieder Textbox1
    D3 wieder Textbox2 usw....

    Kann man dann die Datenquelle also das Excel File diekt in die exe miteinbauen ?


    Derzeit starte ich das wie gehabt über die Auswahl
    Hat das schon mal wer gemacht ?!
    Danke im vorraus.

    VB.NET-Quellcode

    1. Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    2. Dim Auswahl As String = ComboBox1.Text
    3. If Auswahl = "111" Then : TextBox1.Text = "37" : TextBox2.Text = "9"
    4. TextBox3.Text = "28" : TextBox4.Text = "8"
    5. TextBox5.Text = "34" : TextBox6.Text = "18"
    6. TextBox7.Text = "33" : TextBox8.Text = "25"
    7. TextBox9.Text = "30" : TextBox10.Text = "29"
    8. TextBox11.Text = "29" : TextBox12.Text = "36"
    9. TextBox13.Text = "26" : TextBox14.Text = "38"
    10. TextBox15.Text = "38" : TextBox16.Text = "11"
    11. End If
    12. If Auswahl = "222" Then : TextBox1.Text = "6" : TextBox2.Text = "7"
    13. TextBox3.Text = "28" : TextBox4.Text = "8"
    14. TextBox5.Text = "34" : TextBox6.Text = "18"
    15. TextBox7.Text = "33" : TextBox8.Text = "25"
    16. TextBox9.Text = "30" : TextBox10.Text = "29"
    17. TextBox11.Text = "29" : TextBox12.Text = "36"
    18. TextBox13.Text = "26" : TextBox14.Text = "38"
    19. TextBox15.Text = "38" : TextBox16.Text = "11"
    20. End If

    Dazu brauch man kein Excelfile, man kann es auch über ein xml File lösen. Das was du vor hast. Bei einem Excelfile hast du das Problem, wenn es jemand löscht und niemand hat ein BU davon sind die Daten weg. Wenn du aber her gehst und Codeseitig das xml-File erstellst mit inhalt und eine Vorhanden überprüfung einbaust kann es nach dem Kompilieren nie verschwinden, da du es immer wenn es nicht das ist Codeseitig erstellst. Der Nachteil ist, das du eine Methode finden musst, um sicher weitere Werte dem File hinzuzfügen ohne erneut das Programm zu Kompilieren müssen.

    brembo00 schrieb:

    über die Auswahl
    Wie ich das sehe, stellst Du in Deinen zahlreichen TextBoxen ausschließlich numerische Werte dar.
    2 Ansatzpunkte:
    1. keine TextBoxen mehr, sondern ein DataGridView, da lassen sich auch ganz easy Tabellen dran binden.
    2. numerische Controls statt Texteingabe.
      Falls TextBox, dann NumericUpDown oder eine eigene NumericTextBox, gugst Du hier.
      falls DataGridView, dann eine numerische DGV-Column, gugst Du hier.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!