我正在尝试从组合框中检索所选项目,但无法正常工作。
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/