Parsing Combobox.Text to use in a TimeSpan - How?

Hello Experts,

Here's what I have and it works great!

Private dtm_Time_Preset As TimeSpan = TimeSpan.Parse("00:00:05")

Here's what I want and it doesn't work at all!

Private dtm_Time_Preset As TimeSpan = TimeSpan.Parse(cbo_Preset.Text)

Thankx for any help!
Who is Participating?
Fernando SotoConnect With a Mentor RetiredCommented:
Hi donniedarko801;

I suspect that the problem you are having is that the controls have not been initialized yet on the form and you are getting an error such as

An unhandled exception of type 'System.NullReferenceException' occurred in the application.  Object reference not set to an instance of an object

The reason is that a Class level variable is initialized before the New subroutine and non of the controls have been created. to correct your problem do the following.

    Private dtm_Time_Preset As TimeSpan

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        dtm_Time_Preset = TimeSpan.Parse(cbo_Preset.Text)

    End Sub

I hope that this is of some help.


ptakjaConnect With a Mentor Commented:
What version of VS are you using? I just put this code into VS2005 and it works fine:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dtm_Time_Preset As TimeSpan = TimeSpan.Parse(ComboBox1.Text)
    End Sub

Combobox has several preset timespans in it that look like this:


Fernando SotoRetiredCommented:
Hi ptakja;

Your code works fine because the control has already been created. The author of the question is using a Private declaration which can not be used in as a local variable in a sub.

see my post.

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

donniedarko801Author Commented:
Thx Fernando.

By the way, could this approach ever work? Because it doesn't...

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr & ":" & cbo_Min & ":" & "00")
Fernando SotoRetiredCommented:
If cbo_Hr and cbo_Min are ComboBox object then you need to reference the text box of the control to get the string like this:

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr.Text & ":" & cbo_Min.Text & ":" & "00")
donniedarko801Author Commented:
Fernando SotoRetiredCommented:
No far from that, LOL :=)
Fernando, I didn't see your post when I posted mine. But you are absolutely right on.

Personally, I prefer to use the String.Format method instead of concatinating strings using "&" all over the place. So I would code the line in the post above like this:

dtm_Time_Preset = TimeSpan.Parse(String.Format("{0}:{1}:00", cbo_Hr.Text, cbo_Min.Text))

I find this format much easier to read and less prone to error with missing quotes, especially for SQL statements!
Fernando SotoRetiredCommented:
ptakja; I agree with that the format method is much neater.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.