0

I am having problems searching through my Arraylist. The array list stores various information about a number of teams such as the image path to their logo and the team name etc. It is being filled from a separate datafile using a StreamReader

I would like the user to input something in a Textbox from a windows form such as the team name and then consequently the program will then search my arraylist for said string and open another form where the information of the searched team will be loaded up on screen using the Form.Load procedure

To put it simply.

private void btn_Search_Click(object sender, EventArgs e)
{
   //what code do I write here?
}

I understand that I might be a little to deep here for my current knowledge of coding so help would be appreciated.

EDIT: unfortunately it must be in an arraylist, sorry for the inconvenience.

zac
  • 17
  • 5

3 Answers3

4

If you can use LINQ:

string nameToMatch = "Tigers"; //can you tell who's from Michigan?
List<Team> teams = new ArrayList<Team>();
//fill team data here

Team selected = teams.FirstOrDefault(t => t.TeamName.Equals(nameToMatch, StringComparison.OrdinalIgnoreCase));

Something like this should work. (This will match the text exactly but allow the search to be case insensitive. You can read about other options here.)

If you want to match a list of all "partial matches", you can do this instead:

List<Team> matchedTeams = teams.Select(t => t.TeamName.Contains(nameToMatch));

Read here for an extension overload of Contains that takes a StringComparison enum value.

Community
  • 1
  • 1
Codeman
  • 12,157
  • 10
  • 53
  • 91
  • 1
    I would go with *FirstOrDefault* and then check for null -- as the user could enter anything into that TextBox. – George Johnston Apr 12 '13 at 19:56
  • -1: [Don't compare strings using ==](http://msdn.microsoft.com/en-us/library/vstudio/cc165449.aspx) – zimdanen Apr 12 '13 at 19:59
  • @zimdanen the purpose of this question is how to search through an ArrayList, not semantics on the proper technique for string matching in C#. – Codeman Apr 12 '13 at 20:00
  • @Pheonixblade9: The purpose of SO is to teach fresh young minds how to program correctly. – zimdanen Apr 12 '13 at 20:01
  • @Pheonixblade9: Because that didn't occur to me. Done, and will reverse to +1 if edit is accepted so I can change my vote. – zimdanen Apr 12 '13 at 20:06
  • I have yet to learn anything using Linq so I will be sure to research that asap. For my curiosity's sake is this possible to do without it? Also if I were to use an `if` statement for opening up the new "edit_form" if the search was returned as true would that work also? – zac Apr 12 '13 at 20:07
  • 1
    @zac yes, it is possible to do this, but you would have to loop through the list. And that's super lame. Use the special sauce C# gives you to save time! Unless you're just doing it to learn it. – Codeman Apr 12 '13 at 20:11
  • @zimdanen Down voting answers for absurd reasons, as you've just done, discourages learning by scaring users away from answering questions out of fear they will be down voted. If you want to help people learn -- answer questions, don't sharp-shoot those who are. – George Johnston Apr 12 '13 at 20:11
  • @George I appreciate it. I've been around for awhile, and this behavior has been around for awhile. It happens :) – Codeman Apr 12 '13 at 20:17
1

If you're unfamiliar with LINQ like I am you could use a foreach loop. Something like this:

String nameToMatch = textBox1.text; //read from the text box
foreach (Object obj in Teams) 
{
   MyTeam team = (MyTeam)obj; //MyTeam is an object you could write that would store team information.
   if (team.TeamName.ToUpper() == nameToMatch.ToUpper()) //case insensitive search.
   {
       FormTeam frmTeam = new FormTeam(team); //windows form that displays team info.
       frmTeam.Visible = true;
       break; //if team names are unique then stop searching.
   }
}

Worst case senario is pretty bad, but for me, at least, it's easier to get my head around than LINQ. Good Luck, hope that helps.

Amy Marty
  • 46
  • 4
  • upon scanning again I am not to sure where you are reading in from the textbox here. I understand the if statement is where you are launching a new form. But what exactly is "MyTeam"? – zac Apr 12 '13 at 20:32
  • Sorry I should have been more specific about that. "MyTeam" is an object or class that you could write that would hold team information. If you have a class like that. The read from the textbox would occur before the foreach loop. – Amy Marty Apr 12 '13 at 20:35
  • ahh that makes more sense. I have already created a class it was just slightly confusing as I didn't realise that the textbox would be read in automatically. Is there anything I have to write for this to work. as some of the commands appear to be not working. i.e when I wanted to make an arraylist I needed to input `using System.Collections` do I need to write something like that for this piece of code? – zac Apr 12 '13 at 20:49
  • Oh yeah, "FormTeam" would be another windows form that you would define that would display the team information. You just add another windows form to your project and name it "FormTeam" and add the components to it that you need. Also overload the constructor to take a parameter that populates the data fields you defined in "FormTeam" – Amy Marty Apr 12 '13 at 20:53
0

You can use some codes like this to fill your arraylist:

    // ArrayList class object
    ArrayList arrlist = new ArrayList();

    // add items to arrlist collection using Add method
    arrlist.Add("item 1");
    arrlist.Add("item 2");
    arrlist.Add("item 3");
    arrlist.Add("item 4");
    arrlist.Add("item 5");

and use some codes like this to search in your arraylist

string teamName= this.txtTeamName.Text;
// for loop to get items stored at each index of arrlist collection
for (int i = 0; i < arrlist.Count; i++)
{
    if(arrlist[i].toString()==teamName)
      // open a new form for show the found team details
}

it is a good practice to change the cunstractor of your "Team Details" form to get a "team name"

frmTeamDetails(team myteam)

then use this code in the above FOR statement:

frmTeamDetals frm=new frmTeamDetals(teamName);
frm.ShowDialog();
Ramin Bateni
  • 16,499
  • 9
  • 69
  • 98