?
Solved

Having problems with buttons controls with vb.net

Posted on 2006-11-02
1
Medium Priority
?
410 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 86

Accepted Solution

by:
Mike Tomlinson earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Loops Section Overview
Screencast - Getting to Know the Pipeline

840 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