How can I set the same button events for ListBox (value) or tree indexes?

I have a C # Win Form as shown below. I also have some arrays as a menu for a list box.

public string[] ArrayMain = {"1.Water","2.Air","3.Soil","4.Fire"};
public string[] ArrayWater = {"1.Salty","2.Fresh", "3.Contaminated"};
public string[] ArraySalty = {"1.AA", "2.BB", "3.CC"};
public string[] ArrayFresh = {"1.DD", "2.EE", "3.FF"};
public string[] ArrayContaminated = {"1.XX", "2.YY", "3.ZZ"} ;
public string[] ArrayAir = {"1.Fresh", "2.Contaminated"};
public string[] ArraySoil = {"1.Normal", "2.Contaminated"};
public string[] ArrayFire = {"1.Low","2.Mid","3.High"};

      

When the application starts up in the main list loaded with the first array values.

public Form1()
{
    InitializeComponent();
    ListBoxMain.Items.AddRange(ArrayMain);
    listView1.Items.Add(ArrayMain[0])
    // User choose: 1.Water
}

      

If the user presses button 1, the application deletes the master list and loads with that selection. At this time the List-box is loaded with

ListBoxMain.Items.AddRange(ArrayWater);
listView1.Items.Add(ArrayMain[0])
// User choose: 1.Salty

      

Now, if the user clicks button 2, the application clears the main list, and the Menu Favorite List will contain values ​​such as:

1.Water 1.Salty 2.BB

Then the application will receive the first letter (number) with a fixed format, for example:

112

and send it to the WFC service. WFC services will know what to do with "112". My main problem is that I have to set all my buttons to the List-box index (or combo box as I say). Example if the user first clicks button 1 and then the user has to select this menu array in the List box. And again, if the user clicks button 1, then it is necessary to connect the 1st button "List Index" (new values).

In my previous question for this interesting problem, user @Eugene suggested that I could turn the logic into a tree view. And he stated that:

where the nodes contain items for the combo-box (key-string to show, value-value for later use). When you click buttons, you add the corresponding node to some kind of queue and load new elements. In leaf nodes, you will contain Action>, which will call the corresponding service method (for many parameters, this can be the same action)

I am trying to apply a lot of logic and I spend almost 10 days for the simulator to work as expected, but I have failed. I really like to have some information, so I can set my 1 events to ListBox (index) or Treview (nodes). If someone points at me, even if I set my buttons to 1 as I said above, resting will be easy for me.

Respectfully,

First launch of the application:

App Start

After clicking the user button: Pos Terminal Simulation

+3


source to share


1 answer


Thanks @Eugene. I did it. Here it is.

For Main;

public Form1()
{
    InitializeComponent();

    TreeNode tNode;
    //MAIN NODE 1
    tNode = treeView1.Nodes.Add("1.Water");
    tNode = treeView1.Nodes.Add("2.Air");
    tNode = treeView1.Nodes.Add("3.Soil");
    tNode = treeView1.Nodes.Add("4.Fire");

    // WATER NODE
    treeView1.Nodes[0].Nodes.Add("1.Salty");
    treeView1.Nodes[0].Nodes.Add("2.Fresh");
    treeView1.Nodes[0].Nodes.Add("3.Contaminated");

    // AIR NODE
    treeView1.Nodes[1].Nodes.Add("1.Fresh");
    treeView1.Nodes[1].Nodes.Add("2.Contaminated");

    // SOIL NODE
    treeView1.Nodes[2].Nodes.Add("1.Normal");
    treeView1.Nodes[2].Nodes.Add("2.Contaminated");

    // FIRE NODE
    treeView1.Nodes[3].Nodes.Add("1.Low");
    treeView1.Nodes[3].Nodes.Add("2.Mid");
    treeView1.Nodes[3].Nodes.Add("3.High");

    // SALTY NODE
    treeView1.Nodes[0].Nodes[0].Nodes.Add("1.AA");
    treeView1.Nodes[0].Nodes[0].Nodes.Add("2.BB");
    treeView1.Nodes[0].Nodes[0].Nodes.Add("3.CC");

    // FRESH NODE
    treeView1.Nodes[0].Nodes[1].Nodes.Add("1.DD");
    treeView1.Nodes[0].Nodes[1].Nodes.Add("2.EE");
    treeView1.Nodes[0].Nodes[1].Nodes.Add("3.FF");

    // CONTAMINATED NODE
    treeView1.Nodes[0].Nodes[2].Nodes.Add("1.XX");
    treeView1.Nodes[0].Nodes[2].Nodes.Add("2.YY");
    treeView1.Nodes[0].Nodes[2].Nodes.Add("3.ZZ");


    //Clear ListBox items
    ListBoxMain.Items.Clear();

    //Load ListBox First time
    foreach (TreeNode n in treeView1.Nodes)
    {
        ListBoxMain.Items.Add(n.Text);
    }
}

      

For buttons:

// BTN 1
private void Btn_1_Click(object sender, EventArgs e)
{
    int value = 1 - 1;
    int iCount = ListBoxMain.Items.Count;

    if (iCount > value)
    {
        string item = ListBoxMain.Items[value].ToString();
        TreviewNodesSelection(item, value);
    }
}

// BTN 2
private void Btn_2_Click(object sender, EventArgs e)
{
    int value = 2 - 1;
    int iCount = ListBoxMain.Items.Count;

    if (iCount > value)
    {
        string item = ListBoxMain.Items[value].ToString();
        TreviewNodesSelection(item, value);
    }
}

      

Then the TreviewNodesSelection was created:



//TreviewNodesSelection
private void TreviewNodesSelection(string item, int value)
{
    textBox1.Text = item;
    listBoxMenu.Items.Add(item);


    //Find Nodes first
    Traverse(treeView1.Nodes, item);

    //Clear ListBox items
    ListBoxMain.Items.Clear();

    //Get a First ChildNode via Parenet Name
    if (treeView1.SelectedNode.Nodes.Count != 0)
    {
        foreach (TreeNode v in treeView1.SelectedNode.Nodes)
        {
            ListBoxMain.Items.Add(v.Text);
        }
    }
    else
    {
        MessageBox.Show("Now you can start calculation...");
    }
}

      

And this is a TreeView Node Selection

private void Traverse(TreeNodeCollection nodes, string findtext)
{
    foreach (TreeNode node in nodes)
    {
        if (node.Text.ToString().Trim() == findtext)
        {
            node.TreeView.SelectedNode = node.NextNode;
            treeView1.SelectedNode = node;
            node.TreeView.Focus();
            //MessageBox.Show(node.Text + " is selected...");
        }
        Traverse(node.Nodes, findtext);
    }
}

      

I am learning a lot from /fooobar.com / ... so this is my small contribution.

respectfully

Pos Simulation

+2


source







All Articles