C# 搜索对象中特定字符串的自定义对象列表
C# Searching List of custom objects for specific string within object
我正在尝试在对象列表中搜索所述对象的特定“特征”
下面是为列表创建对象的类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dice_Roller_v3._14
{
public class ImageBase
{
public Bitmap Image { get; set; }
public string Name { get; set; }
}
public class ColorBase
{
public Color Color { get; set; }
public string Name { get; set; }
}
这是从构造函数创建对象列表的代码。
public partial class frmImage_Layer : Form
{
//Create lists for storing colors and images
public List<ColorBase> ImgColor = new List<ColorBase>();
//public List<String> ImgPath = new List<string>();
public List<ImageBase> ImgBase = new List<ImageBase>();
public frmImage_Layer()
{
InitializeComponent();
}
此代码包含如何将列表分配给的示例
private void btnImageControl_Control_Click(object sender, EventArgs e)
{
if (ErrorHandling("control") == true)
{
return;
}
if (radImageControl_Color_Add.Checked == true)
{
cpImageControl.ShowDialog();
ImgColor.Add(new ColorBase(){Name = txtImageControl_Name.Text,Color = cpImageControl.Color});
ArraysChanged();
}
else if (radImageControl_Color_Delete.Checked == true)
{
}
else if (radImageControl_Part_Add.Checked == true)
{
}
else if (radImageControl_Part_Delete.Checked == true)
{
}
}
public void ArraysChanged()
{
//This is not related to the original question, but if someone knows how to fill a
//Combo-box with the list it would save me quite some time
cmbImageControl_Color.Items
}
下面是我试图搜索文本的地方,因为简单地键入 ImgColor.Contains("text") 在此上下文中不起作用,我已经清空了相关的 if 语句。
public bool ErrorHandling(string Type)
{
if (Type == "control")
{
if (txtImageControl_Name.Text == "")
{
MessageBox.Show("Name must not be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
else if ()
//Want to check if ImgBase contains a name equal to the text in txtImageControl_Name.Text
{
MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
else if ()
// Want to check if ImgColor contains a name equal to the text in txtImageControl_Name.Text
{
MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
}
return false;
}
不相关的代码已被编辑以提高可读性。
也许这就是您要找的
if (ImgBase.Any(x => x.Name == txtImageControl_Name.Text))
...
else if (ImgColor.Any(x => x.Name == txtImageControl_Name.Text))
...
我正在尝试在对象列表中搜索所述对象的特定“特征”
下面是为列表创建对象的类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Dice_Roller_v3._14
{
public class ImageBase
{
public Bitmap Image { get; set; }
public string Name { get; set; }
}
public class ColorBase
{
public Color Color { get; set; }
public string Name { get; set; }
}
这是从构造函数创建对象列表的代码。
public partial class frmImage_Layer : Form
{
//Create lists for storing colors and images
public List<ColorBase> ImgColor = new List<ColorBase>();
//public List<String> ImgPath = new List<string>();
public List<ImageBase> ImgBase = new List<ImageBase>();
public frmImage_Layer()
{
InitializeComponent();
}
此代码包含如何将列表分配给的示例
private void btnImageControl_Control_Click(object sender, EventArgs e)
{
if (ErrorHandling("control") == true)
{
return;
}
if (radImageControl_Color_Add.Checked == true)
{
cpImageControl.ShowDialog();
ImgColor.Add(new ColorBase(){Name = txtImageControl_Name.Text,Color = cpImageControl.Color});
ArraysChanged();
}
else if (radImageControl_Color_Delete.Checked == true)
{
}
else if (radImageControl_Part_Add.Checked == true)
{
}
else if (radImageControl_Part_Delete.Checked == true)
{
}
}
public void ArraysChanged()
{
//This is not related to the original question, but if someone knows how to fill a
//Combo-box with the list it would save me quite some time
cmbImageControl_Color.Items
}
下面是我试图搜索文本的地方,因为简单地键入 ImgColor.Contains("text") 在此上下文中不起作用,我已经清空了相关的 if 语句。
public bool ErrorHandling(string Type)
{
if (Type == "control")
{
if (txtImageControl_Name.Text == "")
{
MessageBox.Show("Name must not be blank", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
else if ()
//Want to check if ImgBase contains a name equal to the text in txtImageControl_Name.Text
{
MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
else if ()
// Want to check if ImgColor contains a name equal to the text in txtImageControl_Name.Text
{
MessageBox.Show("Name cannot be identical to a name already in place", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return true;
}
}
return false;
}
不相关的代码已被编辑以提高可读性。
也许这就是您要找的
if (ImgBase.Any(x => x.Name == txtImageControl_Name.Text))
...
else if (ImgColor.Any(x => x.Name == txtImageControl_Name.Text))
...