vb .net 2012 datepicker uppercase MAY month turns into 5AY - vb.net

I used this code to make month in uppercase form, the problem is that the month of May turns into 5AY
DateTimePicker1.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = Strings.UCase(Format(DateTimePicker1.Value, "ddMMMyyyy"))

After realising that you were trying to actually use the month name as literal characters in your format specifier, I knew that you just had to escape those special characters. Normally, date format specifiers use the "\" character to escape but the DateTimePicker.CustomFormat property specifically requires you to wrap escaped characters in single quotes. I just tested this and it worked as expected:
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker1.CustomFormat = $"dd'{DateTimePicker1.Value.ToString("MMM").ToUpper()}'yyyy"
End Sub
If you're using an older version of VB that doesn't support string interpolation, this will do the same thing:
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker1.CustomFormat = String.Format("dd'{0}'yyyy", DateTimePicker1.Value.ToString("MMM").ToUpper())
End Sub
There's no need to set the Format property in code as it won't change. Just set it once in the designer. You'll probably want to execute that same code on Load because there's no ValueChanged event when the DateTimePicker takes on the current date/time by default.

Related

Recognize two or more digits

How to make Visual Basic .NET recognize two or more digits instead of one?
When I execute it, it only validates correctly in case both numbers are one digit.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim menorOigual As Boolean
menorOigual = numero1.Text <= numero2.Text
MsgBox(menorOigual)
End Sub
By comparing the value of the Text properties, you're comparing strings, which is why it only works with single digits (because as a string "10" comes before "3", because each character is compared in turn). You need to convert the strings to a numeric type like integer, e.g.
Convert.ToInt32(numero1.Text)
For production code, you'll probably also need to validate the conversion, so see also:
Integer.TryParse

Visual Studio 'null' is not defined

I am trying to learn Visual Studio with VB and started by using the Microsoft training by creating a Picture Viewer form.
Everything was going fine and I figured out how to use VB code which is slightly different than the examples in C#. I have most of the training done and can bring in a pictures from the app, but I am having trouble with the closeButton based on the code provided. I am at a loss after searching for many hours. The following is the code from the Open Picture and Close Code. As I was inputting the code from the interface there was no null so I typed it in and that didn't work.
Any help would be greatly appreciated. Thanks
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles showButton.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
End If
PictureBox1.Load(OpenFileDialog1.FileName)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = null
End Sub
Think if you change null to Nothing, you should be all good
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles clearButton.Click
PictureBox1.Image = Nothing
End Sub
Change the keyword null to nothing please .
For non-nullable value types, Nothing in Visual Basic differs from null in C#. In Visual Basic, if you set a variable of a non-nullable value type to Nothing, the variable is set to the default value for its declared type. If a variable is of a value type, the behavior of Nothing depends on whether the variable is of a nullable data type. If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type.
See this document for more details and demo.

Visual Basic GUI Input Validation

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.

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

How to set default value of DateTimePicker to display some non-date string such as "MM-DD-YYYY"

I am using VB.NET's DateTimePicker, but, when it is first created, it always shows the current date by default (e.g. 01-09-2015). I want to change it so it defaults to some literal string like "MM-DD-YYYY" rather than the current date.
You can use a custom format for this.
Set the Format property to Custom and then set the CustomFormat property to 'MM-DD-YYYY' (be sure to include the ' characters).
You will probably need to reset the format once the user selects a date so that the selected date is displayed properly. You can do this in the ValueChanged event handler:
Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
DateTimePicker1.Format = DateTimePickerFormat.Long
End Sub