VB.Net Hangman Game Multiple Rounds - vb.net

I need some help.
I'm currently creating a Hangman Game in VB.Net.
I'm at the stage where a random word is loaded in from a text file of 6 words, and you can click buttons to guess it. If you guess letters wrong, the frame is shown etc, if they're right, the letter shows in the word through labels.
The next bit, that I'm stuck on, is that multiple rounds are needed. I need there to be 3 turns of this hangman game, so, if you guess the word right you get 10 points, and if you fail, you get 0, And then the game resets with your points and you can play again in Turn 2. Then again in Turn 3, and after Turn 3 finishes, the High Score Form is loaded.

You could create a variable to hold the current round in your Module, at the beginning of each round increase it, and at the end of the round check the current round and make a if then logic.
Dim myRound as Integer = 0
And in the PlayForm in the constructor.
myRound += 1
Once the round is complete.
if myRound >= 3 Then
'open the score page
Else
'start the next round
End if

'In General Declarations:
Dim ButtonList As New List(Of Button) 'or Of Control if you have other types of controls
Dim HgmList As New List(Of PowerPacks.Shape)
Dim AnswerList As New List(Of Label)
'In PlayForm_Load:
With ButtonList
.Clear()
.Add(Me.BtnA)
.Add(Me.BtnB)
.Add(Me.BtnC)
'You get the idea
'Add all your buttons you want to re-enable to the list
End With
With HgmList
.Clear()
.Add(Me.SideFrameLine)
.Add(Me.TopFrameLine)
.Add(Me.CornerFrameLine)
'etc.
End With
With AnswerList
.Add(Me.FirstLetterLbl)
'etc. Just like the other two.
End With
'At the end of your `If Correct = False Then` Block:
Else 'Check for win after each correct guess
Dim Winner As Boolean = True
Dim CheckLetter As Label
For Each CheckLetter in AnswerList
If Not CheckLetter.Visible Then
Winner = False
Exit For
End If
Next
If Winner Then
NextRound(10)
End If
End If
'Somewhere inside your form code:
Private Sub NextRound(RoundScore As Integer)
UserScore += RoundScore
If TurnNumber = 3 Then 'Game Over
MsgBox("Game Over" & vbNewLine & "Score: " & UserScore)
Else 'This is the part you asked about: resetting the form
TurnNumber += 1
PlayForm_Load(Nothing, Nothing)
Dim ResetControl As Control
Dim ResetShape As PowerPacks.Shape
For Each ResetControl In ButtonList
ResetControl.Enabled = True
Next
For Each ResetShape In HgmList
ResetControl.Visible = False
Next
End If
End Sub
I only added the .Clear() to the list builders because you already have your code to get a new word in PlayForm_Load. If you move it (say to a new Sub called NewWord), you don't need .Clear, and you would call your new sub instead of PlayForm_Load.

Related

Remove specific row of TableLayoutPanel using dynamically created button of each row

So I am making a function that will populate the TableLayoutPanel from FileDialog Result then make a delete button for each row using a loop. Here's the code
Private PathtoFile1 As New List(Of String) 'this will contain all the selected file in the dialogwindow
Private rowLineDrawing As Integer = 0
Private selectedfilecountLineDrawing As Integer
Public Function AttachFileLineDrawing(TLP As TableLayoutPanel)
Dim dr = OpenFileDialog1.ShowDialog
If (dr = System.Windows.Forms.DialogResult.OK) Then
selectedfilecountLineDrawing = OpenFileDialog1.FileNames.Count
For Each FileName In OpenFileDialog1.FileNames
Try
Console.WriteLine(FileName.ToString)
PathtoFile1.Add(FileName.ToString)
Catch SecEx As Security.SecurityException
MessageBox.Show("Security error. Please contact your administrator for details.\n\n" &
"Error message: " & SecEx.Message & "\n\n" &
"Details (send to Support):\n\n" & SecEx.StackTrace)
Catch ex As Exception
'Could Not Load the image - probably permissions-related.
MessageBox.Show(("Cannot display the image: " & FileName.Substring(FileName.LastIndexOf("\"c)) &
". You may not have permission to read the file, or " + "it may be corrupt." _
& ControlChars.Lf & ControlChars.Lf & "Reported error: " & ex.Message))
End Try
Next
'MAKE SOMETHING HERE TO DISPLAY THE SELECTED ITEMS IN THE TABLELAYOUTPANEL OF THE SUBMIT PROGRESS
TLP.Controls.Clear()
TLP.RowCount = 0
rowLineDrawing = 0
For Each Path In PathtoFile1
Dim filepath As New Label
filepath.Text = Path
filepath.Width = Val(360)
'this button is for previewing the file
Dim btnPreview As New Button
AddHandler btnPreview.Click,
Sub(s As Object, e As EventArgs)
Dim btn = CType(s, Button)
MsgBox("This is Preview")
End Sub
'This button is for removing rows in the tablelayoutpanel
Dim btnRmv As New Button
Dim StringToIndex As String = Path 'THIS CATCHES EVERY PATH IN THE LOOP AND STORE IT TO THE VARIABLE WHICH THEN BE USED AS A COMPARABLE PARAMETER FOR THE INDEX SEARCH
Dim index = PathtoFile1.IndexOf(Path)
AddHandler btnRmv.Click,
Sub(s As Object, e As EventArgs)
Dim btn = CType(s, Button)
MsgBox(index)
PathtoFile1.RemoveAt(index) 'THIS LINE OF CODE REMOVE THE SPECIFIC ITEM IN THE LIST USING THE BTNRMV CLICK
'MAKE SOMETHING HERE TO REMOVE THE ROW IN THE TABLELAYOUTAPANEL
End Sub
TLP.SuspendLayout()
TLP.RowStyles.Add(New RowStyle(SizeType.Absolute, 20))
TLP.Controls.Add(filepath, 0, rowLineDrawing)
TLP.Controls.Add(btnPreview, 1, rowLineDrawing)
TLP.Controls.Add(btnRmv, 2, rowLineDrawing)
TLP.ResumeLayout()
rowLineDrawing -= -1
Next
End If
End Function
So I am trying to remove the row in the TableLayoutPanel together with the dynamic control. My approach is removing the selected item in the list and I achieved it properly but can't remove the row in the TableLayoutPanel. Any help is much appreciated!
EDIT
I have tried to use the provided module above but got this error
And got this error
Here is an extension method that will enable you to remove any row from a TableLayoutPanel by index:
Imports System.Runtime.CompilerServices
Public Module TableLayoutPanelExtensions
<Extension>
Public Sub RemoveRowAt(source As TableLayoutPanel, index As Integer)
If index >= source.RowCount Then
Throw New ArgumentOutOfRangeException(NameOf(index),
index,
"The row index must be less than the number of rows in the TableLayoutPanel control.")
End If
'Remove the controls in the specified row.
For columnIndex = 0 To source.ColumnCount - 1
Dim child = source.GetControlFromPosition(columnIndex, index)
If child IsNot Nothing Then
child.Dispose()
End If
Next
'Move controls below the specified row up.
For rowIndex = index + 1 To source.RowCount - 1
For columnIndex = 0 To source.ColumnCount - 1
Dim child = source.GetControlFromPosition(columnIndex, rowIndex)
If child IsNot Nothing Then
source.SetCellPosition(child, New TableLayoutPanelCellPosition(columnIndex, rowIndex - 1))
End If
Next
Next
'Remove the last row.
source.RowCount -= 1
End Sub
End Module
I tested that on 3 column by 4 row TableLayoutPanel containing a Label in each cell executing the following code twice:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TableLayoutPanel1.RemoveRowAt(1)
End Sub
The result was as expected, i.e. removing the second-from-top row each time. You may need to fiddle a bit more depending on what you want to happen row heights. I had the row heights set to equal percentages so the remaining rows grew proportionally to fill the space. If you want something different, you can add code accordingly. Note that you could create an almost identical method for removing columns.

LOOP PROBLEMS in VB.Net

I am creating a math tutorial app for kids in the primary level. And I am having a hard time in creating a loop that would generate 2 random numbers,then check if the answer is right/wrong for 5 times. also, the button would be disabled after clicking 5 times. The other things are clear to me except the idea of how to put it in a loop. can someone help me please? thanks! I tried using the FOR LOOP, but sadly, it would just loop 5 times but it would only check the answer 1 time.I need it to check 5 different answers.
For ctr As Integer = 1 To 5
Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point = point + 1
TextBox3.Focus()
Else
MsgBox("wrong")
MsgBox(sum)
TextBox3.Focus()
End If
Next
MsgBox(point)
If you want to process five different TextBoxes in a loop, one way to do that is to create an array containing all the TextBoxes and loop through the array.
Dim boxes() As TextBox = {TextBox3, TextBoxX, TextBox22, TextBoxBB, TextBoxA}
For Each box As TextBox in boxes
box.Focus()
If box.Text = sum Then
MsgBox.("correct")
point += 1
Else
MsgBox("wrong")
End If
Next
Alternatively, if you want the user to enter five answers one at a time in a single TextBox, you can have the user click a button each time an answer is entered. You should define the counter outside the button's click handler.
Private ctr As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ctr += 1
If ctr > 5 Then Button3.Enabled = False
initialize()
If TextBox3.Text = sum Then
MsgBox("correct")
point += 1
Else
MsgBox("wrong")
MsgBox(sum)
End If
MsgBox(point)
End Sub

Why does Windows.Media.VisualTreeHelper.GetChild(...) exit the sub?

I have a vb application with a listview.
I was testing and ended up with this sub for my dragover event:
Private Sub ListView1_DragOver(sender As Object, e As DragEventArgs) Handles ListView1.DragOver
XToolStripLabel.Text = "X:" & e.X
YToolStripLabel.Text = "Y:" & e.Y
Dim Insertion As Integer = GetInsertion(MousePosition, ListView1)
SelectionTestTSlabel.Text = "SEL " & Insertion
If CurrentlyDragging Then
Dim gr As Graphics = ListView1.CreateGraphics
Windows.Media.VisualTreeHelper.GetChild(New Windows.DependencyObject(), 0) 'I know this seems like it doesn't do much
MsgBox("") 'I actually added this later because the rest of the code did not run normally.
Dim InsertionY As Integer = 24 + (Insertion * TestItemHeight)
If MyLastInsPoint = Insertion Then
Else
ListView1.Refresh()
gr.DrawLine(New Pen(Color.Gray, 3), New Point(0, InsertionY), New Point(ListView1.Width, InsertionY))
End If
MyLastInsPoint = Insertion
End If
End Sub
I realized that the MsgBox("") doesn't popup a message box then i spammed breakpoints on the whole sub.
The sub ran as normal but after this line,
Windows.Media.VisualTreeHelper.GetChild(New Windows.DependencyObject(), 0)
the program returned to the form and did not execute the MsgBox("") line.
What's wrong with the line? The program did not even stop at the End Sub breakpoint.
New Windows.DependencyObject() has no children, so index 0 is not available
See the remaks on MSDN
Call the GetChildrenCount method to determine the total number of
child elements of a parent visual.
The value of reference can
represent either a Visual or Visual3D object, which is why the common
base type DependencyObject is used here as a parameter type.

Referencing a picturebox using a variable image in Visual Basic 2012

I'm trying to modify an answer found here but I'm not having much luck....
What I'm trying to do is:
I have 12 pictures and 12 pictureboxes.
In a loop I'm trying to get it to check for the number and load the corresponding image(s)
so if it's #3 it should load images 1-3 in pictureboxes 1-3
I think I'm close but i can't figure it out
Declaration of Dictionary and passing subroutine
Dim ctrlDict As New Dictionary(Of Image, Control)
Dictionary(ctrlDict)
Definition Sub
Sub dicti(pictures() As Image, ByRef ctrlDict As Dictionary(Of Image, Control))
ctrlDict.Add(pictures(0), PictureBox1)
ctrlDict.Add(pictures(1), PictureBox2)
ctrlDict.Add(pictures(3), PictureBox3)
End Sub
Loop
The error I'm getting is "programname.my.resources is a namespace and cannot be used as an expression" and "picturebox is a type and cannot be used as an expression"
Sub Output(Days() As String, Prices() As String, WhichDay As String, total As Double, ctrlDict As Dictionary(Of Image, Control))
For i As Integer = 0 To 11
If WhichDay >= i Then
ctrlDict(PictureBox & i).Image = pictures(i)
End If
Next
End Sub
Off the top of my head, Pretty sure you cant do
ctrlDict(PictureBox & i).Image = pictures(i)
youll need to find the control that has the name property equal to (PictureBox.name & i). Then use that control and set the image.
Dont have an IDE on this computer but it will be something like
dim controlToUse as PictureBox
for each ctrl in ctrlDict
if ctrl.value.name = "Whatever youre calling your pictureboxes" & i then
controlToUse = ctrl.value
exit for
end if
next
if not controlToUse is nothing then controlToUse.image = pictures(i)
Please note that the above is psuedocode. Do not expect it to compile!
I found the answer
Dim pic As PictureBox
For i As Integer = 0 To 11
If WhichDay >= i Then
pic = Me.Controls("picturebox" & i + 1)
pic.Image = pictures(i)
End If
Next

How can you detect key presses in vb console mode?

I wanted to make a program involving a menu at the start but instead of using the typical select case I wanted to do it differently.
I wanted it to give the user some options and then they would use the arrow keys to move a little arrow next to the options.
Finally you press enter and then you would advance to the next screen that you selected. I have been searching the web but I could only find this kind of stuff in form mode, not console. If this isn't possible let me know and I would appreciate any feedback.
One simple way is with a menu class that will print out the menu and highlight the item number passed to it:
Class Menu
Private MenuList As New List(Of String)
Public ReadOnly Property LineCount As Integer
Get
Return MenuList.Count
End Get
End Property
Public Sub CreateNewMenu(strings As IEnumerable(Of String))
MenuList.Clear()
For Each s In strings
MenuList.Add(s)
Next
End Sub
Public Sub PrintMenu(highlight As Integer)
If highlight < 0 OrElse highlight > MenuList.Count - 1 Then
Throw New Exception("Invalid menu index")
End If
For I = 0 To MenuList.Count - 1
If I = highlight Then
SwapColors()
Console.WriteLine(MenuList(I))
Console.ResetColor()
Else
Console.WriteLine(MenuList(I))
End If
Next
End Sub
Private Sub SwapColors()
Dim temp = Console.BackgroundColor
Console.BackgroundColor = Console.ForegroundColor
Console.ForegroundColor = temp
End Sub
End Class
To use it would look like this:
Dim MainMenu As New Menu
MainMenu.CreateNewMenu(
{
"Option A",
"Option B",
"Option C"
})
Dim CurrentItem As Integer = 0
Dim CurrentKey As ConsoleKey
While CurrentKey <> ConsoleKey.Enter
Console.Clear()
MainMenu.PrintMenu(CurrentItem)
CurrentKey = Console.ReadKey(True).Key
If CurrentKey = ConsoleKey.UpArrow Then
CurrentItem -= 1
ElseIf CurrentKey = ConsoleKey.DownArrow Then
CurrentItem += 1
End If
CurrentItem = (CurrentItem + MainMenu.LineCount) Mod MainMenu.LineCount
End While
This will use inverted colors to highlight the selected item, but this can easily be changed to add an indicator when writing the item to the console, if desired.
When the while loop exits CurrentItem will hold the 0-based index of the selected menu item.
One advantage of this approach is that you can have several collections of menus that will allow you to show specific ones based on the selection.