3

我正在尝试从组合框中检索选定的项目,但我无法让它工作。

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;
    }
}

谢谢你。

4

4 回答 4

3

据我所知,您form.comboBox1.SelectedItem.ToString()在创建Form1. 这意味着cpuCount变量在表单创建后立即初始化,到目前为止,您还没有机会用鼠标更改所选项目。

如果要在更改后检索组合框的值,可以使用SelectedIndexChanged事件。

于 2013-03-25T12:15:44.787 回答
3

问题是您的ComboBox. 您创建了表单,然后在没有先前的用户交互的情况下,您想要SelectedItem在那个时刻获得 null 。

当您创建 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 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());
}
于 2013-03-25T12:16:42.517 回答
2

首先,添加一个 Form_Load 事件并将您的代码放入处理程序中。(使用构造函数进行属性初始化和其他变量初始化)

private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.SelectedItem= 5; // This will set the combo box to index 5
string cpuCount = this.comboBox1.SelectedText; // This will get the text of the selected item
}

所以你在 cpuCount变量中的索引 5 处获得 item 的值。

selected子句在您选择某些内容后为您提供值,默认情况下(当您运行您的应用程序时)comoboBox 中没有选择任何内容,因此,它将值显示为 null,在选择项目后您可以使用组合框的 selectedItem,selectedIndex 、 selectedText 和 selectedValue 属性。

您还可以使用数据绑定在组合框中显示项目,在我看来,这比手动添加项目更好。

数据绑定您可以使用的组合框,

// Bind your combobox to a datasource, datasource can be a from a database table, List, Dataset, etc..

 IDictionary<int, string> comboDictionary = new Dictionary<int, string>();
            comboDictionary.Add(1, "first");
            comboDictionary.Add(2, "second");
            comboDictionary.Add(3, "third");
            comboBox1.DataSource = comboDictionary;
            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";

// 

现在您可以在这里使用combobox1.SelectedIndex 来浏览数据源中的项目集合:) 当您使用combobox1.SelectedValue 时,它​​会根据您的键为您提供值。希望这可以帮助。

于 2013-03-25T12:35:51.370 回答
0

您的 ComoboBox 中没有项目。所以它不会正确返回。制作表单对象后,您正在访问组合框选定值权限。

如果它 comboBox 有项目,那么什么都没有被选择。默认情况下,在组合框中没有选择任何内容。你需要设置它。用这个。它返回什么?设置 comboBox.SelectedIndex 然后获取 selectedItem。

int selectedIndex = form.comboBox1.SelectedIndex;

尝试这个。在 ComboBox 中添加一些项目,然后获取 selectedItem。

Form1 form = new Form1();
form.comboBox1.Add("Item 1");
form.comboBox1.Add("Item 2");
form.comboBox1.Add("Item 3");
form.comboBox1.SelectedIndex = 1;
string cpuCount = form.comboBox1.SelectedItem.ToString();
于 2013-03-25T12:17:49.407 回答