我列出了我的班级。我如何通过索引访问该类?

时间:2015-05-14 15:26:03

标签: c# list

继承我的代码:

namespace Visual_Debugger_Beta
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            label1.Text = "Input Modules (" + Input_Hierarchy.Items.Count + " Items)";
            label2.Text = "Output Modules (" + Output_Hierarchy.Items.Count + " Items)";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open Image and put it to Image box.
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "png (*.png)|*.png|jpg (*.jpg)|*.jpg|bmp (*.bmp)|*.bmp";

            if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName.Length > 0)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Image = Image.FromFile(ofd.FileName);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void projectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //New Project
            pictureBox1.Image = null;
        }

        private void inputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Add Input
            Input_Hierarchy.Items.Add(new mod_input(false, true));
            label1.Text = "Input Modules (" + Input_Hierarchy.Items.Count + " Items)";
        }

        private void outputToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Add Output
            Output_Hierarchy.Items.Add(new mod_output(false, true));
            label2.Text = "Output Modules (" + Output_Hierarchy.Items.Count + " Items)";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Input_Hierarchy.Items.Clear();
            label1.Text = "Input Modules (" + Input_Hierarchy.Items.Count + " Items)";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Output_Hierarchy.Items.Clear();
            label2.Text = "Output Modules (" + Output_Hierarchy.Items.Count + " Items)";
        }

        public void eventsUpdate()
        {
            bool a;
            bool t;
            a = Input_Hierarchy.SelectedItem.getIsActive();

        }
    }

    public class mod_input
    {
        public static bool isactive = false;
        public static bool toggle = true;

        public bool getIsActive()
        {
            return isactive;
        }

        public bool getToggle()
        {
            return toggle;
        }

        public mod_input(bool a, bool t)
        {
            isactive = a;
            toggle = t;
        }
    }

    public class mod_output
    {
        public bool isactive = false;
        public bool toggle = true;

        public mod_output(bool a, bool t)
        {
            isactive = a;
            toggle = t;
        }
    }
}

现在,我有一个错误,那就是它没有找到我编码的getIsActive()。我知道这需要5分钟来解决,但我只是不能。

2 个答案:

答案 0 :(得分:0)

使用类似的东西

var a =Input_Hierarchy.SelectedItem != null ?
    ((mod_input)Input_Hierarchy.SelectedItem).getIsActive() : false

答案 1 :(得分:0)

问题是SelectedItem属性将项目公开为对象,因为它不是通用列表。在尝试访问属性之前,您需要强制转换为类类型:

bool a;
bool t;
mod_input input = Input_Hierarchy.SelectedItem as mod_input;
a = input != null && input.getIsActive();