c# - 如何使用字符串创建数组 - 字符串、字符串、颜色 Clr = Color.Black?
问题描述
有一个数组。
我们遍历数组行并发送到方法。
我需要将“Color.Black”和其他颜色传递给该方法。
问题。
1.如何制作这样一个可以存储“Color Clr”的数组?
2. 或者其他方式来完成任务?
代码
public string[,] FillArray()
{
Keywords keyw = new Keywords();
string[,] ar = { /*Поле - `keywords` // Поле - `typeMatchCollection` // Поле - `color`*/
{ keyw.Keywords_prop, "keywords", "Blue"}
, {keyw.Types_prop, "types", "DarkCyan" }
, {keyw.Comments_prop, "comments", "Green" }
// , {keyw.Strings_prop, "strings", "Brown" }
, {keyw.Strings_prop, "strings", Color Clr = Color.Black }
};
return ar;
}
public void Backlight(MatchCollection matchCollection, Color Clr)
{
foreach (Match m in matchCollection)
{
codeRichTextBox.SelectionStart = m.Index;
codeRichTextBox.SelectionLength = m.Length;
codeRichTextBox.SelectionColor = Clr;
// codeRichTextBox.SelectionColor = Color.Brown;
// codeRichTextBox.SelectionColor = Color.FromArgb(255, 100, 10, 16);
// codeRichTextBox.SelectionColor = Color.FromArgb(color);
}
}
更新 1
此更新基于来自@jdweng -> Use: 的评论object [,] ar
。
我尝试使用object [,] ar
结果。
public void General_2()
{
object[,] arr;
arr = FillArray_1();
int rows = arr.GetUpperBound(0) + 1;
int columns = arr.Length / rows;
// или так
// int columns = mas.GetUpperBound(1) + 1;
for (int i = 0; i < rows; i++)
{
string keywords = arr[i, 1].ToString();
string typeMatchCollection = arr[i, 2].ToString();
Color color = arr[i, 3] as Color;
MatchCollection matchCollection = null;
matchCollection = CreateCollectionMatchcollection(keywords, typeMatchCollection);
Backlight_1(matchCollection, color);
}
}
我在行中有错误Color color = arr [i, 3] as Color;
更新 1. 问题。
1.如何Color
从数组中获取?
解决方案
您可以使用类似的项目列表来代替数组。
定义一个类 Item 并根据需要更改此名称:
public class Item
{
public string Property { get; set; }
public string Description { get; set; }
public Color Color { get; set; }
}
根据您的需要更改成员名称。
像这样使用它:
var list = new List<Item>();
list.Add(new Item
{
Property = keyw.Strings_prop,
Description = "strings",
Color = Color.Black
});
然后将颜色传递给方法使用:
Backlight(collection, list[index].Color);
推荐阅读
- powershell - 使用 powershell 生成 SQL 创建表脚本
- node.js - Node.js v11.9.0 上的 ECMAScript 模块
- mysql - 将坐标点沿某个方向移动 X 米
- c# - 带有 ASP .NET CORE 2.0 的 jTable 插件
- javascript - 循环数组不打印在单行中
- mysql - 使用 MySQL 一次返回列中的第一个唯一值
- java - 具有相同凭据的多个 Maven 存储库
- sql - 执行查询后,显示基数估计警告
- python - Python pandas 数据框拆分
- c - ncurses 的 has_colors() 返回 false。问题是什么?