当用户单击树视图节点时,tabcontrol发生更改 [英] tabcontrol changing when user clicks on treeview node

查看:126
本文介绍了当用户单击树视图节点时,tabcontrol发生更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的应用程序的概述:

Here is an overview of my application:

这基本上是一本烹饪书.用户可以创建一个食谱,然后在该食谱中创建食谱.我有一个Cookbook类和一个Recipe类. CookBook会跟踪与食谱相关的食谱,Recipe会跟踪食谱的内容.

It is basically a cook book. The user can create a cookbook and then create recipes within that cookbook. I have a class CookBook and a class Recipe. CookBook keeps track of what recipes are associated with the cook book and Recipe keeps track of the contents of the recipe.

对于UI,我有一个树形视图,其中将显示CookBook中的食谱.我有一个TabControl,它将显示与每个配方关联的选项卡.这些选项卡由用户在运行时动态创建.

For the UI, i have a treeview where it will display the recipes that are in the CookBook. I have a TabControl that will display the tabs associated with each recipe. The tabs are dynamically created by the user during run time.

当用户单击某个食谱时,我希望TabControl显示与该特定食谱相关的选项卡.当用户单击其他配方时,我希望先前选择的选项卡消失而当前选择的选项卡显示. (旁注:我将稍后将标签和内容的内容保存到文件中,以进行保存)

我希望Recipe类包含有关TabControl的详细信息(配方中有多少个选项卡,每个选项卡的标题,选项卡的内容).但是我不希望Recipe负责创建Tabs或TabControl.

I want the Recipe class to contain the details about the TabControl (how many tabs there are for the recipe, title of each of the tabs, contents of the tabs). But i do not want Recipe to be responsible for creating the Tabs or TabControl.

我的问题是,我如何完成上面的粗体部分?人们对这类问题有何看法和经验?这类问题的最佳做法是什么?

My questions is, how do i accomplish the bolded section above? What are people's opinions and experience with this type of problem? What are the best practices for this type of problem?

谢谢!

推荐答案

我希望以下代码能给您一个想法

I hope that the following code gives you an idea

public Form1()
    {
        InitializeComponent();

        Recipe r1 = new Recipe() { Text = "Re1" };
        Recipe r2 = new Recipe() { Text = "Re2" };
        Recipe r3 = new Recipe() { Text = "Re3" };


        listBox1.Items.Add(r1);
        listBox1.Items.Add(r2);
        listBox1.Items.Add(r3);


        tabControl1.TabPages.Add(new AdvancedTabPage() { Recipe = r1,Text=r1.ToString() });
        tabControl1.TabPages.Add(new AdvancedTabPage() { Recipe = r2, Text = r2.ToString() });
        tabControl1.TabPages.Add(new AdvancedTabPage() { Recipe = r3, Text = r3.ToString() });

        listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);

    }

    void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedItem != null)
            foreach (AdvancedTabPage ad in tabControl1.TabPages)
            {
                if (ad.Recipe == listBox1.SelectedItem)
                {
                    tabControl1.SelectedTab = ad;
                    break;
                }
            }

    }

    public class AdvancedTabPage : System.Windows.Forms.TabPage
    {
        public Recipe Recipe{get;set;}


    }

    public class Recipe
    {
        public string Text = "";

        public override string ToString()
        {
            return Text;
        }
    }

这篇关于当用户单击树视图节点时,tabcontrol发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆