Link to home
Start Free TrialLog in
Avatar of nw_khaemba
nw_khaemba

asked on

Crossword puzzle creation tool in vb

I have been trying to implement a crossword puzzle creation tool using vb.Having come up with a grid on the picture box how can I write a code that will detect the event of clicking onto a specific number of cell on the picture box
Avatar of robertlees
robertlees

The MouseUp event of the picture box will give you the X and Y position that was clicked. You can then work out which square that is by dividing these numbers by the number of squares across and down.

But do you really need to do it with a PictureBox ? Why not create an array of labels or textboxes. That way, each square is an individual entity. You can make the BackColor black or white as required. If you use textboxes, the user can type a letter directly into that square - you would have to ensure that if there is already a letter there it would be replaced by the new letter.

Let me know if you need more help.
Hi

Just go here

http://www.a1vbcode.com

and in the search box (Located on top right corner) enter crossword and click search.

You will find a code.

Enjoy

Cheers

Narayanan
Hi.

I once tried it like you, with a picturebox, and it is possible. But you'll need a lot of code then and the only advantages are that you can easily print it and that you can save it as a BMP file.
Using a control array of textboxes, as robertlees already suggested, produces much shorter and much easier code.

Try this simple but functional example and after, tell me if you're still convinced using a picturebox::
*create a form with a checkbox (for choosing typing direction) and a small square textbox (the upper left cell of the crossword).
For the textbox, set Alignment to center, Appearance to flat, and Index to 0.
*paste this code:
Dim Rows As Integer
Dim Cols As Integer

Private Sub MoveRight(Index As Integer)
If Index < Rows * Cols - 1 Then
    Text1(Index + 1).SetFocus
Else
    Beep
End If
End Sub

Private Sub MoveLeft(Index As Integer)
If Index > 0 Then
    Text1(Index - 1).SetFocus
Else
    Beep
End If
End Sub

Private Sub MoveDown(Index As Integer)
If Index + Cols < Rows * Cols Then
    Text1(Index + Cols).SetFocus
Else
    If Index = Rows * Cols - 1 Then
        Beep
    Else
        Text1(Index + Cols - Rows * Cols + 1).SetFocus
    End If
End If
End Sub

Private Sub MoveUp(Index As Integer)
If Index - Cols >= 0 Then
    Text1(Index - Cols).SetFocus
Else
    If Index = 0 Then
        Beep
    Else
        Text1(Index - Cols + Rows * Cols - 1).SetFocus
    End If
End If
End Sub

Private Sub Form_Load()
Dim i As Integer
Rows = 20
Cols = 20
Me.ScaleMode = vbPixels
Check1.Caption = "Move down after typing"
Text1(0).MaxLength = 1
For i = 1 To Rows * Cols - 1
    Load Text1(i)
    Text1(i).Left = Text1(0).Left + (i Mod Cols) * (Text1(0).Width - 1)
    Text1(i).Top = Text1(0).Top + (i \ Cols) * (Text1(0).Height - 1)
    Text1(i).Visible = True
Next i
End Sub

Private Sub Text1_Change(Index As Integer)
'Move right or down after typing a character:
If Len(Text1(Index)) = 1 Then
    If Check1 = 0 Then
        MoveRight Index
    Else
        MoveDown Index
    End If
End If

'Use space to create black cell:
If Text1(Index) = " " Then
    Text1(Index).BackColor = vbBlack
Else
    Text1(Index).BackColor = vbWhite
End If
End Sub

Private Sub Text1_GotFocus(Index As Integer)
'auto-select
Text1(Index).SelStart = 0: Text1(Index).SelLength = 1
End Sub

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
'"UCase" every character
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub

Private Sub Text1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
'Make arrow keys work:
Select Case KeyCode
    Case vbKeyLeft: MoveLeft Index
    Case vbKeyRight: MoveRight Index
    Case vbKeyUp: MoveUp Index
    Case vbKeyDown: MoveDown Index
End Select
End Sub

If you need help on how to print the crossword or how to save it, just ask here.
Avatar of nw_khaemba

ASKER

VBtom I think thats an excellent answer for extra points what code can set the dark area and prevent the cursor from venturing there instead of doing it at run time say I give indicate the cells to be darkened and it does it
ASKER CERTIFIED SOLUTION
Avatar of VBtom
VBtom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Vb tom thank you very much and may God bless you. This is a sure thing and I don't know what to say
Thanks.
This was funstuff, I liked it.
Glad I could help you.