本文介绍了尝试从读入的文本文件为每个按钮生成按钮,名称和数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,我仍然是C#的新手,我正在处理我的一项任务中的问题。我已经成功地将文本文件读入我的代码并将列表转移到列表框中。这只是为了测试目的。然后我能够为文本文件中的每个项目生成相同数量的按钮到面板。但是,我似乎无法将项目的名称变为按钮中的文本,然后将每个按钮/名称分配给正确的值,然后进行计算。我不确定我是否尝试结合两种完全不同的方式来接近这个结果,但任何帮助都会受到赞赏。 我尝试过的: 这是带有表单加载方法的表单类 public Form1( ) { InitializeComponent(); } List< appliances> ; allEntry = new List< appliances>(); public void Form1_Load(object sender,EventArgs e) { allEntry = ApplianceDA.GetAppliances(); DisplayButtons(); } public void DisplayButtons() { int product = 0; int startTop = 10; int startLeft = 10; foreach(all in allEntry中的设备) { 按钮appButton = new Button(); this.Controls.Add(appButton); appButton。 Text =(); appButton.Location = new System.Drawing.Point(startTop +(125 * product),startLeft); appButton.Height = 58 ; appButton.Width = 120; //appButton.Click + = AButton_Click(); pnlBox.Controls.Add(appButton); product ++; } } 这里是读取文本文件的ApplianceDA类 静态类ApplianceDA { 私有const字符串PATH =Appliances.txt; public static List< appliances> GetAppliances() { List< appliances> appliance = new List< appliances>(); StreamReader textIn = new StreamReader(new FileStream(PATH,FileMode.OpenOrCreate,FileAccess.Read)); while(textIn .Peek()!= -1) { string row = textIn.ReadLine(); string [] columns = row。拆分(','); 设备产品=新设备(列[0],Convert.ToInt64(列[1])); appliance.Add(product); } textIn.Close(); b $ b 返回设备; } Hello all, I'm still fairly new to C# and I'm working on an issue in one of my assignments. I've successfully been able to read a text file into my code and transfer the list into a listbox. that was just for testing purposes. Then I was able to generate the same amount of buttons for each item from the text file into a panel. However I cannot seem to get the names of the items to become the text in the buttons and then to assign each button/name to the correct value to then do calculations. I'm not sure if I'm trying to combine two completely different ways of approaching this outcome but any help would be appreciated.What I have tried:this is the form class with the form load method public Form1() { InitializeComponent(); } List<appliances> allEntry = new List<appliances>(); public void Form1_Load(object sender, EventArgs e) { allEntry = ApplianceDA.GetAppliances(); DisplayButtons(); } public void DisplayButtons() { int product = 0; int startTop = 10; int startLeft = 10; foreach (Appliances ap in allEntry) { Button appButton = new Button(); this.Controls.Add(appButton); appButton.Text = (""); appButton.Location = new System.Drawing.Point(startTop + (125 * product), startLeft); appButton.Height = 58; appButton.Width = 120; //appButton.Click += AButton_Click(); pnlBox.Controls.Add(appButton); product++; } }and here is the ApplianceDA class that reads the text file static class ApplianceDA { private const string PATH = "Appliances.txt"; public static List<appliances> GetAppliances() { List<appliances> appliance = new List<appliances>(); StreamReader textIn = new StreamReader(new FileStream(PATH, FileMode.OpenOrCreate, FileAccess.Read)); while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split(','); Appliances product = new Appliances(columns[0], Convert.ToInt64(columns[1])); appliance.Add(product); } textIn.Close(); return appliance; }推荐答案 创建按钮时你将appButton.Text设置为空白。 尝试将其设置为ap.ToString()或你所追求的文本值。When you are creating the buttons you are setting the appButton.Text to blank.Try setting it to ap.ToString() or the text value you are after. 这篇关于尝试从读入的文本文件为每个按钮生成按钮,名称和数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 15:05