Comboboxeintrag in Datei Speichern und wieder ins Programm laden

  • VB.NET
  • .NET (FX) 3.0–3.5

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Comboboxeintrag in Datei Speichern und wieder ins Programm laden

    Guten Tag Zusammen

    Ich habe hier ein Programm welches Formulare abfüllt und dazu werden alle Textboxen, Comboboxen und so weiter nachdem absenden zu Word in ein File gespeichert um diese als Entwurf abzurufen. Beim Aufrufen des Entwurfs wird dann alles wieder in die Felder gefüllt un da kommen wir schon zu meinem Problem. Ich habs irgendwie schlussendlich hingekriegt die DateTimePicker durch die Arrays und so weiter richtig zu formatieren aber ComboBoxen wollen bei mir einfach nicht. in der Datei steht immer das der selektierte Wert 0 ist und so wirds dann auch nicht ausgefüllt. Anzumerken ist vielleicht das die Werte in einer lokalen Datenbank sind welche man erweitern kann. Diese werden dann mit einer Art Intellisense für alle Felder vorgeschlagen wenn man etwas ähnliches eingibt. Den Code zum abfüllen in die Datei:

    VB.NET-Quellcode

    1. ComboBox4.SelectedIndex.ToString()​


    Zum Importieren ins Programm:

    VB.NET-Quellcode

    1. ​Main.ComboBox4.SelectedIndex = CInt(parts(35))


    parts ist das Array welches aus der Datei heraus mit den Werten abgefüllt wird.

    Kann mir wer sagen was es zu beachten gibt oder was ich falsch mache? Bei den Comboboxen welche feste Werte haben die nicht in der Datenbank sind funktioniert es nur halbwegs, scheint so als würde es manchmal abfüllen und manchmal nicht, ich geh dem noch nach.

    mfg

    Orion
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    Wie es wird nirgends in eine Datei geschrieben? Hab über die Suche nicht viel gefunden. Also bei den Comboboxen mit den festen Werten bin ich mir jetzt sicher das sie funktionieren aber die Datengebundenen noch nicht. Muss man diese vielleicht irgendwie refreshen bevor man die mit dem Code selektiert? Hier wäre mein ganzes Snippet zum abspeichern der Datei und Einlesen, weiss jetzt nicht was das ausmacht.
    Spoiler anzeigen

    VB.NET-Quellcode

    1. ​Dim strEnt As String = RichTextBox1.Text
    2. Dim splitStrEnt As String() = strEnt.Split(","c)
    3. Dim StringEnt As String
    4. Dim Checkboxs As String
    5. Dim RadB As String
    6. StringEnt = splitStrEnt(0).ToString()
    7. Dokumentname = Dokumentname + 1
    8. DokumentnameS = Dokumentname.ToString
    9. If RadioButton1.Checked = True Then
    10. RadB = "Frei"
    11. Else
    12. RadB = "Unfrei"
    13. End If
    14. If CheckBox1.Checked = True Then
    15. Checkboxs = "True"
    16. Else
    17. Checkboxs = "False"
    18. End If
    19. Try
    20. If My.Computer.FileSystem.FileExists(path & "\Indexierung\" & StringEnt & ".cch") Then
    21. My.Computer.FileSystem.DeleteFile(path & "\Indexierung\" & StringEnt & ".cch")
    22. My.Computer.FileSystem.WriteAllText(path & "\Indexierung\" & StringEnt & ".cch", RichTextBox1.Text + ";" _
    23. + RichTextBox2.Text + ";" + RichTextBox3.Text + ";" + RichTextBox4.Text + ";" + DateTimePicker2.Value.ToString _
    24. + ";" + RichTextBox5.Text + ";" + RichTextBox6.Text + ";" + RichTextBox7.Text + ";" + RichTextBox8.Text _
    25. + ";" + TextBox9.Text + ";" + TextBox1.Text + ";" _
    26. + ";" + ComboBox12.SelectedIndex.ToString() + ";" + TextBox10.Text + ";" + TextBox11.Text + ";" + RichTextBox9.Text + ";" + Checkboxs _
    27. + ";" + RadB + ";" + RichTextBox13.Text + ";" + RichTextBox11.Text + ";" + TextBox4.Text + ";" + TextBox8.Text _
    28. + ";" + TextBox2.Text + ";" + TextBox5.Text + ";" + TextBox6.Text + ";" + TextBox7.Text + ";" + TextBox3.Text _
    29. + ";" + ComboBox21.SelectedIndex.ToString() + ";" + ComboBox16.SelectedIndex.ToString() + ";" + ComboBox20.SelectedIndex.ToString() _
    30. + ";" + ComboBox7.SelectedIndex.ToString() + ";" + ComboBox9.SelectedIndex.ToString() + ";" + ComboBox15.SelectedIndex.ToString() _
    31. + ";" + ComboBox22.SelectedIndex.ToString() + ";" + RichTextBox12.Text + ";" + RichTextBox10.Text + ";" + ComboBox4.SelectedIndex.ToString() + ";" _
    32. + TextBox12.Text + ";" + DateTimePicker1.Value.ToString + ";" + RichTextBox14.Text + ";" + RichTextBox15.Text + ";" + RichTextBox16.Text + ";" + DateTimePicker3.Value.ToString + ";" + ComboBox2.SelectedIndex.ToString(), True)
    33. Else
    34. My.Computer.FileSystem.WriteAllText(path & "\Indexierung\" & StringEnt & ".cch", RichTextBox1.Text + ";" _
    35. + RichTextBox2.Text + ";" + RichTextBox3.Text + ";" + RichTextBox4.Text + ";" + DateTimePicker2.Value.ToString _
    36. + ";" + RichTextBox5.Text + ";" + RichTextBox6.Text + ";" + RichTextBox7.Text + ";" + RichTextBox8.Text _
    37. + ";" + TextBox9.Text + ";" + TextBox1.Text + ";" _
    38. + ";" + ComboBox12.SelectedIndex.ToString() + ";" + TextBox10.Text + ";" + TextBox11.Text + ";" + RichTextBox9.Text + ";" + Checkboxs _
    39. + ";" + RadB + ";" + RichTextBox13.Text + ";" + RichTextBox11.Text + ";" + TextBox4.Text + ";" + TextBox8.Text _
    40. + ";" + TextBox2.Text + ";" + TextBox5.Text + ";" + TextBox6.Text + ";" + TextBox7.Text + ";" + TextBox3.Text _
    41. + ";" + ComboBox21.SelectedIndex.ToString() + ";" + ComboBox16.SelectedIndex.ToString() + ";" + ComboBox20.SelectedIndex.ToString() _
    42. + ";" + ComboBox7.SelectedIndex.ToString() + ";" + ComboBox9.SelectedIndex.ToString() + ";" + ComboBox15.SelectedIndex.ToString() _
    43. + ";" + ComboBox22.SelectedIndex.ToString() + ";" + RichTextBox12.Text + ";" + RichTextBox10.Text + ";" + ComboBox4.SelectedIndex.ToString() + ";" _
    44. + TextBox12.Text + ";" + DateTimePicker1.Value.ToString + ";" + RichTextBox14.Text + ";" + RichTextBox15.Text + ";" + RichTextBox16.Text + ";" + DateTimePicker3.Value.ToString + ";" + ComboBox2.SelectedIndex.ToString(), True)
    45. End If
    46. Catch ex As Exception
    47. MsgBox("Fehler beim Speichern für die Suche")
    48. MsgBox(ex.ToString())
    49. End Try


    Und zum Importieren über die Intellisense:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. ​Try
    2. Dim fcb As Boolean = False
    3. Dim ValCon As Date
    4. Dim ValCon2 As Date
    5. Dim Valcon3 As Date
    6. Dim reader = My.Computer.FileSystem.ReadAllText(path & "\Indexierung\" + IsBestKeyWord)
    7. Dim parts = reader.Split(";"c)
    8. Dim part12 = parts(38).ToString.Split(" "c)
    9. Dim tripple2 = part12(0).Split("."c)
    10. Main.RichTextBox1.Text = parts(0)
    11. Main.RichTextBox2.Text = parts(1)
    12. Main.RichTextBox3.Text = parts(2)
    13. Main.RichTextBox4.Text = parts(3)
    14. Dim part1 = parts(4).ToString.Split(" "c)
    15. Dim tripple = part1(0).Split("."c)
    16. Dim EndDate As String = tripple(0) + "/" + tripple(1) + "/" + tripple(2)
    17. ValCon = Date.ParseExact(EndDate, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    18. Main.DateTimePicker2.Value = ValCon
    19. Main.RichTextBox5.Text = parts(5)
    20. Main.RichTextBox6.Text = parts(6)
    21. Main.RichTextBox7.Text = parts(7)
    22. Main.RichTextBox8.Text = parts(8)
    23. Main.TextBox9.Text = parts(9)
    24. Main.TextBox1.Text = parts(10)
    25. If parts(11) = "" Then
    26. fcb = True
    27. Else
    28. Main.ComboBox12.SelectedItem = CInt(parts(11))
    29. End If
    30. If fcb = True Then
    31. Main.TextBox10.Text = parts(13)
    32. Main.TextBox11.Text = parts(14)
    33. Main.RichTextBox9.Text = parts(15)
    34. If parts(16).ToString = "True" Then
    35. Main.CheckBox1.Checked = True
    36. Else
    37. End If
    38. If parts(17).ToString = "Frei" Then
    39. Main.RadioButton1.Select()
    40. Else
    41. Main.RadioButton2.Select()
    42. End If
    43. Main.RichTextBox13.Text = parts(18)
    44. Main.RichTextBox11.Text = parts(19)
    45. Main.TextBox4.Text = parts(20)
    46. Main.TextBox8.Text = parts(21)
    47. Main.TextBox2.Text = parts(22)
    48. Main.TextBox5.Text = parts(23)
    49. Main.TextBox6.Text = parts(24)
    50. Main.TextBox7.Text = parts(25)
    51. Main.TextBox3.Text = parts(26)
    52. If parts(27) = "" Then
    53. Else
    54. Main.ComboBox21.SelectedIndex = CInt(parts(27))
    55. End If
    56. If parts(28) = "" Then
    57. Else
    58. Main.ComboBox16.SelectedIndex = CInt(parts(28))
    59. End If
    60. If parts(29) = "" Then
    61. Else
    62. Main.ComboBox20.SelectedIndex = CInt(parts(29))
    63. End If
    64. If parts(30) = "" Then
    65. Else
    66. Main.ComboBox7.SelectedIndex = CInt(parts(30))
    67. End If
    68. If parts(31) = "" Then
    69. Else
    70. Main.ComboBox9.SelectedIndex = CInt(parts(31))
    71. End If
    72. If parts(32) = "" Then
    73. Else
    74. Main.ComboBox15.SelectedIndex = CInt(parts(32))
    75. End If
    76. If parts(33) = "" Then
    77. Else
    78. Main.ComboBox22.SelectedIndex = CInt(parts(33))
    79. End If
    80. Main.RichTextBox12.Text = parts(34)
    81. Main.RichTextBox10.Text = parts(35)
    82. If parts(36) = "" Then
    83. Else
    84. Main.ComboBox4.SelectedIndex = CInt(parts(36))
    85. End If
    86. Main.TextBox12.Text = parts(37)
    87. If tripple2.Length = 3 Then
    88. If tripple2(0).Contains("") Then
    89. tripple2(0) = 11
    90. End If
    91. If tripple2(1).Contains("") Then
    92. tripple2(1) = 11
    93. End If
    94. If tripple2(2).Contains("") Then
    95. tripple2(2) = 1990
    96. End If
    97. Dim EndDate2 As String = tripple2(0) + "/" + tripple2(1) + "/" + tripple2(2)
    98. ValCon2 = Date.ParseExact(EndDate2, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    99. Main.DateTimePicker1.Value = ValCon2
    100. End If
    101. Main.RichTextBox14.Text = parts(38)
    102. Main.RichTextBox15.Text = parts(39)
    103. Main.RichTextBox16.Text = parts(40)
    104. Dim part13 = parts(41).ToString.Split(" "c)
    105. Dim tripple3 = part13(0).Split("."c)
    106. If tripple3.Length = 3 Then
    107. If tripple3(0).Contains("") Then
    108. tripple3(0) = 11
    109. End If
    110. If tripple3(1).Contains("") Then
    111. tripple3(1) = 11
    112. End If
    113. If tripple3(2).Contains("") Then
    114. tripple3(2) = 1990
    115. End If
    116. Dim EndDate3 As String = tripple3(0) + "/" + tripple3(1) + "/" + tripple3(2)
    117. Valcon3 = Date.ParseExact(EndDate3, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    118. Main.DateTimePicker3.Value = Valcon3
    119. ElseIf tripple3.Length <> 3 Then
    120. Valcon3 = Date.ParseExact("11/11/1900", "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    121. Main.DateTimePicker3.Value = Valcon3
    122. End If
    123. If parts(43) = "" Then
    124. Else
    125. Main.ComboBox2.SelectedIndex = CInt(parts(43))
    126. End If
    127. Else
    128. Main.TextBox10.Text = parts(12)
    129. Main.TextBox11.Text = parts(13)
    130. Main.RichTextBox9.Text = parts(14)
    131. If parts(15).ToString = "True" Then
    132. Main.CheckBox1.Checked = True
    133. Else
    134. End If
    135. If parts(16).ToString = "Frei" Then
    136. Main.RadioButton1.Select()
    137. Else
    138. Main.RadioButton2.Select()
    139. End If
    140. Main.RichTextBox13.Text = parts(17)
    141. Main.RichTextBox11.Text = parts(18)
    142. Main.TextBox4.Text = parts(19)
    143. Main.TextBox8.Text = parts(20)
    144. Main.TextBox2.Text = parts(21)
    145. Main.TextBox5.Text = parts(22)
    146. Main.TextBox6.Text = parts(23)
    147. Main.TextBox7.Text = parts(24)
    148. Main.TextBox3.Text = parts(25)
    149. If parts(26) = "" Then
    150. Else
    151. Main.ComboBox21.SelectedIndex = CInt(parts(26))
    152. End If
    153. If parts(27) = "" Then
    154. Else
    155. Main.ComboBox16.SelectedIndex = CInt(parts(27))
    156. End If
    157. If parts(28) = "" Then
    158. Else
    159. Main.ComboBox20.SelectedIndex = CInt(parts(28))
    160. End If
    161. If parts(29) = "" Then
    162. Else
    163. Main.ComboBox7.SelectedIndex = CInt(parts(29))
    164. End If
    165. If parts(30) = "" Then
    166. Else
    167. Main.ComboBox9.SelectedIndex = CInt(parts(30))
    168. End If
    169. If parts(31) = "" Then
    170. Else
    171. Main.ComboBox15.SelectedIndex = CInt(parts(31))
    172. End If
    173. If parts(32) = "" Then
    174. Else
    175. Main.ComboBox22.SelectedIndex = CInt(parts(32))
    176. End If
    177. Main.RichTextBox12.Text = parts(33)
    178. Main.RichTextBox10.Text = parts(34)
    179. If parts(35) = "" Then
    180. Else
    181. Main.ComboBox4.SelectedIndex = CInt(parts(35))
    182. End If
    183. Main.TextBox12.Text = parts(36)
    184. If tripple2.Length = 3 Then
    185. If tripple2(0).Contains("") Then
    186. tripple2(0) = 11
    187. End If
    188. If tripple2(1).Contains("") Then
    189. tripple2(1) = 11
    190. End If
    191. If tripple2(2).Contains("") Then
    192. tripple2(2) = 1990
    193. End If
    194. Dim EndDate2 As String = tripple2(0) + "/" + tripple2(1) + "/" + tripple2(2)
    195. ValCon2 = Date.ParseExact(EndDate2, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    196. Main.DateTimePicker1.Value = ValCon2
    197. End If
    198. Main.RichTextBox14.Text = parts(37)
    199. Main.RichTextBox15.Text = parts(38)
    200. Main.RichTextBox16.Text = parts(39)
    201. Dim part13 = parts(40).ToString.Split(" "c)
    202. Dim tripple3 = part13(0).Split("."c)
    203. If tripple3.Length = 3 Then
    204. If tripple3(0).Contains("") Then
    205. tripple3(0) = 11
    206. End If
    207. If tripple3(1).Contains("") Then
    208. tripple3(1) = 11
    209. End If
    210. If tripple3(2).Contains("") Then
    211. tripple3(2) = 1990
    212. End If
    213. Dim EndDate3 As String = tripple3(0) + "/" + tripple3(1) + "/" + tripple3(2)
    214. Valcon3 = Date.ParseExact(EndDate3, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    215. Main.DateTimePicker3.Value = Valcon3
    216. ElseIf tripple3.Length <> 3 Then
    217. Valcon3 = Date.ParseExact("11/11/1900", "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None)
    218. Main.DateTimePicker3.Value = Valcon3
    219. End If
    220. If parts(41) = "" Then
    221. Else
    222. Main.ComboBox2.SelectedIndex = CInt(parts(41))
    223. End If
    224. End If
    225. Catch Ex As Exception
    226. MsgBox("Fehler beim abfüllen der Daten")
    227. MsgBox(Ex.ToString)
    228. End Try


    Ich weiss jetzt nicht inwiefern das weiterhelfen soll wenn ich fast die ganze Sub poste von der Übersicht her, aber wenn man ja unbedingt die Ganze Funktion haben will so wie ichs verstanden hab.
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    Die beiden Codeschnipsel im Startpost, sind ziemlich nichtssagend und der nachgereichte Code ist mir zu viel, da sehe ich den Wald vor lauter Bäumen nicht mehr. :S

    An deiner Stelle würde ich den Code aber nochmal überarbeiten. Nimm den My. Namespace raus und ersetze ihn durch ein .Net Äquivalent, z.B. My.Computer.FileSystem durch einen StreamWriter ersetzen und anstatt MsgBox, MessageBox verwenden.
    Dann sieht sein Code bischen(!) besser aus, aber sein Prob ist damit nicht gelöst.
    Das eigliche Problem ist, dass hier eine Datenverarbeitung ohne Datenmodell versucht wird. Dadurch verstreuen sich alle eingelesenen Werte in zig oder hunderten von Controls, und zum Speichern das ganze Chaos reverse.

    Aber wenn man schon in sonem Control-Gewusel steckt, dann wird man auch nicht noch mal neu anfangen, und überhaupt lernen, was ein Datenmodell ist.
    Das Projekt war damals eines meiner ersten Projekte, ich wusste genau das ich auf solche Antworten stossen werde darum hab ich erst nur die Schnippsel geschrieben weil ich dieses eine Problem beheben können muss. Wie ist das mit dem Datenmodell? Vielleicht darf ich noch so viel Zeit dafür aufwenden das neu auf zu bauen.
    Metal-Schweiz wurde nun offiziell veröffentlich nach all den Jahren :)

    Für Datenmodellierung muss man weiter ausholen, das ist ganz allgemein in Punkt#5 auf Datenverarbeitungs-Vorraussetzungen behandelt.
    Das musste erstmal verstehen, und dann kannst du ein Dataset anlegen mit einem Datenmodell der Formulare, die du zu bearbeiten hast.
    In so ein Dataset kann man dann die Formulare einlesen, das Dataset abspeichern, oder auch dafür Code schreiben, der's in iwelchen anneren Formaten exportiert.

    Letztendlich ist das alles ganz unsichtbar, aber unsichtbare Sachen sind schlecht zu entwickeln.
    Daher guck dir auch punkt#6 an, zumindest ein paar der vielen Möglichkeiten, dein Dataset sichtbar zu machen.
    Vermutlich hilfreich wird sein, dein Datenmodell hier zu diskutieren.
    (Evtl stellen die Formulare auch aussergewöhnliche Anforderungen an die Modellierung, oder aber es ist zu einfach, dass man drauf kommt.)