Solved

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

Posted on 2006-06-17
9
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 63

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 63

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
LVL 63

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 63

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 63

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 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