|
-
Nov 16th, 1999, 11:20 PM
#3
It looks like you're doing some things you don't need to and making it more complicated for yourself in the long run.
Here's my interpretation of what you're trying to achieve, I've commented pretty much everything,
so you should be able to figure out what's going on fairly easily..
On a Form with an MSFlexGrid (grdMovieSelect), 3 Command Buttons (cmdAdd, cmdDelete & cmdClose), a Combobox (cboMovies) and a Textbox (txtTotal)..
Code:
'Movie File Name/Path
Private Const sMoviedata = "C:\Movies.txt"
'Movie File Type
Private Type tMovieFile
sName As String 'Movie Name
dPrice As Double 'Movie Price ($)
End Type
'Array of Movie File Types
Private atMoviesFiles() As tMovieFile
Private Sub Form_Load()
'Initialize the Combobox list of Movies
Dim sTemp As String
Dim iCount As Integer
Dim iFreeFile As Integer
Dim iInstr As Integer
txtTotal.Enabled = False
txtTotal.Alignment = vbRightJustify
txtTotal = "$0.00"
'Setup the Flexgrid
With grdMovieSelect
'Need to setup the Header,
'can't have a Fixed Row with less than 2 Rows
.Rows = 2
.Cols = 2 'Col1 = Movie Name, Col2 = Movie Price
'Set the Column widths
.ColWidth(0) = .Width - 1500
.ColWidth(1) = 1400
'Right Justify the Price Column
.ColAlignment(1) = flexAlignRightCenter
'Make the Heading Row Fixed
.FixedRows = 1
'Set the Headers
.TextMatrix(0, 0) = "Movie Title"
.TextMatrix(0, 1) = "Price ($)"
'Now we can set the Rows to 1
.Rows = 1
'Turn off the Highlight until something is in the Grid
.HighLight = flexHighlightNever
.SelectionMode = flexSelectionByRow
End With
'Get the Next available File Number
iFreeFile = FreeFile
'Open the Data File to load in the Movies and Prices
Open sMoviedata For Input As iFreeFile
Do
'Increase the Array Size
ReDim Preserve atMoviesFiles(iCount)
Input #iFreeFile, sTemp
iInstr = InStr(sTemp, "|")
'Split the Input into Movie Name and Price
atMoviesFiles(iCount).sName = Left(sTemp, iInstr - 1)
atMoviesFiles(iCount).dPrice = Right(sTemp, Len(sTemp) - (iInstr))
'Add the Movie to the Combo
cboMovies.AddItem atMoviesFiles(iCount).sName
'Store the Array Index in the Combo ItemData as a Cross Reference
cboMovies.ItemData(cboMovies.NewIndex) = iCount
iCount = iCount + 1
Loop Until EOF(iFreeFile)
Close iFreeFile
'If Moves got loaded, select the first one in the list ready.
If cboMovies.ListCount Then cboMovies.ListIndex = 0
End Sub
Private Sub cmdAdd_Click()
Dim dTemp As Double
'Only add a Movie if one is selected..
If cboMovies.ListIndex < 0 Then Exit Sub
With grdMovieSelect
'Get the Movie Price from the MovieFiles Array
dTemp = atMoviesFiles(cboMovies.ItemData(cboMovies.ListIndex)).dPrice
'Add the Selection to the Grid
.AddItem cboMovies & Chr(9) & Format(dTemp, "currency")
'Enable the Highlight
.HighLight = flexHighlightAlways
'Make sure the Entire Row is Selected
.ColSel = .Cols - 1
End With
'Adjust the Total of all Movies Selected
txtTotal = Format(Val(Mid(txtTotal, 2)) + dTemp, "currency")
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdDelete_Click()
With grdMovieSelect
'Only Remove an Item if there is one..
If .Rows < 2 Then Exit Sub
'Adjust the Total to Exclude the selected Movie Price
txtTotal = Format(Val(Mid(txtTotal, 2)) - Val(Mid(.TextMatrix(.Row, 1), 2)), "currency")
'If this is the Last Item Just Set the Rows to 1
If .Rows = 2 Then
.Rows = 1
Else
'Otherwise Remove the Selected Row
.RemoveItem .Row
End If
'Turn the highligh off if there are no more Items
If .Rows = 1 Then .HighLight = flexHighlightNever
End With
End Sub
------------------
Aaron Young
Analyst Programmer
[email protected]
[email protected]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|