Results 1 to 4 of 4

Thread: [2005] Appending strings

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    [2005] Appending strings

    Hello,

    i am using the following,

    However, i am just wondering is they a better way to do this.

    The user will enter an integer value into a text box, then select from a combo box. For example enter 3 in the text box, and select days from the combo box.

    The end result would be: 3 Days.

    Code:
    'Append the duration from the combo box to the value entered into the text box.
        Private Sub cboDuration_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDuration.SelectionChangeCommitted
    
            Try
                Dim value As String = Me.txtEstimatedDuration.Text
                Dim duration As String = Me.cboDuration.SelectedText
    
                Me.txtEstimatedDuration.Text = String.Format("{0} {1}", value, duration)
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    Many thanks for any ideas.
    steve

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Appending strings

    I forget to mention that the combo box is not bounded.
    steve

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Appending strings

    That's how I'd do it. It might be quicker by a few nanoseconds to use concatenation with so few substrings but I think String.Format is more readable.

    Just note though that you are misusing the ComboBox.SelectedText property. You should be using the Text property. SelectedText is the portion of the Text that the user has selected. If none is selected then SelectedText is an empty string, even if there's text in the field.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Appending strings

    Hello,

    Thanks for your reply.

    There was a problem using the text property. I found the selectedItem worked better, which return a string. It seems to me that the text property will not insert what has been selected by the user. But what was in there previous to the user selected the combo box. (so always 1 selection behind)

    I did encounter another problem, was that if the user changed their mind, it would keep on appending was was selected from the combo box.

    so you might end up with something like this: 10 days months weeks

    I fixed this by using tryparse if there is a single integer and using the split method and getting the integer value out and discarding everything else. Not sure if my method is the best, but would be happy to have any comments to make it better.

    Thanks for any comments,

    vb Code:
    1. 'Append the duration from the combo box to the value entered into the text box.
    2.     Private Sub cboDuration_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDuration.SelectionChangeCommitted
    3.  
    4.         Try
    5.             Dim testInteger As Integer = 0
    6.             Dim value As String = String.Empty
    7.             Dim duration As String = String.Empty
    8.  
    9.             If (Integer.TryParse(Me.txtEstimatedDuration.Text, testInteger)) Then
    10.                 'This is the first time the user has selected
    11.                 value = Me.txtEstimatedDuration.Text
    12.                 duration = Me.cboDuration.SelectedItem
    13.                 Me.txtEstimatedDuration.Text = String.Format("{0} {1}", value, duration)
    14.             Else
    15.                 'The user has already entered from the combo box, so instead of appending another combo box value
    16.                 'clear the contents. User could have changed there mind.
    17.                 'Get the integer value and clear the rest of the contents
    18.                 value = Me.txtEstimatedDuration.Text
    19.                 Dim myValue(10) As String
    20.                 myValue = value.Split(" ")
    21.                 value = Convert.ToInt32(myValue(0))
    22.                 duration = Me.cboDuration.SelectedItem
    23.                 Me.txtEstimatedDuration.Text = String.Format("{0} {1}", value, duration)
    24.  
    25.             End If
    26.         Catch ex As Exception
    27.             MsgBox(ex.Message)
    28.         End Try
    29.     End Sub
    Last edited by steve_rm; May 1st, 2007 at 04:23 AM.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width