Code for changing font through menu selection like "Arial"????

Recommended Answers

All 10 Replies

TextBox1.Font = New System.Drawing.Font("Arial", 10)

Trying to validate the input of the textbox,when i enter a a number it displays the message box aswell

'Converting String TextBox to Integer
        guess = Convert.ToInt32(txtGuess.Text)


        If Not IsNumeric(txtGuess.Text) Or Val(txtGuess.Text) Then
            MessageBox.Show("Invalid Input")
        End If

Maybe use a Try / Catch block:

Try
guess = Convert.ToInt32(txtGuess.Text)
Catch Ex as Exception
MessageBox.Show("Invalid Input")
End Try

I think that is the right format - I am not near the right computer to attempt. When you start a "Try" block much of the code generates automaticly.

Got to try it - this works.

Try
            guess = Convert.ToInt32(textbox1.Text)
        Catch ex As Exception
            MsgBox("Invalid Input - not an integer")
        End Try

Thanks that worked fine,I'am still having a problem with the font,

Private Sub TimesNewRomantoolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimesNewRomanToolStripMenuItem.Click
        Me.Font = New System.Drawing.Font("Times New Roman")



    End Sub

it says font cannot be used as an expression??

Are you trying to change the font on everything on the form? You might need to address each control individually. You could loop through each control:

Dim C as control
For each C in Me.Controls
C.Font = New System.Drawing.Font("Times New Roman")

Why are you doing this?

Here is the right answer!

For Each c In Me.Controls
            c.Font = New System.Drawing.Font("TimesNewRoman", 10)
        Next

You cannot have spaces in the font name, and you must show a font size. I tried it with just the Me.Font, and that did not work. I think you need to loop through the controls, or name each control on its own line.

I am curious as to why you are doing this? Is this to give the user some options on the appearance?

On the topic of testing numerics, there is a performance penalty to using try/catch blocks as your testing method.

IsNumeric is a fine VB-specific alternative. Another option and one that is available across VB and C# is the Int32.TryParse method.

Consider the following code:

Imports System.Diagnostics

Module Module1

    Sub Main()

        Dim s As String
        s = "123a"

        Dim stopwatch As New Stopwatch()

        'Timing a try/catch block
        stopwatch.Start()
        Try
            Dim i As Integer
            i = Convert.ToInt32(s)
        Catch ex As Exception
            Console.WriteLine("Intentional error")
        End Try

        stopwatch.Stop()
        Console.WriteLine("Try/Catch - {0}ms", stopwatch.ElapsedMilliseconds)
        stopwatch.Reset()


        'Timing Int32.TryParse
        stopwatch.Start()
        Dim x As Integer
        If Not Int32.TryParse(s, x) Then
            Console.WriteLine("Intentional error")
        End If

        stopwatch.Stop()
        Console.WriteLine("Int32.TryParse - {0}ms", stopwatch.ElapsedMilliseconds)
        stopwatch.Reset()


        'Timing IsNumeric
        stopwatch.Start()
        If Not IsNumeric(s) Then
            Console.WriteLine("Intentional error")
        End If

        stopwatch.Stop()
        Console.WriteLine("IsNumeric - {0}ms", stopwatch.ElapsedMilliseconds)
        stopwatch.Reset()

        Console.Read()

    End Sub

End Module

And the results

Intentional error
Try/Catch - 3ms
Intentional error
Int32.TryParse - 0ms
Intentional error
IsNumeric - 0ms


Error-trapping should be reserved for actually trapping errors, as they are incredibly inefficient methods of performing tests.

That is interesting - I have used isnumeric, and have seen try.parse, but when I took a class I was introduced to the try / catch block and have used that a lot.

In Buzzy's example above, wouldn't isnumeric miss a non-integer numeric entry (e.g. 15.4?) What would be an efficient way identify a decimal input?
As a hobbiest, I am not usually writing programs that would suffer from that sort of time penalty, but it is good to know! Thanks.

IsNumeric will catch all numeric inputs; doubles, integers, whatever you throw at it. As the function name implies, all it's worried about is whether or not it's a number, it doesn't care if it's a whole number or decimal.

The good thing about TryParse is that it exists not only for integers but also for doubles, decimals, DateTimes, etc. It also takes care of populating your variable at the same time. With IsNumeric, you're checking to see if the input is good, and then you convert that input to your desired variable type after determining its validity. With TryParse, you're verifying the input and storing it in your variable all in the same function call. And as I said in my previous post, it also has the benefit of also being available in C# (as opposed to IsNumeric), so if porting code between languages ever becomes a concern, it will go much more smoothly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.