9 Replies - 1939 Views - Last Post: 22 August 2012 - 06:07 PM Rate Topic: -----

#1 fanntastic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Searching a list box, Windows Form App

Posted 22 August 2012 - 04:26 PM

I have a populated list box of names and i have a search button that i want to prompt the user to enter a name and then search for that name and highlight it or only show that name. Stuck on the coding for the search button.

All suggestions thankfully received

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Searching a list box, Windows Form App

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 04:39 PM

Stuck as in you don't know how to get the button event? Stuck as in you can't get access to the listbox contents? Stuck as is your code has an infinite loop while searching?

Show us you current code as well as ask a more explicit question. Tell us what you've already tried.
Was This Post Helpful? 0
  • +
  • -

#3 KieranQuinn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 15-August 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 04:43 PM

View Postfanntastic, on 22 August 2012 - 04:26 PM, said:

I have a populated list box of names and i have a search button that i want to prompt the user to enter a name and then search for that name and highlight it or only show that name. Stuck on the coding for the search button.

All suggestions thankfully received

Thanks

ListBox doesn't have a property that highlights a row, you'd have to draw it manually. You're better of using a ListView, which has BackColor property for items.

You could do it like this:
for (int i = 0; i < listView.Items.Count; i++)
    if (listView.Items[i].ToString().Contains(textBox.Text))
        listView.Items[i].BackColor = Color.Aqua;


Was This Post Helpful? 0
  • +
  • -

#4 fanntastic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 04:44 PM

I tried using console.writeline to get the input but it wouldnt work on the form.
Was This Post Helpful? 0
  • +
  • -

#5 fanntastic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 04:52 PM

View PostKieranQuinn, on 22 August 2012 - 04:43 PM, said:

View Postfanntastic, on 22 August 2012 - 04:26 PM, said:

I have a populated list box of names and i have a search button that i want to prompt the user to enter a name and then search for that name and highlight it or only show that name. Stuck on the coding for the search button.

All suggestions thankfully received

Thanks

ListBox doesn't have a property that highlights a row, you'd have to draw it manually. You're better of using a ListView, which has BackColor property for items.

You could do it like this:
for (int i = 0; i < listView.Items.Count; i++)
    if (listView.Items[i].ToString().Contains(textBox.Text))
        listView.Items[i].BackColor = Color.Aqua;



That looks like it would work well but unfortunately for other reasons it has to be a listbox.
Was This Post Helpful? 0
  • +
  • -

#6 KieranQuinn  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 6
  • Joined: 15-August 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 05:01 PM

View Postfanntastic, on 22 August 2012 - 04:52 PM, said:

View PostKieranQuinn, on 22 August 2012 - 04:43 PM, said:

View Postfanntastic, on 22 August 2012 - 04:26 PM, said:

I have a populated list box of names and i have a search button that i want to prompt the user to enter a name and then search for that name and highlight it or only show that name. Stuck on the coding for the search button.

All suggestions thankfully received

Thanks

ListBox doesn't have a property that highlights a row, you'd have to draw it manually. You're better of using a ListView, which has BackColor property for items.

You could do it like this:
for (int i = 0; i < listView.Items.Count; i++)
    if (listView.Items[i].ToString().Contains(textBox.Text))
        listView.Items[i].BackColor = Color.Aqua;



That looks like it would work well but unfortunately for other reasons it has to be a listbox.

You'll have to look at drawing the background via DrawItem. Here's a link that might help: http://stackoverflow...x-item-winforms
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 05:06 PM

ListBox has a SelectedItem/SelectedItems (or corresponding SelectedIndex/SelectedIndexes) that lets you mark the selected items. It uses the default windows selection color rather than being able to do a custom highlighting like with the ListView.

Chances are that you want to select that items or those items anyway since you did do a search to find them. That would imply that you want to next perform some operation on them.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 05:24 PM

Here's a simple example of selecting all types that start with the letter U:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace SimpleWinForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var listbox = new ListBox();
            listbox.Dock = DockStyle.Fill;
            listbox.SelectionMode = SelectionMode.MultiSimple;
            this.Controls.Add(listbox);

            foreach (var color in Enum.GetNames(typeof(System.TypeCode)))
                listbox.Items.Add(color);

            for(int i = 0; i < listbox.Items.Count; i++)
            {
                string name = (string) listbox.Items[i];
                if (name.StartsWith("U"))
                    listbox.SetSelected(i, true);
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#9 fanntastic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-August 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 05:30 PM

But would that prompt the user to enter the search criteria, because i cant get it to do that, i tried using console.writeline but it will not show.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: Searching a list box, Windows Form App

Posted 22 August 2012 - 06:07 PM

No, the code there was just to show listbox selection happening.

To prompt for input, you'll need to bring up a new form dialog and ask the user to enter the text to search for. At least that's the WinXP and older style of doing things. The modern style is to have a textbox in the same form as your listbox and either have a search button, or the listbox incrementally has things selected as the user types into the textbox.

If you want to go with your original plan of prompting, but don't want to create another form, you can use the built in MsgBox that comes with the VisualBasic assembly: http://msdn.microsof...ion.msgbox.aspx

And the reason why you can't access the console for input and output is because you chose to create a Windows application and by default the application's console standard I/O is not hooked up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1