c# - 组合框 SelectedItem 无法正常工作

标签 c# winforms combobox

我正在尝试从组合框中检索所选项目,但无法正常工作。

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

现在,这不会返回任何东西。但是,如果我将此代码插入我的 InitializeComponent(),它会选择索引 = 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) 或通过用户与控件的交互。在这种情况下,您没有执行上述任何操作,这就是您收到上述错误的原因。

EDIT 基于已编辑的问题 像这样更改您的代码: 首先更改 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 Click 事件处理程序中调用它:

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, RAM, VRAM, 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());
}

关于c# - 组合框 SelectedItem 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15614596/

相关文章:

c# - WPF ComboBoxItem Style 在 Combobox 获得焦点后加载

vba - 使 PowerPoint 组合框正确列出项目的问题

c# - C# 中的地理定位

c# - LINQ 中的嵌套匿名类型

.net - 如何阻止 form.showdialog 在 DotNet 中锁定祖父窗体?

c# - 为什么从数据集中使用绑定(bind)源向 datagridview 添加数据是空白的?

c# - .NET WinForms 应用程序中的异常什么时候可以被吃掉而不被捕获或冒泡到 Windows 异常?

c# - 调用空函数需要多长时间?

c# - 调整窗口大小 C#

extjs - 将组合框中的日期与当前日期进行比较