Você está na página 1de 4

Visual Basic: Hidding Buttons, users input, gettype http://en.allexperts.com/q/Visual-Basic-1048/Hidding-Buttons-1.

htm

AllExperts > Visual Basic

Search

Visual Basic
Volunteer
Answers to thousands of questions
Home · More Visual Basic Questions · Answer Library · Encyclopedia ·
Sponsored Links

PowerBasic vs VisualBasic C# to VB Converter Code Signing Certificates


Faster. No Run-Times. No Bloat! CGI, Macros, Most accurate C# to VB converter. Free demo, Sign active content such as Java, Applets,
ASM, Reg Expressions support, & updates. Active X and Macros
www.powerbasic.com www.tangiblesoftwaresolutions.com www.thawte.com

More Visual Basic Advertisement


Ads by Google
Answers You are here: Experts >
Question Library Computing/Technology > Basic > Visual Basic
> Hidding Buttons PDF Writer for VB
Ask a question about Create, display, print, edit, merge Royalty-free
Visual Basic Visual Basic - Hidding Buttons distribution. Try now!
www.synactis.com
Volunteer
Expert: Sriraman CS - 4/16/2006
Experts of the Month
Expert Login Question
------------------------- Visual Basic Programming
Awards Followup To Free Online Discussion Boards for VB & Other
Question - Programming Languages.
VisualStudio.Net 2003 www.daniweb.com
About Us
Tell friends I'm trying to write a loop that will hide buttons
Link to Us depending on a number. I'll start with 23 buttons total and i'll retrive the number (a number between 1
Disclaimer and 3) each trial and remove as many buttons as the number.

My professor said in case we chose to use buttons (the hard way to do our project) we should think of
"redisplaying them everytime".

I really don't know where to start so any help would be greatly appreciated.
About Sriraman CS
Expertise
I am expertise in Visual
Thanks.
basic concepts, Office Answer -
Development, Visual Basic I don't really understand how do you choose which buttons to hide. And after you hide them, when are
for Applications (VBA) etc. I
have been using these
you unhiding them (if you're doing this)? Anyway, this will hide you some buttons:
technologies for the past
four years. You can ask any Dim k As Integer = 0
questions with regard to
Visual Basic and VBA.
For Each c As Control In Me.Controls
If k < numberOfControlsToHide Then
Experience c.Visible = False
Visual Basic 6.0, VBA
End If
k += 1
Next

Or

the following code assumes that you are providing a textbox for the user to specify how many buttons
they want to have removed. You can modify it for any other type of input you may choose to use:

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


btnClear.Click
'This assumes you have named your buttons Button1, Button2 and so on
'If you wish to make the buttons dissapear in non sequential order on the form
'then you would want to scatter the buttons on the form rather than placing
'then in numerical order
If IsNumeric(txtNumber.Text) Then
'Declare variable representing users input
Dim Num As Integer = CInt(txtNumber.Text)

1 of 4 6/10/2010 11:43 PM
Visual Basic: Hidding Buttons, users input, gettype http://en.allexperts.com/q/Visual-Basic-1048/Hidding-Buttons-1.htm

Dim counter As Integer


'While counter is equal or less than user input
Do While counter <= Num
'Iterate through the controls on the form
For Each ctrl As Control In Me.Controls
'Check for buttons only
If ctrl.GetType.ToString = "System.Windows.Forms.Button" Then
' If the button name is button plus the current counter
If DirectCast(ctrl, Button).Name = "Button" + counter.ToString Then
'If the button is visible then remove it from form
If DirectCast(ctrl, Button).Visible Then
'If its not visible then add 1 to Num to effectivly skip the button
'in the sequence
DirectCast(ctrl, Button).Visible = False
Else
Num = Num + 1
End If

End If
End If
Next
counter = counter + 1
Loop
'If the value remains blank, do nothing, otherwise the messagebox
'will execute if the user changes the value
ElseIf txtNumber.Text = "" Then
Exit Sub
'If the value is not numeric of blank then display a messagebox
Else
MessageBox.Show ("You must enter a numeric value")
End If
End Sub

Hope this will help. Please give some more details if you need more help.

Regards,

Sriraman CS

---------

i'm designing an application for the game of nim; and well the buttons are represent the object one would
take away at a time. i'm using a textbox to input the number of items to be removed. and i'm trying to
write the loop to hide the buttons as each player inputs a number between 1 and 3.
i'm looking at your code and it's starting to make sense (this is my first CS course so i'm still fairly new to
programing)...
i came up with this

Private Sub KittyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles KittyButton.Click
Dim Number As Integer
Number = NumberBox.Text
If Number = 9 Then
Button10.Hide()
ElseIf Number = 8 Then
Button10.Hide()
Button9.Hide()
ElseIf Number = 7 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
ElseIf Number = 6 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()

2 of 4 6/10/2010 11:43 PM
Visual Basic: Hidding Buttons, users input, gettype http://en.allexperts.com/q/Visual-Basic-1048/Hidding-Buttons-1.htm

Button7.Hide()
ElseIf Number = 5 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
ElseIf Number = 4 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
Button5.Hide()
ElseIf Number = 3 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
Button5.Hide()
Button4.Hide()
ElseIf Number = 2 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
Button5.Hide()
Button4.Hide()
Button3.Hide()
ElseIf Number = 1 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
Button5.Hide()
Button4.Hide()
Button3.Hide()
Button2.Hide()
ElseIf Number = 0 Then
Button10.Hide()
Button9.Hide()
Button8.Hide()
Button7.Hide()
Button6.Hide()
Button5.Hide()
Button4.Hide()
Button3.Hide()
Button2.Hide()
Button1.Hide()
End If
End Sub

it works but as you see is quite lengthy and i'm sure my professor would probably say "very inefficient".
i'll try to implement the code you gave me into my program see if i can reason it out. THANK YOU SO
MUCH! if you have any other suggestions i'll be glad to give them a read ;]

hugO

Answer
Hi,

3 of 4 6/10/2010 11:43 PM
Visual Basic: Hidding Buttons, users input, gettype http://en.allexperts.com/q/Visual-Basic-1048/Hidding-Buttons-1.htm

You need to create a control array of all your buttons. i.e., Button(0), Button(1), Button(2)...

The following code is not tested. Please check.

Private Sub KittyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


KittyButton.Click
Dim intCount As Integer
Select Case NumberBox.Text
Case 0 To 9
For intCount = NumberBox.Text + 1 To 10
Button(intcount).Hide()
Next intCount
End If
End Sub

I hope this will fulfill your problem.

Let me know if you need any assitance.

Regards,

Sriraman CS.

Add to this Answer Ask a Question

Related Articles
• Working With Printers
• Create a Time Out Button / Entry Form in Delphi
• Hiding a Panel - GNOME
• Inheriting Controls in VB.NET
• The blink Element - blinking text - Netscape Tags

User Agreement | Privacy Policy | Kids' Privacy Policy | Help


Copyright © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.

4 of 4 6/10/2010 11:43 PM

Você também pode gostar