Solved

Having problems with buttons controls with vb.net

Posted on 2006-11-02
1
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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