gpt4 book ai didi

C# 通过组合框使用 SelectedIndexChanged 更改带有文本文件中文本的标签

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:32 26 4
gpt4 key购买 nike

在组合框中,我可以将所有转换加载到组合框中。但是,当我从组合框中单击一个转换时,由于某种原因它会将数据更改为 null。我不确定是否有人可以在不查看我所有代码的情况下帮助我。但我认为值得一试。我正在尝试选择一个转换,即英尺到英里,并将“英尺”放在第一个标签中,将“英里”放在第二个标签中。下面是组合框点击事件的代码:

public void CmboBxConversion_SelectedIndexChanged(object sender, EventArgs e)
{
//This is not working. If I were to say change the conversion.From to "hello world" the label text does change to hello world.
//But, as is it is it changes it to a null.
Conversion conversion = new Conversion();

Lbl1stConv.Text = conversion.From; //these are labels
LblSecondConv.Text = conversion.To;
}

//this is my Conversion class
public class Conversion
{
public string From { get; set; }
public string To { get; set; }
public decimal Multiplier { get; set; }

public string GetDisplayText(string sep)
{
return From + ("|") + To + ("|") + Multiplier;
}

public string GetCmboBxText(string sep)
{
return From + (" to ") + To;
}
}

//this is how I am loading the combo box
private void Conversion_Load(object sender, EventArgs e)
{
conversions = ConversionDB.GetConversions();
FillConversionComboBox();
}

//here is the method used to fill the combobox
private void FillConversionComboBox()
{
CmboBxConversion.Items.Clear();

foreach (Conversion c in conversions)
{
CmboBxConversion.Items.Add(c.GetCmboBxText("\n"));
}
}

//this is a data base class that the textfile is pulling data
public static class ConversionDB
{
public const string Path = @"..\..\Conversions.txt";
//public const string Path = Dir + "Conversions.txt";

public static List<ConversionList> GetConversions()
{


StreamReader textIn = new StreamReader(
new FileStream(Path, FileMode.Open, FileAccess.Read));
List<ConversionList> conversions = new List<ConversionList>();
while (textIn.Peek() != -1)
{
string row = textIn.ReadLine();
string[] columns = row.Split('|');
ConversionList conversion = new ConversionList();
conversion.From = columns[0];
conversion.To = columns[1];
conversion.Multiplier = Convert.ToDecimal(columns[2]);
conversions.Add(conversion);
}

textIn.Close();
return conversions;
}

最佳答案

我不确定这是否是您想要的,但它是一种更简单的方法,可为您提供相同的功能。我认为如果将组合框绑定(bind)到转化列表,会简单得多。 selecteditem 和 selectedvalue 将更易于使用。

    public partial class Form1 : Form
{
private List<Conversion> conversions=new List<Conversion>();
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
loadconversions();
//foreach (Conversion c in conversions)
//{this.comboBox1.Items.Add(c); }
comboBox1.DataSource = conversions;
this.comboBox1.DisplayMember = "GetCmboBxText";

}

private void loadconversions()
{
fillconversions("feet","yards",3);
fillconversions("yard", "feet", 0.33m);
fillconversions("km", "mile", 0.54m);
fillconversions("mile", "km", 1.6m);
fillconversions("quarts", "gallons", 4);

}
private void fillconversions(string from, string to, decimal multiplier)
{
conversions.Add(new Conversion(from, to, multiplier));
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Conversion c = (Conversion)this.comboBox1.SelectedItem;
this.textBox1.Text = c.GetDisplayText;
}
}
public class Conversion
{
public string From { get; set; }
public string To { get; set; }
public decimal Multiplier { get; set; }

public Conversion(string from, string to, decimal multiplier)
{
From = from;
To = to;
Multiplier = multiplier;
}

public string GetDisplayText
{
get { return From + ("|") + To + ("|") + Multiplier; }
}

public string GetCmboBxText
{
get
{
return From + (" to ") + To;
}
}
}

关于C# 通过组合框使用 SelectedIndexChanged 更改带有文本文件中文本的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31578696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com