944,044 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jun 30th, 2005
0

Setting an object defaults.

Expand Post »
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.
Reputation Points: 23
Solved Threads: 10
Junior Poster
williamrojas78 is offline Offline
111 posts
since Jun 2005
Jun 30th, 2005
0

Re: Setting an object defaults.

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....
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 1st, 2005
0

Re: Setting an object defaults.

Ok, I thought of doing it like that, but is there a specific or a recommended type of file to use? Can you give an example maybe? it would be greatly appreciated.
Reputation Points: 23
Solved Threads: 10
Junior Poster
williamrojas78 is offline Offline
111 posts
since Jun 2005
Jul 1st, 2005
0

Re: Setting an object defaults.

Just a plain text file.... something lto write the file ike:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. open app.path & "\settings.dat" for output as #1
  2. print #1, text1.text
  3. print #1, text2.text
  4. close #1

and read it like:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if dir(app.path & "\settings.dat", vbnormal) <> "" then
  2. open app.path & "\settings.dat" for input as #1
  3. line input #1, text1.text
  4. line input #1, text2.text
  5. close #1
  6. end if
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 1st, 2005
0

Re: Setting an object defaults.

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
Lalo1985 is offline Offline
18 posts
since Jun 2005
Jul 2nd, 2005
0

Re: Setting an object defaults.

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 5th, 2005
0

Re: Setting an object defaults.

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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. if dir(app.path & "\settings.dat", vbnormal) <> "" then
  2.  
  3. Open App.Path & "\settings.dat" For Input As #1
  4.  
  5. Do While Not EOF(1)
  6. Line Input #1, name
  7. cmbNames(i).Text = name
  8. i = i + 1
  9. Loop
  10.  
  11. Close #1
  12. End If


and for writing:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1.  
  2. Open App.Path & "\settings.dat" For Output As #1
  3.  
  4. Print #1, cmbNames(0).Text
  5. Print #1, cmbNames(1).Text
  6. Print #1, cmbNames(2).Text
  7.  
  8. Close #1

Thanks.
Reputation Points: 23
Solved Threads: 10
Junior Poster
williamrojas78 is offline Offline
111 posts
since Jun 2005
Jul 7th, 2005
0

Re: Setting an object defaults.

No. You are absolutely correct. I tried to do it that way also (skipping variables) and it gets pretty upset. I guess it's just the name of the game, and I appreciate your return response.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 8th, 2005
0

Re: Setting an object defaults.

About the response is no problem at all, I try to clarify the problem, so whoever has the same question also has the whole answer and not a part of it.

Thanks a lot for your support.
Reputation Points: 23
Solved Threads: 10
Junior Poster
williamrojas78 is offline Offline
111 posts
since Jun 2005
Jul 11th, 2005
0

Re: Setting an object defaults.

Hi;

You might try using .AddItem instead of .Text. By clearing the combo box first ( .Clear ) it should then refill the list.

Quote ...
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
Try this code:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. If Dir(App.Path & "\settings.dat", vbNormal) <> "" Then
  2. Open App.Path & "\settings.dat" For Input As #1
  3.  
  4. cmbNames.Clear
  5.  
  6. Do While Not EOF(1)
  7. Line Input #1, Name
  8. cmbNames.AddItem (Name)
  9. i = i + 1
  10. Loop
  11.  
  12. Close #1
  13.  
  14. End If
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cookware_ok is offline Offline
5 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: errors in my file but not sure whats wrong file attatched
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: vb newbie are dialogue boxes /buttons etc part of vb6 or are they drawn from windows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC