Link to home
Start Free TrialLog in
Avatar of at_the_biginning
at_the_biginningFlag for United States of America

asked on

No Drive Calculator

Hello everyone,

Ive been trying to create this calculator from this site: www.wisdombay.com:80/hidedrive
To a Visual Basic project, but i got to a point where i get confuse in the number order,
can someone help me?

Thanks

i wonder if someone can help me to do it like the one in the page above.
This is what i have so far, and works ok, but just with some desperfections.
If Check1.Value = 1 Then 'A
Label1.Caption = "1"
End If
 
If Check2.Value = 1 Then 'B
Label1.Caption = "2"
End If
 
If Check3.Value = 1 Then 'C
Label1.Caption = "4"
End If
 
If Check4.Value = 1 Then 'D
Label1.Caption = "8"
End If
 
If Check5.Value = 1 Then 'E
Label1.Caption = "16"
End If
 
If Check6.Value = 1 Then 'F
Label1.Caption = "32"
End If
 
If Check7.Value = 1 Then 'G
Label1.Caption = "64"
End If
 
If Check8.Value = 1 Then 'H
Label1.Caption = "128"
End If
 
If Check9.Value = 1 Then 'I
Label1.Caption = "256"
End If
 
If Check10.Value = 1 Then 'j
Label1.Caption = "512"
End If
 
If Check11.Value = 1 Then 'k
Label1.Caption = "1024"
End If
 
If Check12.Value = 1 Then 'L
Label1.Caption = "2048"
End If
 
'And so on...

Open in new window

Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

If you are using traditional VB (1 to 6) it is easier to have an array of checkboxes.

Put the first checkbox (with caption "A") on the form and set its index to 0 in the Properties pane.
Option Explicit
 
Private Sub Form_Load()
    Dim r As Integer
    Dim c As Integer
    Dim i As Integer
    Dim sngHorizontal As Single
    Dim sngVertical As Single
    sngHorizontal = 800
    sngVertical = 400
    
    For i = 1 To 25
        Load Check1(i)
        r = i \ 5
        c = i Mod 5
        Check1(i).Left = Check1(0).Left + c * sngHorizontal
        Check1(i).Top = Check1(0).Top + r * sngVertical
        Check1(i).Visible = True
        Check1(i).Caption = Chr$(Asc("A") + i)
        Debug.Print i, c, r, Check1(i).Caption
    Next i
End Sub
Private Sub Command1_Click()
    Dim n As Long
    Dim i As Integer
    For i = 0 To 25
        If Check1(i).Value Then
            n = n + 2 ^ (i + 1)
        End If
    Next i
    Label1.Caption = n
End Sub

Open in new window

Avatar of at_the_biginning

ASKER


Hello GrahamSkan,

Good, but i am not getting the same results i am getting from my calculator neither the original.
What i need is to get the exact calculation like the one in the page, and with all the alphabet.

i considered mine to be ok, the only problem i have is that when you click on a letter closer to Y, Z or X,
i don't get a good calculation, it works but only if i check one by one, for example, in the original calculator
you can check mark on F, K, U, R, etc, like a mix of check boxes and it will give you the right calculation.

but the one i made, it give me the right calculation but one by one, if i do what i just explain to you, i get
an unknown calculaion, and i don't want.

Lets take a look at your example code...
if i check and click on the letter F, i get the result of 64 when im supposed to get 32.


Try changing line 28 from

            n = n + 2 ^ (i + 1)
to
            n = n + 2 ^ I

Excellent, perfect :-)
But can you please tell me how can i get to show the rest of the check boxes?
Right now i am getting only 6, and they astart in this orders: Check1, F, K, P, U, Z.

There is a total of 26 Check Boxes.

Is there enough space to see them on the right of the first column? Also. make sure that the whole checkbox and its caption are not so wide that the boxes of one columns are covering each other. The Width property in my test is 495.

Sorry for the delay,

Well the way i made my first project is exactly like the one in the page.
i wonder if i can make your example the same i did with my first project.
Something like this example:

A            B            C            D            E      
F            G            H            I            J      
K            L            M            N            O      
P            Q            R            S            T      
U            V            W            X            Y      
Z


I have deleted my test VB project now, but that layout corresponds to what it looked like to me. If you are getting anything different then I suggest that you attach your form file here, and we will try to see where your form is different.
I don't know if a .frm file is accepted at the moment, but if it isn't, change the extension to say, .txt, and tell us.

Thanks GrahamSkan,

Here is my example.

Note: this project doesn't have nothing from your example, this is the project i was working on.
but you'll everything in place, like all the check boxes and so on, so check it out, thanks Graham.


Form1.txt

Sorry GrahamSkan, is that sometimes i have problem with my typing or writing,
I write things upside down, and thats what i just did, so i hope you understand me.

Thanks

Hello again, GrahamSkan,

Could you please tell me the comclusion of this question?
I would like to hear from you again, i need more suggestions from you on this question.

Thanks
Easy way:


    <Flags()> _
    Public Enum NoDrive
        A = &H1
        B = &H2
        C = &H4
        D = &H8
        E = &H10
        F = &H20
        G = &H40
        H = &H80
        I = &H100
        J = &H200
        K = &H400
        L = &H800
        M = &H1000
        N = &H2000
        O = &H4000
        P = &H8000
        Q = &H10000
        R = &H20000
        S = &H40000
        T = &H80000
        U = &H100000
        V = &H200000
        W = &H400000
        X = &H800000
        Y = &H1000000
        Z = &H2000000
    End Enum
 
    Dim value As Integer = CInt(NoDrive.A Or NoDrive.B Or NoDrive.C Or NoDrive.D Or NoDrive.E)   ' 31

Open in new window


Hi TheLearnedOne,

This is not VB6 is it?
it looks more like VB.NET

Thanks
Same tune, different note:


    Enum NoDrive
        A = &H1
        B = &H2
        C = &H4
        D = &H8
        E = &H10
        F = &H20
        G = &H40
        H = &H80
        I = &H100
        J = &H200
        K = &H400
        L = &H800
        M = &H1000
        N = &H2000
        O = &H4000
        P = &H8000
        Q = &H10000
        R = &H20000
        S = &H40000
        T = &H80000
        U = &H100000
        V = &H200000
        W = &H400000
        X = &H800000
        Y = &H1000000
        Z = &H2000000
    End Enum
 
    Dim value As Integer 
    value = A Or B Or C Or D Or E   ' 31 

Open in new window

For combinations of drives, just add the values together.  In this case, that will work as well as the OR operation that TheLearnedOne used.  
Most people slept through the DP 101 class where they taught about binary values and how to combine them and use them as boolean flags.

Hello guys,

But.. can any of you tell me how to accomplish this code onto my project?
The way GrahamSkan had it was almost fine, Check boxes and the buttom.
except for the rest of the Check boxes.

Build a CheckBox control array, and set the Tag property to the enum.  Loop through the controls, and get the enum value from the Tag property, and sum the checked values (as Dan Rollins has suggested).

I am not sking for the whole work to be done, but atleast an example on how to use this code.
thats all.

Never seen this code, and don't know how to deal with it. so can you explain how to use it?

Thanks
1) The code that you have created doesn't use control arrays.

2) GrahamSkan showed you how to create control arrays:

     Load Check1(i)

    You can also do this with the designer, by copying and pasting the CheckBox control, and it asks you if you want to create a control array.

3) Looping through controls:

   Dim sum As Integer
   For i = 1 To 26
        If Check1(i).Value = 1 Then
           sum = sum + Check1(i).Tag
   Next i

Compile error: Variable not defined

     Dim sum As Integer
     For i = 1 To 26
     If Check1(i).Value = 1 Then
     sum = sum + Check1(i).Tag
     Next i
Coding in the comment block will do that!!

Dim i As Integer

For i = 1 To 26 will only work if you have a control array that starts at 1.  If it starts at 0 that would be For i = 0 to 25.

Ok, look, i tried this with my old project, and i also with GrahamSkan example,
and it doesn't work with none of them.

And i wonder whether TheLearnedOne code actually works or not,
Because if i copy and paste TheLearnedOne code onto my project,
Immediately i get an error pointing to this part of the code:
value = A Or B Or c Or D Or E <======

I liked the way GrahamSkan did it, simple and easy.

Thanks


"For i = 1 To 26 will only work if you have a control array that starts at 1.
If it starts at 0 that would be For i = 0 to 25."

I tried the other way, then i get this error:

Compile error: Wrong number of arguments or invalid property assignment.

I don't have VB6 anymore to test anything, so I hope you find a solution to your problem.

You left me half way... Thanks anyway.
i just wanted to say that i knew that code don't work.
Yeah the "Enum NoDrive, A = &H, B = &H2" and so on.
I expected an explanation on how the code works, but is ok.

Thanks anyway.
I don't have VB eieither and I don't have the time to explain control arrays.  However, using the code you provided, you can make this work.

Total=0
 
If Check1.Value = 1 Then 'A
    Total= Total+1
End If
If Check2.Value = 1 Then 'B
    Total= Total+2
End If
If Check3.Value = 1 Then 'C
    Total= Total+4
End If
If Check4.Value = 1 Then 'D
    Total= Total+8
End If
If Check5.Value = 1 Then 'E
    Total= Total+16
End If
If Check6.Value = 1 Then 'F
    Total= Total+32
End If
If Check7.Value = 1 Then 'G
    Total= Total+64
End If
. ... etc...
 
Label1.Caption = Total

Open in new window


I am trying to get out of my example, not to get in to it.

I would like someone to rebuild GrahamSkan project example,
that will be the only hope in this case.

Thanks
Sorry; been off-line for multiple reasons. Here is a form with the code that I suggested.
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form1"
   ClientHeight    =   3255
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   3075
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3255
   ScaleWidth      =   3075
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   120
      TabIndex        =   1
      Top             =   2520
      Width           =   2775
   End
   Begin VB.CheckBox Check1 
      Caption         =   "A"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   495
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "Label1"
      Height          =   255
      Left            =   720
      TabIndex        =   2
      Top             =   2040
      Width           =   2175
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
 
Private Sub Form_Load()
    Dim r As Integer
    Dim c As Integer
    Dim i As Integer
    Dim sngHorizontal As Single
    Dim sngVertical As Single
    sngHorizontal = 800
    sngVertical = 400
    
    For i = 1 To 25
        Load Check1(i)
        r = i \ 5
        c = i Mod 5
        Check1(i).Left = Check1(0).Left + c * sngHorizontal
        Check1(i).Top = Check1(0).Top + r * sngVertical
        Check1(i).Visible = True
        Check1(i).Caption = Chr$(Asc("A") + i)
        Debug.Print i, c, r, Check1(i).Caption
    Next i
End Sub
Private Sub Command1_Click()
    Dim n As Long
    Dim i As Integer
    For i = 0 To 25
        If Check1(i).Value Then
            n = n + 2 ^ (i + 1)
        End If
    Next i
    Label1.Caption = n
End Sub

Open in new window


Hey hello GrahamSkan!, is good to see you again :-)

Like always the code works like a charm, but only one thing is that the letter E is missing
and the code start counting from 2 and on, instead of 1. for example if you check mark the
letter A and press the buttom you get 2 when you suppose to get 1, then the rest of the code
it keep generating like these: 2, 4, 6, 8, and so on, etc. could this be because of the missing E?

I tried to fix the control array element but i could not, as i don't know that much about array elements.

This is the order of the respective calculation for each letter:

A = 1
B = 2
C = 4
D = 8
E = 16
F = 32
G = 64
H = 128
I  = 256
J = 512
K = 1024
L = 2048
M = 4096
N = 8192
O = 16384
P = 32768
Q = 65536
R = 131072
S = 262144
T = 524288
U = 1048576
V = 2097152
W = 4194304
X = 8388608
Y = 16777216
Z = 33554432


Thank you again GrahamSkan



Oh! No! ...My Bad! letter E is not missing!
after resizing the form i found letter E :-)
Now the only thing is the giving number.

Letter A has been excluded, the calculation start from B and on, and is not given the right calculation.

Sorry GrahamSkan
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Hey GrahamSkan, I knew i can count on you :-)
The project is working perfect with no problems.
Thank you for all your help.