首页 > 解决方案 > 如何使用字符串创建数组 - 字符串、字符串、颜色 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从数组中获取?

标签: c#

解决方案


您可以使用类似的项目列表来代替数组。

定义一个类 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);

推荐阅读