IF Not ( Either / OR ) Question

mellencolie
06-01-2005, 11:11 AM
Experts,

Problem: I have a form of width size A that needs to expand to accomodate width of a listbox on the right to some width value...

I have a command button that needs to be clicked and then it should expand out the form from its initial size A to end result size B, thus resulting in the listbox being shown fully. Then, I need the same button to toggle back and move the form.width back to its original setting.

I was trying to accomplish this with a IF Not statement. Does anybody have any strong suggestions for how to use one button to accomplish this?

Here's what I have (obviously incorrect.. am I on the right track?):

Private Sub cmdListToggle_Click()

If Not frmUserinfo.Width = 3330 Then

frmUserinfo.Width = 8550

End If


End Sub

grahamt
06-01-2005, 11:43 AM
Private Sub cmdListToggle_Click()

If Not frmUserinfo.Width = 3330 Then

frmUserinfo.Width = 8550

End If


End Sub


Try this...

Private Sub cmdListToggle_Click()

If frmUserinfo.Width = 3330 Then

frmUserinfo.Width = 8550
Else
frmUserinfo.Width = 3330
End If


End Sub

Bolek
06-01-2005, 11:45 AM
Have you heard about static variables? These are variables local to functions but they do not lose their values between subsequent calls. For example, this code changes the color of the dialog back and forth between grey and blue


Private Sub Command1_Click()
Static b As Boolean
If (b) Then
Form1.BackColor = &HC0C0C0
Else
Form1.BackColor = &HFF0000
End If
b = Not b
End Sub

HoseHead78
06-01-2005, 11:46 AM
Why not just use an If/Else statement.


If frmUserinfo.Width = 3330 Then
' If True then the form will be changed to the other width
frmUserinfo.Width = 8550
Exit Sub
Else
' If False then it is changed back to the regular width
frmUserinfo.Width = 3330
End If

Bolek
06-01-2005, 11:48 AM
Why not just use an If/Else statement.

If frmUserinfo.Width = 3330 Then
' If True then the form will be changed to the other width
frmUserinfo.Width = 8550
Exit Sub
Else
' If False then it is changed back to the regular width
frmUserinfo.Width = 3330
End If

Because it is not as foolproof as using static variables. It is prone to errors when you decide to change the width from 3330 to 3331 and forget to change the number somewhere. Also, what happens if the user resizes the dialog meanwhile?

mellencolie
06-01-2005, 11:48 AM
Have you heard about static variables? These are variables local to functions but they do not lose their values between subsequent calls. For example, this code changes the color of the dialog back and forth between grey and blue


Private Sub Command1_Click()
Static b As Boolean
If (b) Then
Form1.BackColor = &HC0C0C0
Else
Form1.BackColor = &HFF0000
End If
b = Not b
End Sub


Good point, I forgot about those.. So basically, the line, b = Not b is what toggles the form value "like a light switch...."

HoseHead78
06-01-2005, 11:50 AM
True... I stand corrected
:)

HardCode
06-01-2005, 12:17 PM
You are better to use Constants to represent the width values. Bolek has the theory correct, but the wrong implement:


Private Const WIDTH_A As Long = 3330
Private Const WIDTH_B As Long = 8550

mellencolie
06-01-2005, 01:05 PM
You are better to use Constants to represent the width values. Bolek has the theory correct, but the wrong implement:


Private Const WIDTH_A As Long = 3330
Private Const WIDTH_B As Long = 8550


Thanks for the clarification. So, based on that, would i reference Form.width to = WIDTH_A (ie. form.width = Width_A) ?? Bolek's idea seemed logical..

Maybe I could do this logically..:

Form.width = (width_a or width_b), based on the toggle.

So...

Select Case frmUserinfo.Width

Case frmUserinfo.Width = WIDTH_A
frmUserinfo.Width = WIDTH_B

Case frmUserinfo.Width = WIDTH_B
frmUserinfo.Width = WIDTH_A
End Select

Bolek
06-01-2005, 02:13 PM
You are better to use Constants to represent the width values. Bolek has the theory correct, but the wrong implement:


What's wrong with my implement? I don't see any disadvantage. Using constants for the width in addition to static variables seems logical since it will clarify the thing, but I don't see anything wrong with my implement.

Bolek
06-01-2005, 02:16 PM
Oh, the best solution seems to combine them


Private Const WIDTH_A As Long = 3330
Private Const WIDTH_B As Long = 8550

Private Sub Command1_Click()
Static b As Boolean
If (b) Then
Form1.width = WIDTH_A
Else
Form1.Width = WIDTH_B
End If
b = Not b
End Sub


This is perfectly clear, foolproof even in the case of user resizing the dialog, and it uses named constants so that any changes can appear at just one place. Is it what you wanted, HardCode?

mcgilles
06-01-2005, 02:44 PM
why not make the initial size a variable with module scope, then set it equal to the form's width on startup. that way you can change the size of you form at design time without changing your code.

then when you make it bigger, why not use the combo1.width property so it will size perfectly? or if the combo is not right up against the left size, set the form width to combo1.left + combo1.width?

then use the same type of switching descibed above, this just makes it much more flexible.

HardCode
06-01-2005, 04:29 PM
Oh, the best solution seems to combine them ...
Is it what you wanted, HardCode?

Yup! :) That's the best way to do it. Avoids "magic numbers".

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum