Solved

Having problems with buttons controls with vb.net

Posted on 2006-11-02
1
374 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 113
Fibonacci challenge 11 109
allStar challenge 1 52
Adding Namespace to the second node of the xml 16 64
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

910 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

23 Experts available now in Live!

Get 1:1 Help Now