|
-
May 1st, 2007, 03:21 AM
#1
Thread Starter
Frenzied Member
[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.
-
May 1st, 2007, 03:30 AM
#2
Thread Starter
Frenzied Member
Re: [2005] Appending strings
I forget to mention that the combo box is not bounded.
-
May 1st, 2007, 03:32 AM
#3
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.
-
May 1st, 2007, 04:20 AM
#4
Thread Starter
Frenzied Member
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:
'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 testInteger As Integer = 0
Dim value As String = String.Empty
Dim duration As String = String.Empty
If (Integer.TryParse(Me.txtEstimatedDuration.Text, testInteger)) Then
'This is the first time the user has selected
value = Me.txtEstimatedDuration.Text
duration = Me.cboDuration.SelectedItem
Me.txtEstimatedDuration.Text = String.Format("{0} {1}", value, duration)
Else
'The user has already entered from the combo box, so instead of appending another combo box value
'clear the contents. User could have changed there mind.
'Get the integer value and clear the rest of the contents
value = Me.txtEstimatedDuration.Text
Dim myValue(10) As String
myValue = value.Split(" ")
value = Convert.ToInt32(myValue(0))
duration = Me.cboDuration.SelectedItem
Me.txtEstimatedDuration.Text = String.Format("{0} {1}", value, duration)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|