Answer Needed A.S.A.P Listbox
hey guys, new hre and have a question. im inthe middle of my programming coursework for Uni and i have a dilema.
It is a phone simulator.
When i enter a number and click Keep it ads it into a file which is then read by a listbox adn shown on screen.
what they want me to be able to do is use a speed dial button so i just enter the position of the number in the listbox and it shows it.
here is what they ask
Speed-dial a number. Clicking a speed-dial button should prompt the user to enter the position number of an entry in the list box. This number should be automatically placed into the Number box of the call area. (Example: entering 2 puts 0114328512 in the number area - recall that the first entry in a list box has the position number of 0 )
please answer asap
thanks
[840 byte] By [
Dingotar] at [2007-11-20 0:40:59]

# 1 Re: Answer Needed A.S.A.P Listbox
Hello & welcome to the Forums Dingotar! :wave:
Will this help
Option Explicit
Private ListIndex As Integer 'variable to hold position enetered in textbox
Private Sub Command1_Click()
Select Case ListIndex 'what number was entered
Case 0 '0 was entered
MsgBox List1.List(ListIndex) 'show the corresponding list item
Case 1 '1 was entered
MsgBox List1.List(ListIndex)'show the corresponding list item
End Select 'just continue in the same fashion...
End Sub
Private Sub Text1_LostFocus()
ListIndex = CInt(Text1.Text) 'store & convert number entered to be used again later
End Sub
It's not perfect, but maybe one of the geniuses here on this forum can optimise it further:)
# 2 Re: Answer Needed A.S.A.P Listbox
what will that peice of code do if i use it?
# 3 Re: Answer Needed A.S.A.P Listbox
First set the list to the item you need.
I would put a Textbox (txtSpeedNum)for the speed number beneath the speed dial button (btnSpeedDial).
Then you enter the number in the text field and click the button
Assuming the list box is named lstNumbers
Private Sub btnSpeedDial_Click
lstNumbers.ListIndex = Val(txtSpeedNum.text) - 1
txtPhoneNumber.Text = lstNumbers.List(lstNumbers.ListIndex)
End Sub
That's all you need.
WoF at 2007-11-9 19:58:53 >

# 4 Re: Answer Needed A.S.A.P Listbox
what will that peice of code do if i use it?
Odd question. Did you try either one of the examples provided in this thread
Is this perhaps homework
# 5 Re: Answer Needed A.S.A.P Listbox
i will try the examples thanks
# 6 Re: Answer Needed A.S.A.P Listbox
i tried the example by WoF and i changed it so the listbox (LstNumbers) was changed to mine which is named list1 and i changed all the others and when i enter a number into the list box it doesnt show anything in the Number field
any thoughts?
also how do u mean by set the list to the item u need?
# 7 Re: Answer Needed A.S.A.P Listbox
I slammed together a little sample which should make it clear.
I populate the list with some dummy phonenumbers during Form_Load().
Also I made the list itself clickable so the number you click appears in the txtPhoneNumber, just to show.
WoF at 2007-11-9 20:03:00 >

# 8 Re: Answer Needed A.S.A.P Listbox
thanks for that, that is exactly what i want it to do, i will fiddle with it for a bit and get back to you
# 9 Re: Answer Needed A.S.A.P Listbox
thanks alot it works :D YEY!
one lil question to finish it off.
i store my numbers in the listbox with a name and number
if i jsut key in a number into the phone and press call it enters the listbox called "unamed - (whatever number)
i also have the choice of entering a name a number seperatly with 2 textboxes. so i can type in Matt and 445 and it will show in the listbox as Matt - 445
when i use the listbox technique shown by WoF it enters both the name and number in to the Call field, is there any way to seperate the name a number from each other and just show the number when i press sped dial.
thanks again u have been a great help
# 10 Re: Answer Needed A.S.A.P Listbox
To split off the number you should make shure that the number is always separated with a blank dash blank (" - ") combination.
Then you can change the dial click to separate the number like this:
Private Sub btnDial_Click()
Dim i%, p%
i = Val(txtQuick.Text) - 1
If i > -1 And i < lstNumbers.ListCount Then
lstNumbers.ListIndex = i
'see where the " - " is
p = InStrRev(lstNumbers.List(i), " - ")
If p Then
'take only the number starting there
txtPhoneNumber.Text = Mid$(lstNumbers.List(i), p + 3)
Else
'take everything because there is no " - "
txtPhoneNumber.Text = lstNumbers.List(i)
End If
End If
End Sub
WoF at 2007-11-9 20:06:00 >

# 11 Re: Answer Needed A.S.A.P Listbox
thanks mate this works a treat :D
# 12 Re: Answer Needed A.S.A.P Listbox
as a quick addon, i have a modify button whch means i can change the things already in the listbox. i highlight the one i need to modify and i click the Modify button, using WoF's code above i can seperate the number from the text but i can only get it to show the number, what small adjustment do i need to do to seperate and show both in seperate text boxes
at the moment i have this code to seperate and show the number in a text box
Private Sub Command16_Click()
Dim i%, p%
i = Val(List1.ListIndex)
If i > -1 And i < List1.ListCount Then
List1.ListIndex = i
'see where the " - " is
p = InStrRev(List1.List(i), " - ")
If p Then
'take only the number starting there
Text3.Text = Mid$(List1.List(i), p + 3)
Else
'take everything because there is no " - "
Text3.Text = List1.List(i)
End If
End If
# 13 Re: Answer Needed A.S.A.P Listbox
You got p which is the position of the " - " in the string.
Assuming Text4 to be your TextBox for the name, simply Text4.Text = Left$(List1.List(i), p - 1)
To avoid a crash when there is nothing before the " - " you could put this behind an If statement:
If p > 1 then Text4.Text = Left$(List1.List(i), p - 1)
I forgot: this goes under the 'If p Then' statement
WoF at 2007-11-9 20:09:09 >

# 14 Re: Answer Needed A.S.A.P Listbox
thanks again WoF great help as always.
using this method when i click on a number and change it, it ads another contact to the list with the name a number i give it. leaving the original behind
is there anyway to delete the old one when replacing it with the new?
this will by last question. promise hehe
# 15 Re: Answer Needed A.S.A.P Listbox
Any more questions are welcome. :)
You can put the new one in the same place as the old one.
I assume List1.Listindex is still pointing to the item you want to change.
I further assume Text3 your phonenumber and Text4 your name.
So simply reassign them to the old list item:
List1.List(List1.Listindex) = Text4.Text + " - " + Text3.Text
WoF at 2007-11-9 20:11:07 >

# 16 Re: Answer Needed A.S.A.P Listbox
where abouts in this code should i put that code WoF
i keep trying but cant seem to find where
Private Sub Command14_Click()
filenum = FreeFile
Open App.Path & "NumList.txt" For Append As filenum
Print #1, Text2.Text & " - " & Text3.Text 'This enters the values in the "Name" and "Number" textboxes into the file
Close #1
List1.Clear
Open App.Path & "NumList.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, listinfo
List1.AddItem listinfo
Loop
Close #1
End If
End Sub
also text2 is my name and text 3 is my number
# 17 Re: Answer Needed A.S.A.P Listbox
Hm. I see what you are doing here...
You append the new item to the file to have it also saved and then reread the file into the listbox.
You could replace the new item in the list and then rewrite the file like this:
Private Sub Command14_Click()
'replace the old item with the new one
List1.List(List1.Listindex) = Text2.Text + " - " + Text3.Text
'rewrite the complete listbox to the file
filenum = FreeFile
'please note the backslash in the statement below
Open App.Path & "\NumList.txt" For Output As filenum
Dim i%
For i=0 to List1.ListCount - 1
Print #filenum, List1.List(i)
Next
Close #filenum
End Sub
If I may give some advice:
If you are using FreeFile to get a filenum you should also use this filenum in open, print, input and close statements like in above sample:
Print #filenum, "whatever".
Also please note, when saving and loading your file in the App.Path you have to specify the backslash to separate your filename from the path.
Your sample may not produce an error, but you wont find the file "NumList.txt" in your application path. You should go through your program and correct the usage of App.Path everywhere you used it to acces your file and add the "\" before your filename.
WoF at 2007-11-9 20:13:14 >

# 18 Re: Answer Needed A.S.A.P Listbox
thanks for the advice, its printed and on its way, thanks for your help