borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (listbox question--2nd try)Next Topic (for next problem...) New Topic New Poll Post Reply
AndreaVB Forum : VB General : how to reuse listbox
Poster Message
jeff
Level: Protégé

Registered: 09-05-2006
Posts: 7

icon how to reuse listbox

   I am trying to make two programs.  The first one I developed through trial and error and it seems to work except for the if, then statement which is supposed to  limit the choices in the listbox.  In the second program I want to ask the user to choose new sets of values in the same listbox. I don't know how to reuse the listbox to obtain the new values.  

   Believe it or not, I have spent a lot of time trying to research and find local people to help me so I would REALLY appreciate it if someone would be willing to help me.  I am a 40 year old return student in Hotel Management and started this project expecting to be able to use 10,20,30 etc  to make a BASIC program.

Program 1:

Dim occi, rati, occ1, rat1, newocc, newrate, cost, occmax
Private Sub CommandButton1_Click()
    ListBox1.Clear
    ' ThisDocument.Content.InsertAfter ("B.H. New Run")
    ' ThisDocument.Content.InsertParagraphAfter
     rati = TextBox1.Text
     occi = TextBox2.Text
     rat1 = TextBox3.Text
     occ1 = TextBox7.Text
     cost = TextBox6.Text
     occmax = TextBox8.Text
    
    
    For x = cost To rati
    
        y = -occi / rati * (x - rat1) + occ1
        x = Round(x, 0)
        y = Round(y, 0)
        
      
            ResultLine = Str$(x) + "    dollars for occ =    " + Str$(y)
             If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
             End If
    Next x
    
    ' m = -y / x
    ' y = m * (x - 2)

End Sub


Private Sub ListBox1_Click()
    Cancel = False
    MsgBox (ListBox1.Text)
    newrate = Left(ListBox1.Text, 5)
    newocc = Right(ListBox1.Text, 5)
    
ListBox1.Clear
For x = cost To rati
y = -newocc / newrate * (x - newrate) + newocc
  y = Round(y, 0)
  
        
            ResultLine = Str$(x) + "    dollars for occ =   " + Str$(y)
            If y <= occmax And y > 0
            Then ListBox1.AddItem (ResultLine)
            End If
          
        
        
        'ResultLine = Str$(x) + " dollars for occ = " + Str$(y)
       ' ListBox2.AddItem (ResultLine)
        ' ThisDocument.Content.InsertAfter (ResultLine)
        ' ThisDocument.Content.InsertParagraphAfter
Next x

Label4.Caption = "     This is your strategic pricing schedule"
End Sub


-----------------------------------------------------------------------
Program 2:
Dim occi, rati, occ1, rat1, newocc, newrate, cost, occmax
Private Sub CommandButton1_Click()
    ListBox1.Clear
    ' ThisDocument.Content.InsertAfter ("B.H. New Run")
    ' ThisDocument.Content.InsertParagraphAfter
     rati = TextBox1.Text
     occi = TextBox2.Text
     rat1 = TextBox3.Text
     occ1 = TextBox7.Text
     cost = TextBox6.Text
     occmax = TextBox8.Text
    
    
    For x = cost To rati
    
        y = -occi / rati * (x - rat1) + occ1
        x = Round(x, 0)
        y = Round(y, 0)
        
      
            ResultLine = Str$(x) + "    dollars for occ =    " + Str$(y)
             If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
             End If
            
        ' ThisDocument.Content.InsertAfter (ResultLine)
        ' ThisDocument.Content.InsertParagraphAfter
    Next x
    
    ' m = -y / x
    ' y = m * (x - 2)

End Sub


Private Sub listbox1_click()
    Cancel = False

    newrate = Left(ListBox1.Text, 5)
    newocc = Right(ListBox1.Text, 5)
    newrat1 = newrate + newrate * 0.1
    newocc1 = newocc + newocc * 0.1
ListBox1.Clear
For x = cost To rati
y = -occi / rati * (x - newrat1) + newocc1
  y = Round(y, 0)
  
        
            ResultLine = Str$(x) + "    dollars for occ =   " + Str$(y)
            If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
            End If
        
        
        'ResultLine = Str$(x) + " dollars for occ = " + Str$(y)
       ' ListBox2.AddItem (ResultLine)
        ' ThisDocument.Content.InsertAfter (ResultLine)
        ' ThisDocument.Content.InsertParagraphAfter
Next x

Label4.Caption = "     Choose for more favorable conditions     "

   MsgBox (ListBox1.Text)
    newrat0 = Left(ListBox1.Text, 5)
    newocc0 = Right(ListBox1.Text, 5)
  
    newrat0 = newrate - newrate * 0.1
    newocc0 = newocc - newocc * 0.1
ListBox1.Clear
    For x = cost To rati
y = -occi / rati * (x - newrat0) + newocc0
  y = Round(y, 0)
  
            
            ResultLine = Str$(x) + "    dollars for occ =   " + Str$(y)
            If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
            End If
        
        
        'ResultLine = Str$(x) + " dollars for occ = " + Str$(y)
       ' ListBox2.AddItem (ResultLine)
        ' ThisDocument.Content.InsertAfter (ResultLine)
        ' ThisDocument.Content.InsertParagraphAfter
Next x

Label4.Caption = "     Choose for less favorable conditions     "

End Sub

09-05-2006 at 10:25 AM
View Profile Send Email to User Show All Posts | Quote Reply
jeff
Level: Protégé

Registered: 09-05-2006
Posts: 7
icon Re: how to reuse listbox

Did someone take a look at my code yet? I forgot to delete the 'this document. . . documentation-- I don't need it.

  If someone can just send me a simple example of how to recall listbox1_click() in order to obtain new values this would be sufficient for now.

Thanks,
Jeff

10-05-2006 at 08:19 AM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: how to reuse listbox

Hello, Jeff.
Your code suffers of an 'old-programmer's style' sickness. I don't know if it's related to your main question, but you should correct some bad techniques.

quote:
Dim occi, rati, occ1, rat1, newocc, newrate, cost, occmax
You better always specify the type of your variables, in order to avoid VB's intrinsic casting. If you don't specify a var type, you force VB to determine it by itself when the var is recalled, with a loss of time and a not-always-satisfying conversion. Sometimes we should specify the type even for constants. If you don't believe it, give the following code a run:
Private Sub Command1_Click()
    Dim a As Long
    a = 3600 * 10        ' This row generates an overflow error
    a = 36000        ' This row works fine
End Sub
We can get rid of the error if we explicitly specify the 3600 as a Long value, either by a Const x = 3600 As Long instruction, or a 3600& usage into the code.
quote:
            ResultLine = Str$(x) + "    dollars for occ =   " + Str$(y)
In order to concatenate two strings you should use the '&' operator instead of the '+'. The '+' operator is an old reminescence of VB4. It's still supported, but nowadays it can create some misworking with SQL insructions for ADO objects.
quote:
             If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
             End If
If you write the If instruction in one single line, I mean when you write the assignment after the Then keyword on its same line, you don't have to give the End If instruction. Read here for more details.

And of course you know it's not a good technique to give non-significant names to variables and controls, such as Text1, Text2, ...TextN, but I think you didn't brute-paste your code here, just wrote down it to show us the working mechanism, did you?

Now, your main question: I didn't understand what you are meaning by "reuse the listbox". The two controls are two different controls, and they're placed into two separated, different applications. You simply can't use any control from another program.
You can create a control as an ocx, and include an instance of it into Program1 and another instance into Program2, but they'll be two different instances, and you can't use the same instance in Prog1 and Prog2. In fact, a listbox control (or any other VB controls) is nothing but an instance of VB's ListBox Control.
You can either include the whole Program1 into Program2 as an ocx, and then use its objects in Program2.
Or you can make they chat, and share data, and you can make one to pass values to the other. There are many ways to implement these things.
But if you need to use a control equal to another control - not the same one - you must design both, and fill each one at its own run time.
So, what do you really mean? Are you intended to create another listbox like the first one, and containing the same values?

Please be more specific, and let us know. I'll hang around here.

PS. Oh, I read your post again now: are you using some MSOffice VBA, or VB6 or VB.NET, or what?

[Edited by yronium on 10-05-2006 at 09:40 AM GMT]

____________________________
Real Programmer can count up to 1024 on his fingers
10-05-2006 at 08:31 AM
View Profile Send Email to User Show All Posts | Quote Reply
jeff
Level: Protégé

Registered: 09-05-2006
Posts: 7
icon Re: how to reuse listbox

OK,
Thanks.  

  Firstly, I had two lines and end if and one line and end if, but for some reason, nothing seems to limit the listbox1.additem resultline for y<occmax.

  Second, I don't know what brute-paste is.  I just saw other people posted their codes and an expert fixed it up for them which I thought was VERY NICE and was hoping someone would do it for me.  

  The first program and second program don't need to chat and program 1 works.  In program 2, I want the user to be able to make multiple choices under different scenerios [more favorable, (newrate=oldrate+10%) and less favorable (oldrate - 10%)].  I want to hold these values to create the final pricing schedule.   I was hoping to use the same listbox or at least erase the old listbox and put a new listbox in its place on the userform.

   Besides what you mentioned, I assume this program would be simplified by creating one function and sending new variables  to the same equation,
    ie. y = -occi / rati * (x - rat1) + occ1 for rat1=newrate. I need a simple example of this to work from.

I am using the VB editor on Word because it is the only compiler I happen to own.  

Thanks again for your help.

Jeff

10-05-2006 at 09:23 AM
View Profile Send Email to User Show All Posts | Quote Reply
jeff
Level: Protégé

Registered: 09-05-2006
Posts: 7
icon Re: how to reuse listbox

I cleaned this up a bit--

Dim occi, rati, occ1, rat1, newocc, newrate, cost, occmax
Private Sub CommandButton1_Click()
    ListBox1.Clear
     rati = TextBox1.Text
     occi = TextBox2.Text
     rat1 = TextBox3.Text
     occ1 = TextBox7.Text
     cost = TextBox6.Text
     occmax = TextBox8.Text
    
    
    For x = cost To rati
    
        y = -occi / rati * (x - rat1) + occ1
        x = Round(x, 0)
        y = Round(y, 0)
        
      
            ResultLine = Str$(x) & "    dollars for occ =    " + Str$(y)
             'this still doesn't work
             If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
            
    Next x
End Sub


Private Sub listbox1_click()
    Cancel = False
   MsgBox (ListBox1.Text)
    newrate = Left(ListBox1.Text, 5)
    newocc = Right(ListBox1.Text, 5)
    newrat1 = newrate + newrate * 0.1
    newocc1 = newocc + newocc * 0.1

For x = cost To rati
y = -occi / rati * (x - newrat1) + newocc1
  y = Round(y, 0)
  
        
            ResultLine = Str$(x) & "    dollars for occ =   " + Str$(y)
            If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
            
Next x

  
    
Label4.Caption = "     Choose for more favorable conditions     "

   'I am confused here for getting new input -- maybe a new    sub or function?
    
    MsgBox (ListBox1.Text)
    newrat1 = Left(ListBox1.Text, 5)
    newocc1 = Right(ListBox1.Text, 5)
  
    newrat0 = newrate - newrate * 0.1
    newocc0 = newocc - newocc * 0.1

For x = cost To rati
y = -occi / rati * (x - newrat0) + newocc0
  y = Round(y, 0)
  
        
            ResultLine = Str$(x) & "    dollars for occ =   " + Str$(y)
            If y <= occmax And y > 0 Then ListBox1.AddItem (ResultLine)
Next x

Label4.Caption = "     Choose for less favorable conditions     "
    
    'Also here I want somehow to get a new input
    'I don't think the MsgBox is the right way to go here
    'Rather I need to somehow send newrat0 and newocc0 to
    'the original equation.

    MsgBox (ListBox1.Text)
    newrat0 = Left(ListBox1.Text, 5)
    newocc0 = Right(ListBox1.Text, 5)

End Sub

'Here I want to use six veriables newrate,newocc,newrat0,newocc0,newrat1,newocc1
'and put a pricing schedule based on a for,next loop on an equation using
'these variables

10-05-2006 at 12:08 PM
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: how to reuse listbox

Dear jeff, I am sorry but I still don't understand the logic of your code, but maybe there's some more part that you didn't post. Anyway...

Brute-paste: when someone just copies/pastes some code, as it is. I think you didn't copy/paste your code, but typed it again here in the topic textbox, isn't it? So, when rewriting some code, it could happen some typing mistake.


quote:
   'I am confused here for getting new input -- maybe a new sub or function?

[....]

    'Also here I want somehow to get a new input
    'I don't think the MsgBox is the right way to go here
    'Rather I need to somehow send newrat0 and newocc0 to
    'the original equation.
Maybe you can be interested in the InputBox function. It returns a String value, that you can convert by the conversion functions, like CLng, CDbl, etc.
Its usage is simple:
    Dim returnstring As String, mynum As Long
    ' prompts the user for a value, the argument is the prompt text
    returnstring = InputBox("Insert here the new value you need to search for")
    ' convert user's input in a number
    mynum = CLng(returnstring)

And of course, a separate function can do the job. You should build it, starting by the calculation formula you posted, and then assign the return value to a variable.
Keep in mind:
- a function has a return value, so you can assign it to a variable. When declaring the function, you got to declare the return value type, say
    Private Function ComputeIt() As Double
- a function need some arguments to work. Arguments are those value to pass time by time to the function. They have to be declared in the function declaration, too
    Private Function ComputeIt(FirstNum As Double, SecondNum As Double) As Double
So you can recall it in the code, like
    Dim x As Double, first As Double, second As Double
    x = ComputeIt(first, second)

Hope it helps

____________________________
Real Programmer can count up to 1024 on his fingers
11-05-2006 at 09:36 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : how to reuse listbox
Previous Topic (listbox question--2nd try)Next Topic (for next problem...) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder