
23866
26.01.2006, aktualisiert am 06.02.2006
Label Caption zur Laufzeit ändern und speichern (VB6)
Label Caption zur Laufzeit ändern und speichern
Dieser Sourcecode kann frei verwendet werden
Dieses Tutorial bezieht sich auf Visual Basic 6 !
Auf der Suche nach einer Möglichkeit die Label Caption zur Laufzeit zu ändern habe ich mir folgende Variante überlegt. Wahrscheinlich gibt es noch eine andere bessere oder einfachere Möglichkeit aber diese Variante funktioniert auch.
Um die Label Caption beim nächsten Programmstart wieder auf dem letzten Stand zuhaben, werden die Werte in einer Textdatei (setting.txt) gespeichert.
Falls die Datei (setting.txt) nicht existiert wird sie automatisch angelegt.
Also, alles was wir benötigen ist folgendes:
1. Auf Form1:
Einen Button (Command1) mit Caption "beenden und speichern"
Einen Button (Command2) mit Caption "Reset alle Labels"
6x ein Label Control (Label1) mit Index von 0 - 5
Und folgenden Code für Form1:
Option Explicit
Public ind As Integer
Dim setting As String
Dim F As Integer
Private Sub Command1_Click()
saveSet
Me.Hide
Unload Me
End Sub
Private Sub Command2_Click()
For ind = 0 To Label1.Count - 1
Label1(ind).Caption = "leer"
Next
End Sub
Private Sub Form_Load()
setting = App.Path & "\Setting.txt"
If FileExists(setting) = True Then
readSet
Else
For ind = 0 To Label1.Count - 1
Label1(ind).Caption = "leer"
Next
Call MsgBox("Setting.txt konnte nicht gefunden werden und wurde neu angelegt ", vbOKOnly + vbExclamation + vbSystemModal + vbDefaultButton1, "Hinweis - Setting.txt")
saveSet
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
saveSet
End Sub
Private Sub Label1_Click(Index As Integer)
ind = (Index)
Load Form2
Form2.Show
End Sub
Public Function saveSet()
'Settings.txt Speichern
Dim F As Integer
F = FreeFile
setting = App.Path & "\Setting.txt"
Open setting For Output As #F
Print #F, Label1(0).Caption
Print #F, Label1(1).Caption
Print #F, Label1(2).Caption
Print #F, Label1(3).Caption
Print #F, Label1(4).Caption
Print #F, Label1(5).Caption
Close F
End Function
Public Function readSet()
setting = App.Path & "\Setting.txt"
'Settings.txt einlesen
Dim F As Integer
Dim Zeile0 As String
Dim Zeile1 As String
Dim Zeile2 As String
Dim Zeile3 As String
Dim Zeile4 As String
Dim Zeile5 As String
F = FreeFile
Open setting For Input As #F
Line Input #F, Zeile0
Line Input #F, Zeile1
Line Input #F, Zeile2
Line Input #F, Zeile3
Line Input #F, Zeile4
Line Input #F, Zeile5
'Settings aus Settings.txt zuordnen
Label1(0).Caption = Zeile0
Label1(1).Caption = Zeile1
Label1(2).Caption = Zeile2
Label1(3).Caption = Zeile3
Label1(4).Caption = Zeile4
Label1(5).Caption = Zeile5
Close F
End Function
Public Function FileExists(ByVal sFile As String) As Boolean
'Der Parameter sFile enthält den zu prüfenden Dateinamen
Dim Size As Long
On Local Error Resume Next
Size = FileLen(sFile)
FileExists = (Err = 0)
On Local Error GoTo 0
End Function
Auf Form2 benötigen wir:
Eine Textbox (Text1)
1 Button (Command1) mit Caption "Caption ändern"
1 Button (Command2) mit Caption "Abbruch"
Und folgenden Code für Form2:
Option Explicit
Dim n As String
Dim save As String
Private Sub Command1_Click()
Form1.Label1(n).Caption = Text1.Text
save = Form1.saveSet
Me.Hide
Unload Me
End Sub
Private Sub Command2_Click()
Me.Hide
Unload Me
End Sub
Private Sub Form_Load()
n = Form1.ind
End Sub
Dieser Sourcecode kann frei verwendet werden
Dieses Tutorial bezieht sich auf Visual Basic 6 !
Auf der Suche nach einer Möglichkeit die Label Caption zur Laufzeit zu ändern habe ich mir folgende Variante überlegt. Wahrscheinlich gibt es noch eine andere bessere oder einfachere Möglichkeit aber diese Variante funktioniert auch.
Um die Label Caption beim nächsten Programmstart wieder auf dem letzten Stand zuhaben, werden die Werte in einer Textdatei (setting.txt) gespeichert.
Falls die Datei (setting.txt) nicht existiert wird sie automatisch angelegt.
Also, alles was wir benötigen ist folgendes:
1. Auf Form1:
Einen Button (Command1) mit Caption "beenden und speichern"
Einen Button (Command2) mit Caption "Reset alle Labels"
6x ein Label Control (Label1) mit Index von 0 - 5
Und folgenden Code für Form1:
Option Explicit
Public ind As Integer
Dim setting As String
Dim F As Integer
Private Sub Command1_Click()
saveSet
Me.Hide
Unload Me
End Sub
Private Sub Command2_Click()
For ind = 0 To Label1.Count - 1
Label1(ind).Caption = "leer"
Next
End Sub
Private Sub Form_Load()
setting = App.Path & "\Setting.txt"
If FileExists(setting) = True Then
readSet
Else
For ind = 0 To Label1.Count - 1
Label1(ind).Caption = "leer"
Next
Call MsgBox("Setting.txt konnte nicht gefunden werden und wurde neu angelegt ", vbOKOnly + vbExclamation + vbSystemModal + vbDefaultButton1, "Hinweis - Setting.txt")
saveSet
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
saveSet
End Sub
Private Sub Label1_Click(Index As Integer)
ind = (Index)
Load Form2
Form2.Show
End Sub
Public Function saveSet()
'Settings.txt Speichern
Dim F As Integer
F = FreeFile
setting = App.Path & "\Setting.txt"
Open setting For Output As #F
Print #F, Label1(0).Caption
Print #F, Label1(1).Caption
Print #F, Label1(2).Caption
Print #F, Label1(3).Caption
Print #F, Label1(4).Caption
Print #F, Label1(5).Caption
Close F
End Function
Public Function readSet()
setting = App.Path & "\Setting.txt"
'Settings.txt einlesen
Dim F As Integer
Dim Zeile0 As String
Dim Zeile1 As String
Dim Zeile2 As String
Dim Zeile3 As String
Dim Zeile4 As String
Dim Zeile5 As String
F = FreeFile
Open setting For Input As #F
Line Input #F, Zeile0
Line Input #F, Zeile1
Line Input #F, Zeile2
Line Input #F, Zeile3
Line Input #F, Zeile4
Line Input #F, Zeile5
'Settings aus Settings.txt zuordnen
Label1(0).Caption = Zeile0
Label1(1).Caption = Zeile1
Label1(2).Caption = Zeile2
Label1(3).Caption = Zeile3
Label1(4).Caption = Zeile4
Label1(5).Caption = Zeile5
Close F
End Function
Public Function FileExists(ByVal sFile As String) As Boolean
'Der Parameter sFile enthält den zu prüfenden Dateinamen
Dim Size As Long
On Local Error Resume Next
Size = FileLen(sFile)
FileExists = (Err = 0)
On Local Error GoTo 0
End Function
Auf Form2 benötigen wir:
Eine Textbox (Text1)
1 Button (Command1) mit Caption "Caption ändern"
1 Button (Command2) mit Caption "Abbruch"
Und folgenden Code für Form2:
Option Explicit
Dim n As String
Dim save As String
Private Sub Command1_Click()
Form1.Label1(n).Caption = Text1.Text
save = Form1.saveSet
Me.Hide
Unload Me
End Sub
Private Sub Command2_Click()
Me.Hide
Unload Me
End Sub
Private Sub Form_Load()
n = Form1.ind
End Sub
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 24513
Url: https://administrator.de/forum/label-caption-zur-laufzeit-aendern-und-speichern-vb6-24513.html
Ausgedruckt am: 26.04.2025 um 19:04 Uhr
6 Kommentare
Neuester Kommentar

Wer programmiert, weiß, daß es unnötig ist, so eine ellenlange Prozedur zu schreiben...
Lonesome Walker
Lonesome Walker

Hey Plonk,
ohn jetzt mal auf Deinen Nick einzugehen, aber
Du bist mir irgendwie sympathisch.
Lonesome Walker
ohn jetzt mal auf Deinen Nick einzugehen, aber
Du bist mir irgendwie sympathisch.
Lonesome Walker
...hab mich nochmal schlau gemacht, für die die es interessiert.
Also man könnte diesen Teil hier:
setting = App.Path & "\Setting.txt"
'Settings.txt einlesen
Dim F As Integer
Dim Zeile0 As String
Dim Zeile1 As String
Dim Zeile2 As String
Dim Zeile3 As String
Dim Zeile4 As String
Dim Zeile5 As String
F = FreeFile
Open setting For Input As #F
Line Input #F, Zeile0
Line Input #F, Zeile1
Line Input #F, Zeile2
Line Input #F, Zeile3
Line Input #F, Zeile4
Line Input #F, Zeile5
'Settings aus Settings.txt zuordnen
Label1(0).Caption = Zeile0
Label1(1).Caption = Zeile1
Label1(2).Caption = Zeile2
Label1(3).Caption = Zeile3
Label1(4).Caption = Zeile4
Label1(5).Caption = Zeile5
Close F
so abkürzen:
'festlegen der settingsdatei und pfad
setting = App.Path & "\Setting.txt"
'wenn überhaupt vorhanden
if dir$(setting)<>"" then
'open
f=freefile
i=-1
open setting for input as #f
'nach und nach alle settings einlesen...
do
i=i+1
line input#f, label(i).caption
'bis am dateiende angekommen
loop until eof(f)
close #f
endif
Das geht nicht so sehr verschwenderisch mit den Variablen um, außerdem kann man das als Public-Function einbinden, um allgemein Labels mit neuen Werten zu füllen.
Also man könnte diesen Teil hier:
setting = App.Path & "\Setting.txt"
'Settings.txt einlesen
Dim F As Integer
Dim Zeile0 As String
Dim Zeile1 As String
Dim Zeile2 As String
Dim Zeile3 As String
Dim Zeile4 As String
Dim Zeile5 As String
F = FreeFile
Open setting For Input As #F
Line Input #F, Zeile0
Line Input #F, Zeile1
Line Input #F, Zeile2
Line Input #F, Zeile3
Line Input #F, Zeile4
Line Input #F, Zeile5
'Settings aus Settings.txt zuordnen
Label1(0).Caption = Zeile0
Label1(1).Caption = Zeile1
Label1(2).Caption = Zeile2
Label1(3).Caption = Zeile3
Label1(4).Caption = Zeile4
Label1(5).Caption = Zeile5
Close F
so abkürzen:
'festlegen der settingsdatei und pfad
setting = App.Path & "\Setting.txt"
'wenn überhaupt vorhanden
if dir$(setting)<>"" then
'open
f=freefile
i=-1
open setting for input as #f
'nach und nach alle settings einlesen...
do
i=i+1
line input#f, label(i).caption
'bis am dateiende angekommen
loop until eof(f)
close #f
endif
Das geht nicht so sehr verschwenderisch mit den Variablen um, außerdem kann man das als Public-Function einbinden, um allgemein Labels mit neuen Werten zu füllen.