加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Combobox SelectedItem无法正常工作

发布时间:2020-12-16 01:53:54 所属栏目:百科 来源:网络整理
导读:我正在尝试从组合框中检索所选项目,但我无法让它工作. Form1 form = new Form1();string cpuCount = form.comboBox1.SelectedItem.ToString(); 现在,这不会返回任何东西.但是,如果我在InitializeComponent()中插入此代码,它会选择index = 3的项目,并返回该项
我正在尝试从组合框中检索所选项目,但我无法让它工作.

Form1 form = new Form1();
string cpuCount = form.comboBox1.SelectedItem.ToString();

现在,这不会返回任何东西.但是,如果我在InitializeComponent()中插入此代码,它会选择index = 3的项目,并返回该项目.

comboBox1.SelectedIndex = 3;

为什么它会像这样?如果我现在选择索引= 5的示例项,它仍然会认为所选项是索引= 3的项.

———-我想我应该扩展以向您展示我的代码的外观.

Form1 – 将所有项目添加到组合框中.

public partial class Form1 : Form
{
    Profile profile = new Profile();
    public Form1()
    {
        InitializeComponent();
        Profile profile = new Profile();
        string[] prof = profile.getProfiles();
        foreach (var item in prof)
        {
            comboBox5.Items.Add(Path.GetFileNameWithoutExtension(item));
        }

        int ram = 1024;
        for (int i = 0; i < 7; i++)
        {
            comboBox4.Items.Add(ram + " GB");
            ram = ram * 2;
        }

        int vram = 512;
        string size;
        for (int i = 0; i < 5; i++)
        {
            if(vram > 1000)
            {
                size = " GB";
            }
            else
            {
                size = " MB";
            }
            comboBox2.Items.Add(vram + size);
            vram = vram * 2;
        }

        for (int i = 1; i < 5; i++)
        {
            comboBox1.Items.Add(i * 2);
        }

        for (int i = 0; i < 5; i++)
        {
            comboBox3.Items.Add(i * 2);
        }

        private void button3_Click(object sender,EventArgs e)
        {
            string current = profile.currentProfile();
            profile.saveProfile(current);
        }

    }

所以,button3是我的“保存”按钮.
这是我的“个人资料” – 类

class Profile
{
    public string folder { get; set; }
    public Profile()
    {
        this.folder = "Profiles";
        if (!File.Exists(folder))
        {
            Directory.CreateDirectory(folder);
            File.Create(folder + "/default.cfg").Close();
        }
    }

    public string[] getProfiles()
    {
        string[] files = Directory.GetFiles(folder);
        return files;
    }

    public void saveProfile(string filename)
    {
        Form1 form = new Form1();
        string cpuCount = "cpuCount=" + form.comboBox1.SelectedItem;
        string RAM = "maxRAM=" + form.comboBox4.SelectedItem;
        string VRAM = "maxVRAM=" + form.comboBox2.SelectedItem;
        string threads = "cpuThreads=" + form.comboBox3.SelectedItem;
        string path = folder + "/" + filename;
        StreamWriter sw = new StreamWriter(path);
        string[] lines = { cpuCount,RAM,VRAM,threads };

        foreach (var item in lines)
        {
            sw.WriteLine(item);
        }



        sw.Close();

    }

    public string currentProfile()
    {
        Form1 form = new Form1();
        string selected = form.comboBox5.SelectedValue + ".cfg".ToString();
        return selected;
    }
}

谢谢.

解决方法

问题是您的ComboBox中没有选择任何内容.您创建表单然后,在没有先前的用户交互的情况下,您希望获得当时为null的SelectedItem.

当您创建ComboBox控件并使用项填充它时,SelectedItem属性为null,直到您以编程方式设置它(通过使用例如comboBox1.SelectedIndex = 3)或通过用户与控件的交互.在这种情况下,您没有做上述任何事情,这就是您提出上述错误的原因.

编辑基于编辑的问题
像这样更改你的代码:
首先更改saveProfile方法,以便将写入的四个字符串传递给文本文件.请注意,您也可以传递表单的引用,但我不建议您这样做.所以改变这样的方法:

public void saveProfile(string filename,string cpuCount,string RAM,string VRAM,string threads)
    {
        string path = folder + "/" + filename;
        using(StreamWriter sw = new StreamWriter(path)) 
        {
             sw.WriteLine("cpuCount=" + cpuCount);
             sw.WriteLine("maxRAM=" + RAM );
             sw.WriteLine("maxVRAM=" + VRAM );
             sw.WriteLine("cpuThreads=" + threads);
        }        
    }

然后从button3点击事件处理程序调用它,如下所示:

private void button3_Click(object sender,EventArgs e)
{
            string current = profile.currentProfile();
            string cpuCount = this.comboBox1.SelectedItem.ToString();
            string RAM =  this.comboBox4.SelectedItem.ToString();
            string VRAM = this.comboBox2.SelectedItem.ToString();
            string threads = this.comboBox3.SelectedItem().ToString();
            profile.saveProfile(current,cpuCount,threads);
}

或者

private void button3_Click(object sender,EventArgs e)
{
            string current = profile.currentProfile();
            profile.saveProfile(current,this.comboBox1.SelectedItem.ToString(),this.comboBox4.SelectedItem.ToString(),this.comboBox2.SelectedItem.ToString(),this.comboBox3.SelectedItem().ToString());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读