Solved

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

Posted on 2006-06-17
9
469 Views
Last Modified: 2006-11-18

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!
0
Comment
Question by:donniedarko801
  • 5
  • 2
  • 2
9 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 450 total points
ID: 16927236
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.

Fernando

0
 
LVL 14

Assisted Solution

by:ptakja
ptakja earned 50 total points
ID: 16927240
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)
        MessageBox.Show(dtm_Time_Preset.Seconds.ToString)
    End Sub

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

00:00:05
00:00:10
00:00:15
00:00:30

etc...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16927257
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.

Fernando
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16927343
Thx Fernando.

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

dtm_Time_Preset = TimeSpan.Parse(cbo_Hr & ":" & cbo_Min & ":" & "00")
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16927366
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")
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16927379
Genious!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16927387
No far from that, LOL :=)
0
 
LVL 14

Expert Comment

by:ptakja
ID: 16927460
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!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 16927493
ptakja; I agree with that the format method is much neater.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now