I am programming in Visual Studio 2013, c# winform. Im trying to make something like Steam Library, but I don't know how to save FlowLayoutPanel that I have in tab1 (Library).
This is how it looks (Library)
This is how it looks (Adding a new game)
This is how it looks (Deleting a new game): http:// oi62.tinypic.com/2uzfc3k.jpg
(sorry, im not able to add images and more than 2 links)
Here is my code:
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=flpContainter.
So, my question is, how can i save items (buttons) in FlowLayoutPanel and how to load them later?
Thank you for your answers!
You should create a class for your items (game buttons), including their Title, Image etc. Then you can save them using XML.
class Game
{
// Properties here
}
This link will provide you with a quick How-to on how to accomplish this.
For saving images you can convert the image to base64 and convert it back to an image when loading the XML file again.
Related
I am making an application that has some options in drop-down menus that get populated from the App.Config file. I was testing a reset function when the program stopped doing the reset. My code for Form1 is below:
public Form1()
{
InitializeComponent();
InitializeDropDownMenu();
}
private void InitializeDropDownMenu()
{
//Populate all the menus from app.config
foreach (string s in Properties.Settings.Default.Box1Contents)
{
comboBox1.Items.Add(s);
}
foreach (string s in Properties.Settings.Default.Box2Contents)
{
comboBox2.Items.Add(s);
}
foreach (string s in Properties.Settings.Default.Box3Contents)
{
comboBox3.Items.Add(s);
}
//Controls for drop down menus
this.Controls.Add(comboBox1);
comboBox1.SelectedIndexChanged +=
new System.EventHandler(comboBox1_SelectedIndexChanged);
this.Controls.Add(comboBox2);
comboBox2.SelectedIndexChanged +=
new System.EventHandler(comboBox2_SelectedIndexChanged);
this.Controls.Add(comboBox3);
comboBox3.SelectedIndexChanged +=
new System.EventHandler(comboBox3_SelectedIndexChanged);
//Begin Program with all dDMenus enabled.
comboBox1.Enabled = true;
comboBox2.Enabled = true;
comboBox3.Enabled = true;
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox1.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox1.SelectedText;
}
else if( result == DialogResult.No)
{
comboBox1.ResetText();
}
}
private void comboBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox2.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox2.SelectedText;
}
else if (result == DialogResult.No)
{
comboBox2.ResetText();
}
}
private void comboBox3_SelectedIndexChanged(object sender, System.EventArgs e)
{
DialogResult result = MessageBox.Show(
"Change Viewer to: \r\n" + comboBox3.Text + "\r\n\r\n" + "Confirm?",
"Menu",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
label3.Text = comboBox3.SelectedText;
}
else if (result == DialogResult.No)
{
comboBox3.ResetText();
}
}
private void ResetApp()
{
comboBox1.ResetText();
comboBox2.ResetText();
comboBox3.ResetText();
}
private void button1_Click(object sender, EventArgs e)
{
ResetApp();
label3.Text = "ResetApp Ran";
}
Any ideas as to why label3 is always set to null, and why when reset is clicked the ComboBoxes aren't being reset to blanks anymore?
Thank you for your help,
-Arthur
EDIT* I will use Items.Clear(); and then just call InitializeDropDownMenu() in the reset function. Should work for my intended use. Thank you all.
I think the problem is in use of SelectedText. The SelectedTextproperty "Gets or sets the text that is selected in the editable portion of a System.Windows.Forms.ComboBox".
Instead try to use the SelectedItem property.
label1.Text = comboBox1.SelectedItem.ToString();
How can I limit the number of files to be uploaded using the multi-select openfiledialog in c#?
Here's my code:
private void btn_upload_Click(object sender, EventArgs e)
{
OpenFileDialog op1 = new OpenFileDialog();
op1.Multiselect = true;
op1.ShowDialog();
op1.Filter = "allfiles|*.xls";
textBox1.Text = op1.FileName;
int count = 0;
string[] FName;
foreach (string s in op1.FileNames)
{
FName = s.Split('\\');
File.Copy(s, "C:\\file\\" + FName[FName.Length - 1]);
count++;
}
MessageBox.Show(Convert.ToString(count) + " File(s) copied");
}
It will upload as how much the user wants to. But I want to limit it by 5 files only.
You can't do that directly but you can check the selected files count and display a message to user:
if(op1.FileNames.Length > 5)
{
MessageBox.Show("your message");
return;
}
Or you can take the first five file from selected files:
foreach (string s in op1.FileNames.Take(5))
{
...
}
I just tested and this works:
private void btn_upload_Click(object sender, EventArgs e)
{
OpenFileDialog op1 = new OpenFileDialog();
op1.Multiselect = true;
op1.FileOk += openFileDialog1_FileOk; // Event handler
op1.ShowDialog();
// etc
}
void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
OpenFileDialog dlg = sender as OpenFileDialog;
if (5 < dlg.FileNames.Length)
{
MessageBox.Show("Too Many Files");
e.Cancel = true;
}
}
public partial class Form1 : Form
{
SaveFileDialog sfd = new SaveFileDialog();
OpenFileDialog ofd = new OpenFileDialog();
public string contents = string.Empty;
public Form1()
{
InitializeComponent();
this.Text = "Untitled";
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
sfd.Title = "Save";
if (SaveFile() == 0)
return;
else
{
richTextBox1.Text = "";
this.Text = "Untitled";
}
contents = "";
}
else if (dr == DialogResult.No)
{
richTextBox1.Text = "";
this.Text = "Untitled";
contents = "";
}
else
{
richTextBox1.Focus();
}
}
else
{
this.Text = "Untitled";
richTextBox1.Text = "";
contents = "";
}
}
private int SaveFile()
{
sfd.Filter = "Text Documents|*.txt";
sfd.DefaultExt = "txt";
if (sfd.ShowDialog() == DialogResult.Cancel)
{
richTextBox1.Focus();
return 0;
}
else
{
contents = richTextBox1.Text;
if (this.Text == "Untitled")
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
else
{
sfd.FileName = this.Text;
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
}
this.Text = sfd.FileName;
return 1;
}
}
private void OpenFile()
{
ofd.Filter = "Text Documents|*.txt";
if (ofd.ShowDialog() == DialogResult.Cancel)
richTextBox1.Focus();
else
{
richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
this.Text = ofd.FileName;
contents = richTextBox1.Text;
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
SaveFile();
OpenFile();
}
else if (dr == DialogResult.No)
{
OpenFile();
}
else
{
richTextBox1.Focus();
}
}
else
OpenFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
sfd.Filter = "Text Documents|*.txt";
sfd.DefaultExt = "txt";
if (sfd.ShowDialog() == DialogResult.Cancel)
{
richTextBox1.Focus();
}
else
{
contents = richTextBox1.Text;
richTextBox1.SaveFile(sfd.FileName, RichTextBoxStreamType.PlainText);
this.Text = sfd.FileName;
}
}
When we open Windows notepad application then open a file, changes it contents and save it, it simply gets saved without opening the Save File dialog. But in the notepad program I have created above the Save File dialog opens on clicking 'Save' after changing the contents of saved file. Although the same file name appears in the save file dialog but on clicking 'Save' it gives a message "The same file name already exists. Do you want to replace it?". That is what I want to remove and make the changed contents saved directly to the opened file without opening the save file dialog.
Set sfd.OverwritePrompt = false any time after construction and before ShowDialog to suppress the overwrite warning.
You want to have two choices to save: A 'Save As..' button and a 'Save' button. You can create a string to hold the path of the opened file. The location may also change if the user specifies a new location when they save the file. If the user did not open the file, the 'Save As...' button would open the regular Save File Dialog. Once the user specifies the location of their document, you can save the file path to that string and use a `StreamWriter' to save it without the dialog:
...
using System.IO;
...
public partial class Form1 : Form
{
SaveFileDialog sfd = new SaveFileDialog();
OpenFileDialog ofd = new OpenFileDialog();
public string contents = string.Empty;
//string to hold file location
string currentFileLoc;
public Form1()
{
InitializeComponent();
this.Text = "Untitled";
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
sfd.Title = "Save";
if (SaveFile() == 0)
return;
else
{
richTextBox1.Text = "";
this.Text = "Untitled";
}
contents = "";
}
else if (dr == DialogResult.No)
{
richTextBox1.Text = "";
this.Text = "Untitled";
contents = "";
}
else
{
richTextBox1.Focus();
}
}
else
{
this.Text = "Untitled";
richTextBox1.Text = "";
contents = "";
}
}
private int SaveFile()
{
sfd.Filter = "Text Documents|*.txt";
sfd.DefaultExt = "txt";
if (sfd.ShowDialog() == DialogResult.Cancel)
{
richTextBox1.Focus();
return 0;
}
else
{
contents = richTextBox1.Text;
if (this.Text == "Untitled")
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
else
{
sfd.FileName = this.Text;
richTextBox1.SaveFile(sfd.FileName,RichTextBoxStreamType.PlainText);
}
this.Text = sfd.FileName;
//
currentFileLoc = sfd.FileName;
return 1;
}
}
private void OpenFile()
{
ofd.Filter = "Text Documents|*.txt";
if (ofd.ShowDialog() == DialogResult.Cancel)
richTextBox1.Focus();
else
{
richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);
this.Text = ofd.FileName;
contents = richTextBox1.Text;
}
currentFileLoc = ofd.FileName;
this.Text = currentFileLoc;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != contents)
{
DialogResult dr = MessageBox.Show("Do You want to save the changes made to " + this.Text, "Save", MessageBoxButtons.YesNoCancel);
if (dr == DialogResult.Yes)
{
SaveFile();
OpenFile();
}
else if (dr == DialogResult.No)
{
OpenFile();
}
else
{
richTextBox1.Focus();
}
}
else
OpenFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Save();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFile();
}
//new method
private void Save()
{
if (currentFileLoc != null)
{
using (StreamWriter writer = new StreamWriter(currentFileLoc))
{
writer.WriteLine(richTextBox1.Text);
}
}
else
saveFile();
}
}
I suggest you also enclose the using(...){ } block in a try/catch statement and handle any exceptions.
What you need to do is save the filename entered, then, when the save option is pressed, check for a previously entered filename. If you have one, skip showing the dialog and just execute the save code.
I have a question about the opfiledialog function within c#. When i dont select a file with openfiledialog it put's a text automaticly in my textbox. That text will be "filedialog1". What can i do to fix this.
using System;
using System.Windows.Forms;
using System.IO;
namespace Flashloader
{
public partial class NewApplication : Form
{
private toepassinginifile _toepassinginifile;
private controllerinifile _controllerinifile;
//private controllerinifile _controlIniFile;
public NewApplication(toepassinginifile iniFile)
{
_controllerinifile = new controllerinifile();
_toepassinginifile = iniFile;
InitializeComponent();
controllerComboBox.DataSource = _controllerinifile.Controllers;
}
public bool Run()
{
var result = ShowDialog();
return result == System.Windows.Forms.DialogResult.OK;
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
openFileDialog1.Title = ("Choose a file");
openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
}
}
private void button3_Click(object sender, EventArgs e)
{
Toepassing toepassing = new Toepassing();
toepassing.Name = nameBox.Text;
toepassing.Controller = (Flashloader.Controller)controllerComboBox.SelectedItem;
toepassing.TabTip = descBox.Text;
toepassing.Lastfile = openFileDialog1.FileName;
fileBox.Text = openFileDialog1.FileName;
if (nameBox.Text == "")
MessageBox.Show("You haven't assigned a Name");
else if (controllerComboBox.Text == "")
MessageBox.Show("You haven't assigned a Controller");
else if (descBox.Text == "")
MessageBox.Show("You haven't assigned a Desciption");
else if (fileBox.Text == "")
MessageBox.Show("You haven't assigned a Applicationfile");
_toepassinginifile.ToePassingen.Add(toepassing);
_toepassinginifile.Save(toepassing);
MessageBox.Show("Save Succesfull");
DialogResult = DialogResult.OK;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
var newcontroller = new Newcontroller(_controllerinifile);
newcontroller.ShowDialog();
controllerComboBox.DataSource = null;
controllerComboBox.DataSource = _controllerinifile.Controllers;
}
}
}
Thanks all for the help
private void button3_Click(object sender, EventArgs e)
{
toepassing.Lastfile = openFileDialog1.FileName;// Dont do this
fileBox.Text = openFileDialog1.FileName; //or this
Its unclear to me why you are holding onto an Open file dialog I would personally do the following
using(OpenFileDialog ofd = new OpenFileDialog())
{
if(ofd.ShowDialog() == DialogResult.OK)
{
classStringVariable = ofd.FileName;
fileBox.Text = ofd.FileName;
}
}
Then in button 3
toepassing.LastFile = classStringVariable ;
fileBox.Text = classStringVariable ;
When you use the Form Designer to add an OpenFileDialog control on you form, the designer assigns at the property FileName the value openFileDialog1.
I suppose you have set something as the initial value for the property FileName. Then in button_click3 you have no mean to check for the DialogResult and thus you get inconditionally this default back.
Fix it removing this default from the designer FileName property
Just add openFileDialog1.FileName= ""; before you show the dialog.
openFileDialog1.Filter = "Srec Files (.a20; .a21; .a26; .a44)|*.a20; *.a21; *.a26; *.a44|All files (*.*)|*.*";
openFileDialog1.Title = ("Choose a file");
openFileDialog1.InitialDirectory = Path.Combine(Directory.GetCurrentDirectory());
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
{
fileBox.Text = (System.IO.Path.GetFileName(openFileDialog1.FileName));
}
In your button3_Click event you're checking for an empty string file name anyways, so they would get the correct error message and they wouldn't have some weird arbitrary default name show up when they open the dialog.
I have made a method so that when you click on Browse From File the openDialogBox appear. But it does not . What is wrong ?
This is the method that I made to Open the OpenFileDialog:
public void Lista()
{
string[] col2 = new string[dataGridView1.Rows.Count];
for (int i = 0; i < dataGridView1.Rows.Count; i++)
if (col2[i] == "Browse From File...")
{
DialogResult result2 = openFileDialog2.ShowDialog();
if (result2 == DialogResult.OK)
{
// filename = openFileDialog1.FileName;
}
}
}
This is the method where I call the method Lista.
private void button1_Click(object sender, EventArgs e)
{
// opens window **BROWSE**
openFileDialog1.Title = "Choose File CSV ";
string filename = "";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
filename = openFileDialog1.FileName;
textBox1.Text = filename;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text);
stringforData = file.ReadLine();
while ((line = file.ReadLine()) != null)
{
//read inside the table
fileList.Add(line.Split(';'));
}
file.Close();
this.ToDataGrid();
this.Lista();
}
}
DataGridView by only having one editing control for all the rows. Here's how I handled a similar situation, First try a delegate to the EditControlShowing event
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog ofd = new OpenFileDialog();
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Columns.Add("ID", "Product ID");
DataGridViewComboBoxColumn comboxboxCol = new DataGridViewComboBoxColumn();
comboxboxCol.HeaderText = "Type";
comboxboxCol.Items.Add("Obj1");
comboxboxCol.Items.Add("Obj2");
comboxboxCol.Items.Add("Obj3");
dataGridView1.Columns.Add(comboxboxCol);
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(Grid_EditingControlShowing);
}
void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox combo = e.Control as ComboBox;
if (combo != null)
{
// the event to handle combo changes
EventHandler comboDelegate = new EventHandler(
(cbSender, args) =>
{
ofd.ShowDialog();
});
// register the event with the editing control
combo.SelectedValueChanged += comboDelegate;
}
}
}
}
Hope this will solve your Issue