Setting an object defaults.

Reply

Join Date: Jun 2005
Posts: 85
Reputation: williamrojas78 is an unknown quantity at this point 
Solved Threads: 4
williamrojas78's Avatar
williamrojas78 williamrojas78 is offline Offline
Junior Poster in Training

Setting an object defaults.

 
0
  #1
Jun 30th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Setting an object defaults.

 
0
  #2
Jun 30th, 2005
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....
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 85
Reputation: williamrojas78 is an unknown quantity at this point 
Solved Threads: 4
williamrojas78's Avatar
williamrojas78 williamrojas78 is offline Offline
Junior Poster in Training

Re: Setting an object defaults.

 
0
  #3
Jul 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Setting an object defaults.

 
0
  #4
Jul 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 18
Reputation: Lalo1985 is an unknown quantity at this point 
Solved Threads: 0
Lalo1985's Avatar
Lalo1985 Lalo1985 is offline Offline
Newbie Poster

Re: Setting an object defaults.

 
0
  #5
Jul 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Setting an object defaults.

 
0
  #6
Jul 2nd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 85
Reputation: williamrojas78 is an unknown quantity at this point 
Solved Threads: 4
williamrojas78's Avatar
williamrojas78 williamrojas78 is offline Offline
Junior Poster in Training

Re: Setting an object defaults.

 
0
  #7
Jul 5th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Setting an object defaults.

 
0
  #8
Jul 7th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 85
Reputation: williamrojas78 is an unknown quantity at this point 
Solved Threads: 4
williamrojas78's Avatar
williamrojas78 williamrojas78 is offline Offline
Junior Poster in Training

Re: Setting an object defaults.

 
0
  #9
Jul 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 5
Reputation: cookware_ok is an unknown quantity at this point 
Solved Threads: 0
cookware_ok's Avatar
cookware_ok cookware_ok is offline Offline
Newbie Poster

Re: Setting an object defaults.

 
0
  #10
Jul 11th, 2005
Hi;

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
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC