Re: [2005] Appending strings
I forget to mention that the combo box is not bounded.
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.
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