c#保存流布局面板
Posted
技术标签:
【中文标题】c#保存流布局面板【英文标题】:c# Save flow layout panel 【发布时间】:2014-07-09 02:57:34 【问题描述】:我在 Visual Studio 2013 中编程,c# winform。我正在尝试制作 Steam 库之类的东西,但我不知道如何保存我在 tab1(库)中拥有的 FlowLayoutPanel。
This is how it looks (Library)
This is how it looks (Adding a new game)
这是它的样子(删除一个新游戏):http://oi62.tinypic.com/2uzfc3k.jpg
(抱歉,我无法添加图片和超过 2 个链接)
这是我的代码:
private void btnTest_Click_1(object sender, EventArgs e)
if (textBox1.Text != "")
if (textBox2.Text != "")
if (textBox3.Text != "")
Button btn = sender as Button;
Button btnNew = new Button();
btnNew.Text = "";
btnNew.Height = 108;
btnNew.Width = 230;
btnNew.Name = textBox3.Text;
comboBox1.Items.Add(textBox3.Text);
btnNew.BackgroundImage = new Bitmap(textBox1.Text);
btnNew.BackgroundImageLayout = ImageLayout.Stretch;
btnNew.FlatStyle = FlatStyle.Flat;
flpContainer.Controls.Add(btnNew);
btnNew.Click += btnNew_Click;
btnNew.Tag = textBox2.Text;
counter1+=+1;
label1.Text = counter1.ToString();
System.Windows.Forms.MessageBox.Show("Game " + textBox3.Text + " was successfully added to library!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
else if (textBox3.Text == "")
MessageBox.Show("You didn't wrote name!");
else if (textBox2.Text == "")
System.Windows.Forms.MessageBox.Show("You didn't choose exe file!");
else if (textBox1.Text == "")
System.Windows.Forms.MessageBox.Show("You didn't choose image!");
private void btnNew_Click(object sender, EventArgs e)
Button clickedButton = (Button)sender;
Process.Start((string)clickedButton.Tag);
private void ZvolitObrazek_Click(object sender, EventArgs e)
openFileDialog1.Title = "Open Image";
openFileDialog1.FileName = "Image file";
openFileDialog1.Filter = "Image files (*.jpg, *.img, *.png, *.jpeg)|*.jpg; *.img; *.png; *.jpeg|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
textBox1.Text = openFileDialog1.FileName;
private void button1_Click(object sender, EventArgs e)
openFileDialog2.Title = "Open exe";
openFileDialog2.FileName = "Exe file";
openFileDialog2.Filter = "Exe files (*.exe)|*.exe|All files(*.*)|*.*";
if (openFileDialog2.ShowDialog() == DialogResult.OK)
textBox2.Text = openFileDialog2.FileName;
private void flpContainer_Paint(object sender, PaintEventArgs e)
flpContainer.AutoScroll = true;
private void button2_Click(object sender, EventArgs e)
if (comboBox1.Text == "")
MessageBox.Show("You didn't choose game that you want delete!");
else if (comboBox1.Text != "")
Control foundControl = null;
foreach (Control c in flpContainer.Controls)
c.Name = comboBox1.Text;
foundControl = c;
flpContainer.Controls.Remove(foundControl);
counter1 = counter1 - 1;
label1.Text = counter1.ToString();
MessageBox.Show("Game " + comboBox1.Text + " was successfully deleted");
comboBox1.Items.Remove(comboBox1.Text);
comboBox1.Text = "";
FlowLayoutPanel=flpContainer。 所以,我的问题是,如何在 FlowLayoutPanel 中保存项目(按钮)以及以后如何加载它们? 感谢您的回答!
【问题讨论】:
【参考方案1】:您应该为您的项目(游戏按钮)创建一个类,包括它们的标题、图像等。然后您可以使用 XML 保存它们。
class Game
// Properties here
This 链接将为您提供有关如何完成此操作的快速操作指南。 为了保存图像,您可以将图像转换为 base64 并在再次加载 XML 文件时将其转换回图像。
【讨论】:
谢谢你:)。顺便说一句,这会花费我很多时间吗? :O :D 创建类还是保存类?如果您具有 C# 的基本知识,那么创建类应该不会那么难。但是你总是可以使用谷歌,它会帮助你在一小时内完成它:) 哦,哎呀,我没有 C# 的基本知识,我还在学习……我应该给那个游戏课写什么? :) 谢谢你的时间。 你应该在类里面写属性。例如,对于游戏的标题,您应该创建以下属性:public string Title get; set;
。然后您可以使用以下代码进行设置:Game exampleGame = new Game(); exampleGame.Title = "Your title";
。现在您可以序列化该类(使用我在上面发布的链接中的操作方法),它将 exampleGame 对象保存到 XML 文件中。
嗨,对不起,我以为我明白了,但实际上我没有......当我在那里输入“你的标题”时,它只是将它保存为“你的标题”(是的,这是合乎逻辑的)但我想保存你在程序运行时在文本框中写的标题,可以以某种方式完成吗?很多。以上是关于c#保存流布局面板的主要内容,如果未能解决你的问题,请参考以下文章