【发布时间】:2020-11-19 15:30:24
【问题描述】:
我正在尝试在对象列表中搜索所述对象的特定“特征”
以下是为列表创建对象的类:
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;
}
为了便于阅读,已对不相关的代码进行了编辑。
【问题讨论】:
标签: c# list constructor contains