- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从组合框中检索所选项目,但无法正常工作。
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/
我正在尝试将我的 ViewModel (ComboBox) 中的 SelectedItems (ListView) 和 SelectedItem 或 SelectedCategory 多重绑定(bin
一两天以来,我一直试图找出一个奇怪的 WPF 错误。我有一个 ListBox 控件,它绑定(bind)到 PolicyId 的 ObservableCollection,它是一个实现 INotifyP
我有一个 ASP DropDown,声明如下: 数据是这样填的: DropDown1.DataSource = source; DropDown1.DataTextField = "Key"; D
我有一个绑定(bind)到 ListCollectionView 的 ListBox,有一次我在列表框中选择了一个项目,然后在未来我重新创建了 ListCollectionView,但是 ListBo
我有一个简单的组合框,其中复选框作为项目。如何防止项目的实际选择。用户应该只能选中或取消选中复选框? 目前,如果我单击一个元素(而不是内容或检查本身),它将被选中。这样做的问题是:ComboBox 的
有没有办法知道在 Windows 资源管理器中选择了哪个文件?我一直在看这里发布的教程 Idiots guide to ...但描述的行动是: 徘徊 语境 菜单属性 拖 拖放 我想知道是否有在选择文件
我有一个 WPF 数据网格。 DataGrid 绑定(bind)到 IList。该列表有许多项目,因此 DataGrid MaxHeight 设置为预定义值,并且 DataGrid 自动显示滚动条。选
好的,已经使用 WPF 有一段时间了,但我需要一些帮助。 我有一个 ComboBox如下所示: 每当我离开选项卡 1 然后回到它时,选择就会被删除。
示例代码如下: ddlCat.Items.Insert(0, new ListItem("Item1", "1")); ddlCat.Items.Insert(1, new ListItem("It
我想使用 SelectedItem 将选择设置为代码中的组合框。 我只能通过使用 SelectedValue 让它工作。 SelectedItem 将在堆栈跟踪的顶部抛出一个空引用异常: 在 Atta
这是我在这个很棒的平台上的第一个问题,是的,我在这里搜索了如何执行此操作,在我的情况下很少见,因为它应该可以工作,但我从 SelectedItem 中得到的只是一个字符串(它的内容,这是一个 Text
我有一个ListBox,其中包含填充TextBoxes 的项目。当从 ListBox 中进行选择时,如何识别选择的文本字符串。这是我的 ListBox XAML 代码:
我有一个列表框,其中填充了来自 ImageDomainService(RIA 服务)的列表。我想从 ListBox 中选择一张图像并在其旁边显示该图像的较大版本。图像单独存储在/images/文件夹中
我有一个绑定(bind)到 ObservableCollection 的 ListView ItemsSource。我通过 MVVM 添加了一个属性来跟踪 ListView.SelectedItem。
我正在尝试从 QTableView 小部件(下面复制的片段)返回选定行的向量,但是返回的值与选择不对应,我相信我不了解 QModelIndexList/QModelIndex 与 QTableView
我试图寻找答案,但我没有任何运气。基本上我有一个 ListView ,它绑定(bind)到从 View 模型返回的集合。我将 ListView 的选定项目绑定(bind)到我的 ListView 中的
我有一个实现 INotifyPropertyChanged 的 View 模型.在这个 viewModel 上有一个名为 SubGroupingView 的属性。 .此属性绑定(bind)到组合框的选
我有一个 ComboBox,它的 ItemsSource 绑定(bind)到静态 List的选项。 ComboBox 是绑定(bind)到 CustomObject 类的表单的一部分,该类的属性之一是
我有一个将 SelectedItem 绑定(bind)到 ViewModel 的 ComboBox。 当用户在 View ComboBox 中选择一个新项目时,我想显示一个提示并验证他们是否要进行更
我有一个 ViewModel(其结构的伪代码): class ViewModel { public List Packages { get; set; } } enum Type {
我是一名优秀的程序员,十分优秀!