Solved

Need to clear Textbox after each number is added to ListBox

Posted on 2004-10-07
10
200 Views
Last Modified: 2008-03-17
I have a program I made for my kids math homework. The problem I am having is the user enters a number in a textbox then click an enter buttonor presses enter on the keyboard and the number is added to the listbox.  What I want happen next is the textbox is cleared and textbox has focus so the user can enter a new number right away without having to clear the textbox themselves.  What is happening is after you type the first number in and click the button there is a delay in which the number appears in the listboxafter that though the rest of the numbers are added to listbox immediately.  This isn't such a big deal but I was wondering if there is a better way to clear the textbox after each number is added to the listbox.
here is my code....

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Num = CLng(TextBox1.Text)
        ListBox1.Items.Add(Format(Num, "#,#"))
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub


Thanks...
0
Comment
Question by:ucla11
[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
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12251069
You can just set TextBox to blank like following:

TextBox1.Text = ""
0
 
LVL 27

Expert Comment

by:planocz
ID: 12251084
Try something like this...
 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Num = CLng(TextBox1.Text)
        ListBox1.Items.Add(Format(Num, "#,#"))
        Clear_Form
    End Sub
   
   Private Sub Clear_Form
        ListBox1.Refresh
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub
0
 

Author Comment

by:ucla11
ID: 12251984
TextBox1.Text = "" does the same thing

 Private Sub Clear_Form
        ListBox1.Refresh
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub
works in reverse the listbox loads quickly but the textbox clears slowly, and only after the first inputthen it works fine either way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:planocz
ID: 12252368
This works for me...

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Num As Long
        If TextBox1.Text.Length <= 0 Then
            Exit Sub
        End If
        Num = CLng(TextBox1.Text)
        ListBox1.Items.Add(Format(Num, "#,#"))
        Clear_Form()

    End Sub
    Private Sub Clear_Form()
        ListBox1.Refresh()
        TextBox1.Clear()
        TextBox1.Focus()
    End Sub
0
 

Author Comment

by:ucla11
ID: 12252839
Maybe I am doing some different because it still works  the same for me.....works in reverse the listbox loads quickly but the textbox clears slowly, and only after the first input then it works fine either way.

the only thing that looks different is   Dim Num As Long....I already declared Num.......

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private Num As Long


there is about a 3second delay for me before the textbox clears(again it is only on the first number inputed and only when you first start the Application.  If I clear  the listbox an enter new numbers it works fine, even the first number inputed.  If I can't fix it that is okay but it is driving me crazy.  I made the exact same program in VB 6.0 and it works fine.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 125 total points
ID: 12252913
Hi ucla11,

I don't think the issue is in clearing the textbox... I think that the issue is that it's taking longer to add the first item to the listbox than it takes to add the second or third etc.  This is because the system has to allocate new storage space for the array of list items etc....

I don't think there is much you can do about the delay, however you might be able to fool the user by changing the order that you do things:

        Num = CLng(TextBox1.Text)
        TextBox1.Clear()
        TextBox1.Focus()
        ListBox1.Items.Add(Format(Num, "#,#"))


Cheers!
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12254469
I would either do as mdougan said or, right when the form first opens, add a number to the list box and then removed it right quick.  Sure, it's a stupid work around for a problem that should exist, but at least the delay will be out of the way by the time the user is ready to enter data.

Good luck.
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12254476
"Sure, it's a stupid work around for a problem that should exist..."

should read:

"Sure, it's a stupid work around for a problem that should not exist..."
0
 
LVL 18

Expert Comment

by:mdougan
ID: 12254689
I like that idea UncleMidriff.... the user is not really going to notice it much as the form loads.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12255131
Well... I have pasted your code in one form and there are no problem, the textBox is cleared as fast as the first time.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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