1

まあ、私はC#にほとんど慣れていないので、マルチレベル配列がC#でどのように機能するかわかりませんでした。

次のようなメニューを含むtreeViewを作成しました:


  • メニュー_1
  • --child_1.1
  • --child_1.2
  • ----子_1.2.1
  • ----子_1.2.2
  • ----子_1.2.3
  • --child_1.3
  • メニュー_2
  • --child_2.1
  • --child_2.2
  • ----子_2.2.1

すべての MenuItem には、次のような 6 つのプロパティ / 属性 / 値が必要です。

Item = { ID:int , "NAME:String , POSITION:String , ACTIVE:Bool , ACTION:bool , PATH:string }

それで :

Menu_1 = { 1, "File", "1", true, false, "" }
child_1.1 = { 2, "Open", "1.1", true, true, "./open.exe" }

... 等々

これまでのところ :

私はいくつかの String-Arrays ( String[] ) を手動で設定して、eat menuItem に情報を入力しました。

String[] Item_1 = {"1", "File", "1", "1", "0", ""};
String[] Item_2 = ...

...

今、これらすべての String-Array をArrayList[]内に配置し、各アイテムの「POSITION」値を使用して Sort() それらを配置したい ( Item_1 [ 2] )

また、SQLテーブルから値を読み取って、アイテム自体の配列を動的に作成するコードが必要です。これらの配列は、私が今行ったように単なる文字列配列であってはなりません。ID を int のままにし、ACTIVE および ACTION 値を bool のままにしたいからです。

最終製品は次のようになります。

MenuItems = ArrayList(
    item_1 = Array(Int, String, String, Bool, Bool, String)  // \
    item_2 = Array(Int, String, String, Bool, Bool, String)  //  \
    item_3 = Array(Int, String, String, Bool, Bool, String)  //  / all sortet by the 3rd value, the position )
    item_4 = Array(Int, String, String, Bool, Bool, String)  // /
    ...
    )
)

私を助けてくれる皆さんに感謝します。

4

2 に答える 2

0

配列だけではできません。柔軟にしたい場合は、サブアイテムを配列またはリストとして含むクラスを作成します

public class MenuItem
{
    public MenuItem()
    {
        SubItems = new List<MenuItem>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public bool Active { get; set; }
    public bool Action { get; set; }
    public string Path { get; set; }
    public List<MenuItem> SubItems { get; private set; }
}

次に、このようなサブアイテムを追加できます

var child_1_1 = new MenuItem{ 2, "Open", "1.1", true, true, "./open.exe" };
Menu_1.SubItems.Add(child_1_1);
于 2012-03-08T14:50:03.593 に答える
0

C# 2.0 以降を使用していると仮定すると、ArrayList の代わりにジェネリック リストを使用し、単なる配列ではなくクラス コンテナーを使用します。.NET 3.5 以降を使用していると仮定すると、並べ替えにも LINQ を使用することをお勧めします。

まず、メニュー項目の型のクラス コンテナーを作成します。

public class MenuItem
{
    public int ID {get;set;}
    public string Name {get;set;}
    public string Position {get;set;}
    public bool Active {get;set;}
    public bool Action {get;set;}
    public string Path {get;set;}
} 

次に、このクラスのインスタンスをジェネリック List に格納できます。

var items = new List<MenuItem>();
items.Add(new MenuItem{ID="1", Name="File", Position="1", Active=true, Action=false, Path=""});

次に、このリストを位置で並べ替えるには、LINQ を使用できます。

var sorted = items.OrderBy(i => i.Position);
于 2012-03-08T14:41:21.003 に答える