Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Confessions of a terrible programmer

Am trying to transfer selections from List1 to List2. Why won't this work:
Private Sub Form_Load()
With Form1.List1
.AddItem "One"
.AddItem "Two"
.AddItem "Three"
End With

End Sub

Private Sub List2_Click()
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected = True Then
List2.AddItem List1.List
End If
Next
End Sub
[471 byte] By [wisgro] at [2007-11-11 8:15:54]
# 1 Re: Confessions of a terrible programmer
Try this:

Private Sub Form_Click()
Dim I As Integer
For I = 0 To List1.ListCount - 1
If List1.Selected(I) Then
List2.AddItem List1.List(I)
End If
Next
End Sub
Phil Weber at 2007-11-11 17:25:41 >
# 2 Re: Confessions of a terrible programmer
Thanks for responding. This code works perfect with a command button but I can't get it to work without a button. I'm trying a simple program but running into walls at every turn. I consult my books. All examples use a button after user selection which turns out to be a superfluous step but I can't get the blessed stuff to run without a button. Here's what you sent,

Private Sub Form_Click()
Dim i as integer
For i = 0 To List1.ListCount - 1
If List1.Selected(i) Then
List2.AddItem List1.List(i)
End If
Next
End Sub

Private Sub Form_Load()
Dim i As Integer
With Form1.List1
.AddItem "One"
.AddItem "Two"
.AddItem "Three"
End With


End Sub
wisgro at 2007-11-11 17:26:41 >
# 3 Re: Confessions of a terrible programmer
VB is "event-driven." That means most code is triggered by an event, such as a key press or mouse click. What event do you want to trigger the moving of selected items from List1 to List2?
Phil Weber at 2007-11-11 17:27:48 >
# 4 Re: Confessions of a terrible programmer
I hoped to transfer items when they are selected. I tried click, mouse down, activate, initialze in the form and click, and mouse down in list2. I selected everything on the list that seemed to apply... got focus, etc. and nothing worked. Am I to conclude that the only way to transfer selections from one list to another is with a button?
wisgro at 2007-11-11 17:28:42 >
# 5 Re: Confessions of a terrible programmer
How does the user select an item in List1? Try putting the code in List1_Click. ;-)
Phil Weber at 2007-11-11 17:29:51 >
# 6 Re: Confessions of a terrible programmer
Phil... here's the module code that I hoped would initiate the carriage return in the text box.
Walt

Option Explicit
'***************************************************************
'ModProcessRequest.Bas March 5, 2006
'***************************************************************
'
'
'Function receives choice from lstAppliance_Click event then
'returns average monthly operating cost for selected appliance
'and displays it in the txtAvgMoCost Text Box.
'
'Initialize newLine variable that will use the
'ASCII value or vbCrLf for carriage return and line feed

Public Function GetInfo(Choice As Long) As String

Dim newLine As String
newLine = Chr(13) + Chr(10) 'or vbCrLf (doesn't work with either)

Select Case Choice

'Ran debug. After 1st selection runs succesfully
'program skips remaining cases and stops at lstAppliance_Click()


Case 0
GetInfo = Format$(2.1, "currency") & newLine
Case 1
GetInfo = Format$(3.25, "currency") & newLine
Case 2
GetInfo = Format$(5.98, "currency") & newLine

End Select

End Function
wisgro at 2007-11-11 17:30:50 >
# 7 Re: Confessions of a terrible programmer
OK, and how are you using the value returned by this function?
Phil Weber at 2007-11-11 17:31:45 >
# 8 Re: Confessions of a terrible programmer
Here's the rest of the code:
Option Explicit
'****************************************************************************
'Walt Isgro, wisgro@me.acadia.net
'Purpose:
'1. A user selects items from a list of appliances then uses a button to
' transfers the items to a second list.
'2. When the appliances are transfered an average monthly cost appears in a
' text box.
'3. Eventually a running total of appliance costs will accumulate in a second
' text box located below the first.
'
'Variable names:
'txtAvgMoCost is the average monthly operating cost of an appliance
'selected by a user.
'lstAppliance is the list of appliance selections
'lstSelectedAppliances displays appliances selected by a user
'
'PROBLEM: Module modProcess Request doesn't execute a carriage return after
' each selection is made.
'Ran debug, step into. After making 1st selection the program runs succesfully
' but program skips the remaining cases and stops at lstAppliance_
' Click()
'****************************************************************************

Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdMovetoLeft_Click()
Dim i As Integer
If lstSelectedAppliances.ListIndex = -1 Then Exit Sub
For i = lstSelectedAppliances.ListCount - 1 To 0 Step -1
If lstSelectedAppliances.Selected(i) = True Then
lstAppliance.AddItem lstSelectedAppliances.List(i)
lstSelectedAppliances.RemoveItem i
End If
Next i
End Sub

Private Sub cmdMovetoRight_Click()
Dim i As Integer
If lstAppliance.ListIndex = -1 Then Exit Sub
For i = lstAppliance.ListCount - 1 To 0 Step -1
If lstAppliance.Selected(i) = True Then
lstSelectedAppliances.AddItem lstAppliance.List(i)
lstAppliance.RemoveItem i
End If
Next i

End Sub

Private Sub Form_Load()
With frmMultiSelectListBoxes.lstAppliance

'abbreviated list for test
.AddItem "100 Watt Bulb"
.AddItem "60 Watt Bulb"
.AddItem "Fan"

End With
End Sub

Private Sub lstAppliance_Click()
'txtAvgMoCost.Text = ""
'txtAvgMoCost.SetFocus

'Select Case for list test
Select Case lstAppliance.ListIndex
Case 0
txtAvgMoCost.Text = GetInfo(0)
Case 1
txtAvgMoCost.Text = GetInfo(1)
Case 2
txtAvgMoCost.Text = GetInfo(2)

'Case...
End Select

End Sub

Private Sub lstSelectedAppliances_Click()
'intCounter is usually named i
Dim i As Integer
If lstAppliance.ListIndex = -1 Then Exit Sub
For i = lstAppliance.ListCount - 1 To 0 Step -1
If lstAppliance.Selected(i) = True Then
lstSelectedAppliances.AddItem lstAppliance.List(i)
'lstAppliance.RemoveItem i
End If
Next i

End Sub

Private Sub txtAvgMoCost_Change()
Dim rs
Dim Choice As Long

'Identify item chosen in lstAppliance
Choice = frmMultiSelectListBoxes.lstAppliance.ListIndex

'Call the function GetInfo and get the average monthly cost
'that corresponds to the selected item

rs = GetInfo(Choice)

'Display average cost in txtAvgMoCost text box.

frmMultiSelectListBoxes.txtAvgMoCost.Text = rs

End Sub

Private Sub txtTotalAvg_Change()

End Sub
wisgro at 2007-11-11 17:32:48 >
# 9 Re: Confessions of a terrible programmer
I discovered that the CHR(13) and CHR(10) in the function didn't have the $ sign. I changed it but the program still won't work right. I replaced the ascii with vbCrLf and that doesn't work either. I've analyzed this to the point that I am not able to see the omission or obstruction that doesn't allow the program to run.
wisgro at 2007-11-11 17:33:56 >