Solved

Having problems with buttons controls with vb.net

Posted on 2006-11-02
1
368 Views
Last Modified: 2012-05-05
I'm making a hangman game. For some reason I'm able to disable buttons after their clicked if their in the word. If the button clicked isn't in the word I want to be able to set its property to invisible.....here's the code....



Public Class Form1
    Inherits System.Windows.Forms.Form


    Dim strWord As String


 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strWord = InputBox("Please enter a word for the user to guess.")
        txtWord.Text = underscore(strWord.Length, "")
        MessageBox.Show("The word is " & CStr(strWord.Length) + " letters long.")

End sub

 Private Function underscore(ByVal wordlength As Integer, ByVal undscore As String) As String

        For wordlength = 0 To wordlength - 1
            underscore += "_ "
        Next

        Return underscore


    End Function

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQ.Click, btnP.Click, btnO.Click, btnN.Click, btnM.Click, btnL.Click, btnK.Click, btnJ.Click, btnI.Click, btnH.Click, btnG.Click, btnA.Click, btnB.Click, btnC.Click, btnD.Click, btnE.Click, btnF.Click, btnR.Click, btnS.Click, btnT.Click, btnU.Click, btnV.Click, btnW.Click, btnX.Click, btnY.Click, btnZ.Click

        Dim btn As Button = CType(sender, Button)
        Dim indx As Integer
        Dim i As Integer






        If Not btn.Text = "" Then
            indx = GroupBox1.Controls.IndexOf(btn)
            For i = 0 To strWord.Length - 1

                If strWord.Substring(i, 1) = btn.Text Then
                    strWord.Replace("_"c, btn.Text)

                    txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text & txtWord.Text.Substring(i + 1).Replace("_"c, " "c)





                 



                    If btn.Enabled = True Then
                        btn.Enabled = False
                    Else
                        btn.Visible = False




                    End If


                End If


            Next i
        End If

    End Sub

End Class


0
Comment
Question by:sethRnda
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 17858848
Make a boolean flag and toggle it if you find a match.  Then when you're all done act on that flag:

        Dim found As Boolean = False
        Dim btn As Button = CType(sender, Button)
        Dim indx As Integer
        Dim i As Integer

        If Not btn.Text = "" Then
            indx = GroupBox1.Controls.IndexOf(btn)  ' <------ where are you using this?
            For i = 0 To strWord.Length - 1
                If strWord.Substring(i, 1) = btn.Text Then
                    found = True
                    strWord.Replace("_"c, btn.Text)     ' <------ this DOESN'T do anything!
                    txtWord.Text = txtWord.Text.Substring(0, i) & btn.Text & txtWord.Text.Substring(i + 1).Replace("_"c, " "c)      
                End If
            Next i
            If found Then
                btn.Enabled = False
            Else
                btn.Visible = False
            End If
        End If
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now