Tipp 0057 Programmeinstellungen in Ressource speichern
Autor/Einsender:
Datum:
  Michael Werner
10.10.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Wie einfach man benutzerdefinierte Anwendungseinstellungen in einer Ressource-Datei speichern und wieder auslesen kann (mit Hilfe der Klassen ResourceWriter und ResourceReader), zeigt dieser Tipp. Zum Testen verändern Sie die Einstellungen, beenden das Programm und starten Sie es erneut.
 
Imports System.Resources
Imports System.IO

Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Private Const RES_FILENAME As String = "my.resources"
  Private transCol As ColorTranslator

  Private Sub Form1_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    If File.Exists(Application.StartupPath & "\" & _
              RES_FILENAME) Then
      ReadRessource()
    End If
  End Sub

  Private Sub WriteRessource()
    Dim rw As ResourceWriter = New ResourceWriter(RES_FILENAME)

    rw.AddResource("getpos", CheckBox2.Checked.ToString)
    If CheckBox2.Checked Then
      rw.AddResource("left", Me.Left.ToString)
      rw.AddResource("top", Me.Top.ToString)
    End If
    rw.AddResource("topmost", Me.TopMost.ToString)
    rw.AddResource("name", TextBox1.Text)
    rw.AddResource("backcolor", transCol.ToHtml(Me.BackColor))
    rw.AddResource("erstelldatum", "Ihre Einstellungen von " & _
           Date.Now.ToLongDateString() & "   " & _
           Date.Now.ToLongTimeString)
    rw.Close()
  End Sub

  Private Sub ReadRessource()
    Dim rr As ResourceReader = New ResourceReader(RES_FILENAME)
    Dim dict As IDictionaryEnumerator = rr.GetEnumerator()

    While dict.MoveNext()
      Select Case dict.Key.ToString
        Case "getpos"
          CheckBox2.Checked = CBool(dict.Value.ToString)
        Case "left"
          If CheckBox2.Checked Then
            Me.Left = CInt(dict.Value.ToString)
          End If
        Case "top"
          If CheckBox2.Checked Then
            Me.Top = CInt(dict.Value.ToString)
          End If
        Case "topmost"
          CheckBox1.Checked = CBool(dict.Value.ToString)
        Case "name"
          TextBox1.Text = dict.Value.ToString()
          Me.Text = "Hallo " & TextBox1.Text
        Case "backcolor"
          Me.BackColor = transCol.FromHtml(dict.Value.ToString)
        Case "erstelldatum"
          Label2.Text = dict.Value.ToString()
        Case Else
      End Select
    End While

    rr.Close()
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Me.BackColor = Color.FromKnownColor(KnownColor.Control)
    Me.StartPosition = FormStartPosition.CenterScreen
    CheckBox1.Checked = False
    CheckBox2.Checked = False
    TextBox1.Text = "vb-fun"
    Me.Text = "Hallo " & TextBox1.Text
    Label2.Text = String.Empty
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button3.Click
    ColorDialog1.AnyColor = True
    ColorDialog1.Color = Me.BackColor
    If (ColorDialog1.ShowDialog() = DialogResult.OK) Then
      Me.BackColor = ColorDialog1.Color
    End If
  End Sub

  Private Sub Form1_Closing(ByVal sender As Object, _
          ByVal e As System.ComponentModel.CancelEventArgs) _
          Handles MyBase.Closing
    WriteRessource()
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
  End Sub

  Private Sub CheckBox1_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
      Me.TopMost = True
    Else
      Me.TopMost = False
    End If
  End Sub

  Private Sub TextBox1_TextChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles TextBox1.TextChanged
    Me.Text = TextBox1.Text
  End Sub
End Class
 
Weitere Links zum Thema
Binäre Serialisation
Cursor aus Ressource laden
Objekte erstellen und serialisieren

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (8,7 kB) Downloads bisher: [ 1044 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Montag, 23. Januar 2012