9 Replies - 1994 Views - Last Post: 12 October 2014 - 11:23 AM Rate Topic: -----

#1 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Need A bit of help with my button code xD

Posted 11 October 2014 - 11:16 AM

Guys How can i make my Button not bound to any Textbox if i click it. Sorry for the silly question im a bit of a newbie when it comes to programming and im asked to make a 4 pics 1 word game on vb10..

I having problems figuring how to make the button Display Specific character on the textbox and not be bound to any of the other textbox. Like typing but using Buttons like in the game 4 pics 1 word.
Is This A Good Question/Topic? 0
  • +

Replies To: Need A bit of help with my button code xD

#2 thecoat   User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Need A bit of help with my button code xD

Posted 11 October 2014 - 11:34 AM

What type of project are you creating? The reason I ask is that buttons are not bound to anything if you just drop one on a winforms or wpf project. Perhaps you used a wizard of some sort, or created a lightswitch application that is somehow binding a button.

Anyway responding to a button click event is easy, and assigning text to a textbox is one line of code:

TextboxName.Text = "What you want to see in the text box"


Different types of projects have different methods of wiring up button event handling, so without knowing the project type it's kind of hard to be more detailed on that aspect, however if we assume you are doing a winforms project, you can double click your button in the designer and it should take you to the code page inside the button event handler.
Was This Post Helpful? 0
  • +
  • -

#3 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Re: Need A bit of help with my button code xD

Posted 11 October 2014 - 11:51 AM

Thanks for the reply Sir.

Im Trying to Create a game similar to the game called 4pics1word game on VB10.


Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click
        If TextBox1.Text = "D" Or TextBox2.Text = "D" Or TextBox3.Text = "D" Or TextBox4.Text = "D" Or TextBox5.Text = "D" Or                TextBox6.Text = "D" Or TextBox7.Text = "D" Or TextBox8.Text = "D" Then
            B1.Visible = False
        End If
    End Sub


That is the only code i come up with so far and what i need to do is that what if i clicked the "F" character button in a different textbox not in textbox1 how can i make it happen and also make the button invisible when the character is either typed or button clicked

Attached image(s)

  • Attached Image

This post has been edited by andrewsw: 11 October 2014 - 01:08 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 thecoat   User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Need A bit of help with my button code xD

Posted 11 October 2014 - 04:02 PM

So the key issue here deals with object focus. When the user clicks on a button or textbox that object on the form gets focus and any previously focused control loses it. So if the user clicks on the third textbox then clicks a letter button when the button click event fires the third textbox no longer has focus or any indicator that it is selected... so you have to build in a way to save the last selected textbox so the button knows which one to target for the text property assignment.


So first create a property to contain the textbox reference at the class level on the form:

Private _lastselectedtextbox As TextBox = TextBox1


Note in the declaration assigning it to the first Textbox is much like placing the focus there at the start.

Now you need to assign the textbox to that variable when it is selected (overwriting any previously selected textbox). I'll use the GotFocus event because this fires if the user clicks on it or tabs to it etc.

You could do this for each textbox:

    Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
        _lastselectedtextbox = TextBox1
    End Sub


    Private Sub TextBox2_GotFocus(sender As Object, e As EventArgs) Handles TextBox2.GotFocus
        _lastselectedtextbox = TextBox1
    End Sub


However there is a better way, make the method generic for a textbox and add each textbox to the handles declaration:

    Private Sub TextBox_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus, TextBox4.GotFocus
        _lastselectedtextbox = DirectCast(sender, TextBox)
    End Sub


You can also do this at runtime with the addhandler command, and could even loop through a collection of textboxes (a collection you might maintain if you dynamically added textboxes at runtime).

Anyway now that you have a reference to the last selected textbox, you can do something similar with the buttons.

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click
        Dim mybutton As Button = DirectCast(sender, Button)
        _lastselectedtextbox.Text = mybutton.Text 
        mybutton.Visible = False
    End Sub


Note that part of the trick in making this method generic so it can handle all the button events is that instead of hardcoding "A" or "B" I used the button.text it's self.

One final issue, if the button clicked is the last focusable control on the form, when you make it invisible it will wrap around the tab index and focus to the first focusable control which might be the first textbox, the easiest way to fix this is in the button click method by saving a reference to the focused textbox before setting the button invisible and then setting it back after the fact:

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click
        Dim mybutton As Button = DirectCast(sender, Button)
        Dim tboxsave As TextBox = _lastselectedtextbox
        _lastselectedtextbox.Text = mybutton.Text
        mybutton.Visible = False
        _lastselectedtextbox = tboxsave
    End Sub

Was This Post Helpful? 1
  • +
  • -

#5 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 01:52 AM

Thank you very much! :) ill try it now :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 02:25 AM

I don't know what im doing wrong but its still rewriting textbox1 instead of transferring to the next textbox.. Here is the code so far please let me know what im doing wrong there or is there something missing.. I'll post picture of the output at runtime also :cry:



Public Class Image1
    Dim allowed As String = "CHILDRENSQ"
    Private _lastselectedtextbox As TextBox = TextBox1

    Private Sub B2Main_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2Main.Click
        GameMenu.Show()
        Me.Hide()
    End Sub
    Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus, TextBox4.GotFocus, TextBox5.GotFocus, TextBox6.GotFocus, TextBox7.GotFocus, TextBox8.GotFocus
        _lastselectedtextbox = DirectCast(sender, TextBox)

    End Sub

    Private Sub Image1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.CharacterCasing = CharacterCasing.Upper
        TextBox2.CharacterCasing = CharacterCasing.Upper
        TextBox3.CharacterCasing = CharacterCasing.Upper
        TextBox4.CharacterCasing = CharacterCasing.Upper
        TextBox5.CharacterCasing = CharacterCasing.Upper
        TextBox6.CharacterCasing = CharacterCasing.Upper
        TextBox7.CharacterCasing = CharacterCasing.Upper
        TextBox8.CharacterCasing = CharacterCasing.Upper
    End Sub
    
    Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles B1.Click, B2.Click, B3.Click, B4.Click, B5.Click, B6.Click, B7.Click, B8.Click, B9.Click, B10.Click

        Dim mybutton As Button = DirectCast(sender, Button)
        Dim tboxsave As TextBox = _lastselectedtextbox
        _lastselectedtextbox.Text = mybutton.Text
        mybutton.Visible = False
        _lastselectedtextbox = tboxsave
    End Sub

End Class


Attached Image
Attached Image
Was This Post Helpful? 0
  • +
  • -

#7 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1505
  • View blog
  • Posts: 3,810
  • Joined: 01-February 13

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 04:29 AM

Hi,
I think the biggest problem here is that nobody seems to have played this game to know how the game is suppose to work exactly. Maybe if you could explain in more detail how the game is played step by step and some of the rules of it, it would help get an answer. There are several ways this could be set up but, we may show you a way to do it that would not account for some rule that we did not know from the get go. Then you may have to start over to do it a different way to account for it.

For instance, after pressing a button and the 1st textbox is set to the letter of the button, is it suppose to focus on the next textbox in the row or focus back on the same textbox?

After the user presses a button or a few buttons and the letters are added to the first few textboxes, can the user go back and change one of the letters?

If they do then should it start setting letters from that textbox forward from that point on?

Is it a one shot deal where after they press a button and the text is set in the textbox they can not go back and change it?

I myself would say that you would be better off adding all your textboxes to an array and use an Integer variable that can be used to set or get the currently selected textbox. Of coarse i can see where using a variable of the currently selected textbox could also be used in conjunction to this for certain rules or gameplay that are needed.

Here is a simple example to make it focus on the next textbox after clicking a button and adding the text to the current textbox using an array. As i said though, there may wind up being things i don`t know about the game that would change the way things need to be set up.
Public Class Form1
    Dim allowed As String = "CHILDRENSQ"
    Private currentTbox As Integer = 0
    Private Tboxs() As TextBox

    Private Sub B2Main_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2Main.Click
        GameMenu.Show()
        Me.Hide()
    End Sub

    Private Sub Image1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'add all the textboxes to an array of TextBox
        Tboxs = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8}

        'iterate through each textbox in the array and set its CharacterCasing
        For Each tb As TextBox In Tboxs
            tb.CharacterCasing = CharacterCasing.Upper
        Next
    End Sub

    Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles B1.Click, B2.Click, B3.Click, B4.Click, B5.Click, B6.Click, B7.Click, B8.Click, B9.Click, B10.Click
        If Not currentTbox > Tboxs.Length - 1 Then
            Dim mybutton As Button = DirectCast(sender, Button) 'cast the sender to a Button

            mybutton.Visible = False 'set the button to non visible

            Tboxs(currentTbox).Text = mybutton.Text 'set the current textbox text to the buttons text

            currentTbox += 1 'advance the currentTbox number by 1

            If Not currentTbox > Tboxs.Length - 1 Then 'check to make sure the currentTbox is not greater than the number of textboxes in the array
                Tboxs(currentTbox).Select() 'if its not, then select the textbox
            End If
        End If
    End Sub
End Class


Was This Post Helpful? 1
  • +
  • -

#8 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 05:06 AM

4 pics 1 word is a guessing game. You will be shown a screen with four pictures. These four pictures each have something in common and you need to guess what that is. Below the pictures, you will see blank spaces indicating how many letters are in the answer. Below that, there will be a scrambled keyboard of possible letters you can select from when typing in your guess for the answer. You need to click on these letters to spell out the word you believe to be the answer. When you answer the question correctly, you can progress to the next level.

Quote

For instance, after pressing a button and the 1st textbox is set to the letter of the button, is it suppose to focus on the next textbox in the row or focus back on the same textbox?


It's supposed to focus on the next textbox in the row after the the 1st textbox is set to the button you first clicked.

Quote

After the user presses a button or a few buttons and the letters are added to the first few textboxes, can the user go back and change one of the letters?


In the original game, one can just click on the letter which you want to change/delete. Deleting this letter means bringing back the button representing the letter.

Quote

If they do then should it start setting letters from that textbox forward from that point on?


Yes

If all of the textbox have been filled with the appropriate amount of letters and it is the wrong answer, the buttons will reset leaving all of the textbox empty. But if the answer is correct, the game will move on to the next set of pictures.
Was This Post Helpful? 0
  • +
  • -

#9 YuZhenXie1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-October 14

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 10:06 AM

:bananaman: I figured out the code the only thing remaining to me to do is

Quote

In the original game, one can just click on the letter which you want to change/delete. Deleting this letter means bringing back the button representing the letter.


And


Quote

If all of the textbox have been filled with the appropriate amount of letters and it is the wrong answer, the buttons will reset leaving all of the textbox empty. But if the answer is correct, the game will move on to the next set of pictures.


Hope that someone so i can do the finishing touches to the program.. Thanks in advance!!

Also Thank you so much! IronRazer and thecoat for giving some advice and code for my program. :)
Was This Post Helpful? 0
  • +
  • -

#10 thecoat   User is offline

  • D.I.C Addict

Reputation: 153
  • View blog
  • Posts: 537
  • Joined: 07-December 13

Re: Need A bit of help with my button code xD

Posted 12 October 2014 - 11:23 AM

IronRazer already suggested collections and you will benefit from those in a big way. A collection of your letter buttons is also going to be very useful.

Quote

In the original game, one can just click on the letter which you want to change/delete. Deleting this letter means bringing back the button representing the letter.


In the button click event set the tag property of the target textbox to the button, then when the textbox is clicked you can check the tag property and make the button visible. I think you mentioned keyboard input earlier. That's going to introduce two issues to overcome if you want that as well. A collection of your letter buttons will go a long way toward helping you find the appropriate button to act on, especially considering you may have buttons with the same letter and you have to pick which one you want to act on.

Quote

If all of the textbox have been filled with the appropriate amount of letters and it is the wrong answer, the buttons will reset leaving all of the textbox empty. But if the answer is correct, the game will move on to the next set of pictures.


Next set of pictures and words to guess. This will likely introduce a lot of changes to what you have so far. For instance if not every word is the same length, you may need to display more or less text boxes. The Handles keyword isn't going to work if you start generating buttons and textboxes at runtime, for that you will have to use AddHandler. You've also not told us where you are getting the content and how you are loading it in, I've been under the assumption that you were basically doing a mock and put this content in at design time.

Anyway how decide when you do the word match check will somewhat be determined by what you let the user do. If you allow the user to touch/click on textboxes out of order then you need to check for when they are all complete after each time you've assigned a letter. If you have a linear cycle of textboxes that the user can not interrupt then you'd only need to do the check after the last check box is assigned.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1