| | |
Setting an object defaults.
![]() |
Hi guys
I have a a text box that is initialized with some value, let's say "William".
When I run the program, the text box shows "William" of course, but what I want to do is to type something else, let's say "Vincent" and set this value as the default value, so when I run the program next time, it will show "Vincent" instead of "William".
Thanks in advance.
I have a a text box that is initialized with some value, let's say "William".
When I run the program, the text box shows "William" of course, but what I want to do is to type something else, let's say "Vincent" and set this value as the default value, so when I run the program next time, it will show "Vincent" instead of "William".
Thanks in advance.
The easiest way to do this, is to have a settings file (you could use the registry also, but I find files are easier to clean up, and fit logical structure a lot better). In your settings file, you have 1 line per text box (or default value). This way, line 1 of the file will always be the default value for text1. Line 2 for text2, etc, etc. Then, on form load (or sub main, or whatever), just open the file for input, read it in, and set the values accordingly. This is how I do it. I actually have a sub that I call on startup, that makes sure everything is set before it actually loads. I just callt he sub in form_load, and it makes sure I have directories (should I need them), and if I have settings it loads the settings....
Just a plain text file.... something lto write the file ike:
and read it like:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
open app.path & "\settings.dat" for output as #1 print #1, text1.text print #1, text2.text close #1
and read it like:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
if dir(app.path & "\settings.dat", vbnormal) <> "" then open app.path & "\settings.dat" for input as #1 line input #1, text1.text line input #1, text2.text close #1 end if
I think files are great because you can transfer them around, but they do involve more steps than the registry. I personally would rather use the windows registry, specially if you only need the textbox to display the last entry. From the coding point of view, I think it's easier than using files, and I think the registry helps keep prying eyes out.
As a person who is diligent in the open source community, even programs that are NOT open source, should still have the availibility to have the settings altered fairly easily. That's one reason I've hated proprietary software, is because it's on my computer, I should be able to mess with it as I please..... If I want to change the settings with Notepad, or DOS Edit instead of some gui that someone has decided was the best interface, I should have the option and that right, and that should be made fairly easily to access. Granted, you could use regedit to "manually" alter settings, but it's the principle of the idea behind it. Plus, there isn't really that many more steps needed.... on form unload, simply do an open for output (which overwrites previous values) and on form_load read the input into the correct textboxes.... but either way you choose to do it, I wish you the best of luck.
Hi.
Thanks again, that worked !!
I have only one more question: when i read from the file I have to save the values to a variable, and then copy the value of the variable to the combo box. If I do it directly it does not work. However, when writing to the file, I can do it directly. Am I doing something wrong? I guess that's the way it is.
Here is the code for reading:
and for writing:
Thanks.
Thanks again, that worked !!
I have only one more question: when i read from the file I have to save the values to a variable, and then copy the value of the variable to the combo box. If I do it directly it does not work. However, when writing to the file, I can do it directly. Am I doing something wrong? I guess that's the way it is.
Here is the code for reading:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
if dir(app.path & "\settings.dat", vbnormal) <> "" then Open App.Path & "\settings.dat" For Input As #1 Do While Not EOF(1) Line Input #1, name cmbNames(i).Text = name i = i + 1 Loop Close #1 End If
and for writing:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Open App.Path & "\settings.dat" For Output As #1 Print #1, cmbNames(0).Text Print #1, cmbNames(1).Text Print #1, cmbNames(2).Text Close #1
Thanks.
Hi;
You might try using .AddItem instead of .Text. By clearing the combo box first ( .Clear ) it should then refill the list.
Try this code:
You might try using .AddItem instead of .Text. By clearing the combo box first ( .Clear ) it should then refill the list.
•
•
•
•
if dir(app.path & "\settings.dat", vbnormal) <> "" then
Open App.Path & "\settings.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, name
cmbNames(i).Text = name
i = i + 1
Loop
Close #1
End If
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
If Dir(App.Path & "\settings.dat", vbNormal) <> "" Then Open App.Path & "\settings.dat" For Input As #1 cmbNames.Clear Do While Not EOF(1) Line Input #1, Name cmbNames.AddItem (Name) i = i + 1 Loop Close #1 End If
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows






