I basically have criminals with different variables that are added to a report and it looks something like this:
reports.Add(new Report(tbxCriminalSuspicion.Text,
(new Criminal(Ordformatering(tbxName.Text), Ordformatering(tbxSex.Text), int.Parse(tbxAge.Text),
double.Parse(tbxHeight.Text), Ordformatering(tbxHairColor.Text)))));
Then I create another string that I want to use to search between the criminals. You type into a textbox and it is placed into a list by:
string allSearchWords = tbxSearchWords.text;
List<string> splitSearchWords = new List<string>();
allSearchWords = allSearchWords.Replace(" ", "");
splitSearchWords = allSearchWords.Split(',').ToList();
Now I've got a list of words and I'm able to search my criminals via:
splitSearchWords.All((criminal).Contains);
However, in the case of the situation looking as:
Criminal 1: 18 years old, 176 cm
Criminal 2: 34 years old, 180 cm
Both criminals will show up in the Listbox even though only one is 18 years old. In other words, is it possible to search for an exact match using a list as a pattern? Can attach some code.
List<Report> reports = new List<Report>();
List<string> splitSearchWords = new List<string>();
private void btnSearch_Click(object sender, EventArgs e)
{
lbxLista.Items.Clear();
if (string.IsNullOrWhiteSpace(tbxSearchWords.Text))
{
MessageBox.Show("Vänligen fyll i dina sökord innan du söker enligt \"Sökord, sökord, sökord, ...\"",
"Felinmatning", MessageBoxButtons.OK, MessageBoxIcon.Information);
//Just a check so that the searchbox isn't empty!!!!
}
else
{
string allSearchWords = tbxSearchWords.Text;
allSearshWords = allSearshWords.Replace(" ", "");
splitSearchWords = allSearchWords.Split(',').ToList();
for (int i = 0; i < reports.Count; i++)
{
if(ContainsSearchWord(reports[i].GetData(), splitSearchWords))
{
lbxLista.Items.Add(reports[i].ToString());
}
}
}
}
public static bool ContainsSearchWord(string stringToTest, List<string> searchedWords)
{
return searchedWords.All((stringToTest).Contains);
}
The Classes, Report and Criminal:
class Report
{
public string GetData()
{
return "" + criminal.criminalName + " " + criminal.criminalSex +
" " + criminal.criminalAge + " " +
criminal.criminalHeight + " " + criminal.criminalHairColor;
}
}
class Criminal
{
private string privatCriminalName;
private string privatCriminalSex;
private int privatCriminalAge;
private double privatCriminalHeight;
private string privatCriminalHairColor;
public Criminal(string mCriminalName, string mCriminalSex,
int mCriminalAge, double mCriminalHeight, string mCriminalHeightColor)
{
this.criminalName = mCriminalName;
this.criminalSex = mCriminalSex;
this.criminalAge = mCriminalAge;
this.criminalHeight = mCriminalHeight;
this.criminalHairColor = mCriminalHeightColor;
}
//Then I have properties to access these from outside the class but removed due to being messy and lengthy in post.
}
Pardon the long post, after +10 hours I'm just lost.