Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Text Box Control
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01025856
Message ID:
01026018
Views:
17
Just an quick inquiry having to do with performance on something like this. Which one would perform better (faster and smoother to the user)

1.
Use a textbox and append the new text to its text property and use the scrolltocaret method
2.
Use a listbox, add new items to the list

In my test it seems that a listbox would be 'nicer'.... Here is sample code in VB.NET for you to digest (one textbox[multiline, vertical scrollbar], one listbox, four buttons) ;)
Public Class Form1
    Inherits System.Windows.Forms.Form
'blah blah blah the things the are added automatically go here
    Public Function addText(ByVal newText As String)
        'Is it first item on the list?  If so, do not add linefeed
        If TextBox1.TextLength = 0 Then
            TextBox1.Text = newText
        Else
            'I could use string builder here, but I am lazy
            TextBox1.Text = TextBox1.Text + Chr(13) + Chr(10) + newText
        End If
        TextBox1.SelectionStart = TextBox1.TextLength - 1
        TextBox1.ScrollToCaret()
    End Function
    Public Function addList(ByVal newItem As String)
        'Use the begin and end update to avoid flicker
        ListBox1.BeginUpdate()
        ListBox1.Items.Add(newItem)
        ListBox1.SetSelected(ListBox1.Items.Count - 1, True)
        ListBox1.EndUpdate()
    End Function
    Private Sub butAddLst1K_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAddLst1K.Click
        'Adds 1000 items to the list
        Dim t As Int16
        For t = 1 To 1000
            addList("Item #" + t.ToString())
        Next
    End Sub
    Private Sub butAddLst1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAddLst1.Click
        'Adds 1000 items to the list
        addList("One more Item")
    End Sub
    Private Sub butAddTxt1K_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAddTxt1K.Click
        'Add 1000 'items' to the textbox
        Dim t As Int16
        For t = 1 To 1000
            addText("Item #" + t.ToString())
        Next
    End Sub

    Private Sub butAddTxt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAddTxt1.Click
        'Add 1 'item' to the textbox
        addText("One More Item")
    End Sub
end class
Ricardo A. Parodi
eSolar, Inc.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform