Visual Basic GUI Input Validation - vb.net

I took an entry level computer programming class this past term and I'm having problems with my final project. I have to design a program in visual basic GUI that asks the player to accurately guess a number between 1-100 within a limited number of guesses.
My first form asks the user to set the number of guesses allowed. It has one textbox and an "enter" button, among other buttons that I've gotten to work.
I'm trying to get code to work that will validate the input on the guesses allowed. Specifically, I want a message box to pop up if the player enters letters or special characters instead of numbers, or enters a number less than zero, or greater than twenty. Here's what I have:
Public Class Noofguesses
Shared maxguesscnt As Integer
Private Sub Numberofguesses_TextChanged(sender As Object, e As EventArgs) Handles Numberofguesses.TextChanged
End Sub
Private Sub Quit_Click(sender As Object, e As EventArgs) Handles Quit.Click
End
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form3.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Val(Numberofguesses) > 20 Then MsgBox("Number of Guesses Cannot Exceed 20")
If Val(Numberofguesses) < 0 Then MsgBox("Number of Guesses Must Be Greater Than 0")
If Not IsNumeric(Numberofguesses) Then MsgBox("Entry Cannot be Letters or Characters")
Me.Close()
Form2.Show()
End Sub
End Class
What am I doing wrong? Please let me know.
Thanks

I would generally suggest using a NumericUpDown rather than a TextBox, in which case no validation is required. As this is an assignment though, I'm guessing that validating a TextBox is a requirement. In that case, you should use Integer.TryParse to validate a String, i.e. the Text of the TextBox and convert it if it is valid. You can then test the converted value to make sure that it isn't less than zero, etc. I'm not going to write the code for you, given that this is homework, but that should be enough for you to do it yourself or, if you feel you must, find examples online.

Related

Textbox search accented characters problem?

I am looking for information on datagrid using textbox assign textchanged_event.
The problem I encountered was that when I typed the characters with a accented then datagrid would blink (it seems every change would be recorded in the textbox not just the final result).
Example: when typing "khang"
If I continue to type the letter "a", in english it will result in "khanga" and everything goes well, but in some languages it should be "khâng" and I see in textchanged it will take many steps as:
"khang" -> "khang•" -> "khang" -> "khan" -> "kha" -> "khâ" ->"khân"->"khâng"
in this moment, the datagrid will blink continuously.
what should I do to textchanged only handle the final result?
Thanks for advices!
You can't guarantee that you only handle the final value unless you use a Button.Click instead of a TextBox.TextChanged, or maybe force the user to hit Enter and handle the TextBox.KeyDown or the like. What you can do instead is ensure that you only act on the TextChanged if no other TextChanged has occurred for a specific length of time by using a Timer. That way, the delay is not so great that the user really feels it but it is long enough to allow multiple keystrokes without acting on them in between.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer every time the Text changes.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Perform filter when time expires.
Timer1.Stop()
BindingSource1.Filter = $"SomeColumn LIKE '%{TextBox1.Text}%'"
End Sub
It's up to you want you set the Interval of the Timer to but I'd be looking at around 300. You can do a bit of experimentation to find what gives you the best balance.

How can I let a code run permanent in the background, which waits for an input ? (Visual Basic)

I want to program a round based rpg like game in visual basic.
For that I want to make it possible, to show a picture of the current selected unit in the right bottom corner and add some information about the heal points and some other stats.
So I want to make a program part, which permanently asks, which of the units is selected and based on that let a other program part running, which changes the picture and the information about hp and this stuff, matching to the current unit.
But I am not able to run a program which runs in the background and doesn't freezes the main program while running (may I need a background worker ?)
Also I am not very sure, what for a program type I need to used for this (like a sub or something).
I don't know other types than sub's in vb, but I could derive it from my knowledge of java programming (functions and objects).
I know this is a bit much to ask, but I need to know, which program type I need to use and how I can let it run permanent in the background + how to transfer some information between these parts.
Would be nice, if someone could help me and explain a bit instead of just saying something like: you need a background worker use this link xy.
I tried to use a variable, which becomes a different number, when a other unit is clicked, unfortunately you cant see much of my code right now, since for this form, I didn't created much more than the graphical interface.
Public Class FormBattlescreen
Dim marked As Integer = 0
Private Sub Formsounds_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
FormMainCampagne.Close()
End Sub
Private Sub OwnSoldier1_Click(sender As Object, e As EventArgs) Handles OwnSoldier1.Click
marked = 1
End Sub
Private Sub OwnSoldier2_Click(sender As Object, e As EventArgs) Handles OwnSoldier2.Click
marked = 2
End Sub
Private Sub OwnSoldier3_Click(sender As Object, e As EventArgs) Handles OwnSoldier3.Click
marked = 3
End Sub
Private Sub EnemyOfficer1_Click(sender As Object, e As EventArgs) Handles EnemyOfficer1.Click
marked = 4
End Sub
Private Sub RunGame()
Select Case marked
Case 0 'nothing is selected//at the beginning
Case 1
PictureBoxStats.Image = My.Resources.Soldier2
Case 2
Case 3
Case 4
End Select
End Sub
End Class
Thank you, I appreciate your help.

Counting number of characters in TextBox

I have a TextBox by which if the user enters more than 10 characters it displays a MsgBox. That part works :D
The problem is the message also displays if the TextBox is empty and the user types the first character. I think thats because Null is seen as something greater than 10? but I'm not sure.
A) What is going on?
B) How can I fix this?
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Length >= 10 Then
MsgBox("WARNING")
End If
End Sub
You can try this. By using trim, white space characters is ignored. For an example, if the user only entered 10 [Spacebar] keys it will trim it out.
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Trim().Length() >= 10 Then
MsgBox("WARNING")
End If
End Sub

VB.net SendKeys to send keys combined not using "ALT" or "CTRL", like "{INS}(S)"

I need to send the keystroke combination INSERT + S keys simultaneously but it seems that VB only leaves use key combinations with ALT, CTRL and SHIFT.
Is there any way to do it?
I've tried:
{INS}(S)
{INS}S
and many others, but this does not work.
Thanks a lot.
It does. See:
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Had you tried "{INS}(S)" ?
UPDATE
I did the following test:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
SendKeys.Send("{Ins}S")
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
MsgBox(e.KeyCode)
End Sub
Look, I received in the KeyDown event 2 codes: the INS and the "S"
The problem is about the KeyDown event will file TWO TIMES, I mean, you cannot get the INS-S code with ONE reading.
So, you must consider have a FRIEND/PUBLIC variable to receive both codes and only after this, make the critic about these.
Good luck

Menu Strip Control "Window"

I am working on a project and am just stymied by this. It should be really straight forward. I have included the code so you can see the other Menu Strip Items.
The user has the ability to open as many "Child" forms into the mdiParent form. I would like the "Window" function on the menu strip to populate with the Bank Name found on the Child form so if a user had 10 bank forms open, they could find a specific form by clicking Window and seeing the bank name (the name of the text field which I would like to pull is Bank.lblbank.text) This functionality was found in the 2007 and older versions of many of the Microsoft Suite products.
If I hadn't seen my professor do this in class, I would think it was a bit of proprietary Microsoft Office coding that us mere mortals cannot access. Unfortunately, he whipped it out and I didn't get it captured.
Obviously, I am not asking the right questions on the search engines because I cannot find a clear answer. Does anyone have any advice? This functionality isn't necessary but a little something I want to add. I've worked on this way too long and just want a little bit of success.
Let me know if a zip copy of the project or screen prints would be helpful. I'm happy to send them your way.
Appreciate everyone looking at this post and their feedback. Thank you for your time!
Lauren
Public Class Loan_Evaluator
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub NewLoanToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewLoanToolStripMenuItem.Click
Dim NewBank As New Bank
NewBank.MdiParent = Me
NewBank.Show()
End Sub
Private Sub VerticalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VerticalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileVertical)
End Sub
Private Sub CascadeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CascadeToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub
Private Sub HorizontalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HorizontalToolStripMenuItem.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub
Private Sub WindowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WindowToolStripMenuItem.Click
End Sub
End Class
Select the main MenuStrip control, not the ToolStripMenuItem. So you'd click on MenuStrip1, not WindowToolStripMenuItem, for example. Now change the MdiWindowListItem() property to "Window" (or whatever menu item you want to be populated with open MdiChildren). Done.