listbox - topindex

kaddu747

New member
Joined
Sep 30, 2005
Messages
4
Programming Experience
1-3
So I'm designing a listbox with employee names.The listbox itself can't "show" more than 10 names.

So I am supposed to show the newest entry after 10 names on top.
 
If you just need the top 10 newest, change your query to "Top 10" and sort desc by your table's identity (assuming it incriments upwards).

If you need the 10 you'd normally get and then the newest one as well, use your original sql statement and UNION the "Top 1" in the second half of the query.
 
topindex-listbox

Its not really an SQL probelm.I am basically adding the names through a textbox and then hitting the add button which adds the names to the listbox below.Now the listbox can display 10 names...once we enter the 11th name it should show on top.

VB.NET:
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        If txtName.Text.Length() > 0 Then
                With lstNames.Items
                    .Add(txtName.Text)
                    txtName.Text() = ""
                lblCount.Text = lstNames.Items.Count
            End With
        Else
                MessageBox.Show("Employee Name must have a value before we can add it to ListBox", "Add Error")
            End If
            txtName.Focus()
    End Sub
 
This code sample will always show the newest one on top. I added something to remove the last line when the cap is breached. Adjust it as necesary. :)
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lstNames.Items.Count = 10 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#008000]'Listbox is capped, drop last
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lstNames.Items.RemoveAt([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lstNames.Items.Count - 1)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].lstNames.Items.Insert(0, [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].txtName.Text.Trim)[/SIZE]
 
Back
Top