Hey guys, I am thinking about creating a sudoku game in VB in order to improve my programming skills and as I am quite new to VB I was wondering would it be best to try this task using labels and text boxes or a felxigrid?
Thanks in advance.
Hey guys, I am thinking about creating a sudoku game in VB in order to improve my programming skills and as I am quite new to VB I was wondering would it be best to try this task using labels and text boxes or a felxigrid?
Thanks in advance.
Short answer: make the cells a control array (VB6) or a single reusable Label handler (VB.NET) and either (A) let a Label click place the textbox value directly into that Label, or (B) let a Label click select the cell and use a separate "Set" button (or keyboard) to commit the number. That keeps your code small and gives you easy validation, highlighting, and "fixed clue" protection.
As suggested, control arrays are the easiest in VB6. Example — direct-click puts the textbox value into the clicked cell:
' VB6 - direct click places number from txtNumber into clicked cell
Private Sub Label1_Click(Index As Integer)
If Label1(Index).Tag = "fixed" Then Exit Sub ' protect starting clues
Dim n As Integer
n = Val(txtNumber.Text)
If n >= 1 And n <= 9 Then
Label1(Index).Caption = CStr(n)
Else
MsgBox "Enter a number 1-9"
End If
End Sub If you prefer select-then-commit (gives a visible selection and an undo point), use a selectedCell variable and a button:
Dim selectedCell As Integer
Private Sub Form_Load()
selectedCell = -1
txtNumber.MaxLength = 1
Me.KeyPreview = True
End Sub
Private Sub Label1_Click(Index As Integer)
If selectedCell <> -1 Then Label1(selectedCell).BorderStyle = 0
selectedCell = Index
Label1(selectedCell).BorderStyle = 1
End Sub
Private Sub cmdSet_Click()
If selectedCell = -1 Then Exit Sub
If Label1(selectedCell).Tag = "fixed" Then MsgBox "This is a fixed clue.": Exit Sub
Dim n As Integer: n = Val(txtNumber.Text)
If n >= 1 And n <= 9 Then Label1(selectedCell).Caption = CStr(n) Else MsgBox "Enter 1-9"
End Sub Extra tips: use Label.Tag = "fixed" for puzzle clues so players can't edit them; restrict txtNumber in KeyPress to digits only; set KeyPreview = True and handle Form_KeyPress so number keys fill the selected cell; consider PictureBox arrays if you want themed digits or images (as mentioned). If you are using VB.NET, create Label controls dynamically and wire a single Click handler with AddHandler, using Tag to store index — same concepts, slightly different syntax.
Jump to Post— Comatose 290I would actually go with a control array of picture box's. This way, you could use pictures in the box's to represent the numbers, and if you wanted to give different styles or themes, it would be a breeze. If that seems a bit too complicated, go with the labels...
Jump to Post— Comatose 290If you have the label's in a control array, it's easy enough to make a function in the click event of the label, and have it change the label to the number in the textbox.... You might consider checking out the attached project, which makes a textbox dragable, so that …
I would actually go with a control array of picture box's. This way, you could use pictures in the box's to represent the numbers, and if you wanted to give different styles or themes, it would be a breeze. If that seems a bit too complicated, go with the labels...
Thanks for your advice. :)
I have decided to go with labels and text boxes but I have come across one problem if the user wants to enter a number in the first label and to do this they enter it into a text box then click a command button how can I allow them to choose which label the number will go into?
If you have the label's in a control array, it's easy enough to make a function in the click event of the label, and have it change the label to the number in the textbox.... You might consider checking out the attached project, which makes a textbox dragable, so that you can drag the entered number to the label in question. Let me know if this helps any.
Thanks for the advice, the attached program was a good example of what can be done. I think I will now look into the click event like you suggested.
Thanks again.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.