I have been making a form to let the user save his progress. There are 6 virtual slots which contain different save files read from a folder. I want to have the same set up just with a scrollbar to let user scroll through save files, in case he has more than 6 made.
The set up is: picturebox which loads save file when clicked, label for file name and for file date, picturebox to delete the save file, and a panel underneath to save file when the slot is clicked.
Below is the code I use to load in 6 save files. (I will get the date by reading the save file start as it contains the date, but I have not done that part yet).
private void loadsavestoscreen()
{
string filename;
string extension;
string locpath = #"C:\test";
String[] allfiles = System.IO.Directory.GetFiles(locpath, "*.*", System.IO.SearchOption.TopDirectoryOnly);
int counter = 0;
foreach (String file in allfiles)
{
if (counter == 6 || counter == allfiles.Length - 1)
{ labelcheck(); break; }
if ((extension = Path.GetExtension(file)) == ".dat")
{
filename = Path.GetFileNameWithoutExtension(file);
//Console.WriteLine(filename);
changelbl(counter, filename);
counter++;
}
}
}
'labelcheck' checks if the text is correct, if not it hides the label.
'lblchange' changes the name of the label on the correct slot.
My question is: How would I implement a scrollbar to allow the user to scroll through more save files when there is more than 6?
Here's a snippet of the form:
I am slightly new to programming so my apologies if I've made some obvious errors. Thanks for any help.
Without a list object or a container this is not very easy to solve.
I suggest you to use a DataGridView or a ListView object. You can easily add your file entries as objects to these lists. They have an option Scrollable, which you can set true or false.
I would also create a class for that save file entries (storing label/image position and contents) and add them to your DataGridView or ListView.
If you want to know how to add images to those controls:
How do add image to System.Windows.Forms.ListBox?
Related
My Windows Application consists of various Settings which mainly include the data in numeric form(both decimal and Hex) and some comboBox .
When the user gives input in these box and when Save button is pressed the settings should be saved(any simple format or any file can work, I am not bound to any particular types) and when Load button is pressed the respective values should be displayed in respective boxes.
What could be the easiest way to perform this.
P.S : My design is in the form of tabcontrol and the boxes are on different tabpages, and save should save data from all tabpages in one file.
I would recommend you to save it in Application Settings where its already there for you:
Properties.Settings.Default.SettingName = "Setting Value";
Properties.Settings.Default.Save();
you can find more info about it in here.
Another way would be just saving your settings in a text file and loading them (Not Recommended).
string Settings = "SomeComboBoxValue = 1\r\n" +
"SomeButtonValue = OK" //goes on like this
To Save:
File.WriteAllText("settings.txt", Settings);
To Load:
string[] lines = File.ReadAllLines("settings.txt");
foreach(string setting in lines)
{
string[] s = setting.Split('=');
switch(s[0].Trim())
{
case "SomeComboBoxValue":
ComboBox1.SelectedIndex = int.Parse(s[1].Trim()); break;
case "SomeButtonValue":
Button1.Text = s[1].Trim(); break;
//goes on like this
}
}
I am currently using a file dialog to export a file, but I was wondering how I could export my file using drag and drop. I couldn't figure out how to get the file path of where the item is being dropped. Here is the code that I used for open file dialogue in case its required.
if (this.listView1.SelectedItems.Count > 0)
{
ListViewItem item = this.listView1.SelectedItems[0];
string text = this.faderLabel8.Text;
if (!text.EndsWith(#"\"))
{
text = text + #"\";
}
using (SaveFileDialog dialog = new SaveFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
Jtag.ReceiveFile(item.SubItems[0].Text, text + item.SubItems[0].Text);
}
}
}
You don't need the path of where the file is being dropped. Instead you need to create a temporary file.
Save file to a temporary folder
Initiate drag on an event/command, such as mouse down, in the following way:
//(This example is uses WPF/System.Windows.DragDrop)
//Create temporary file
string fileName = "DragDropSample.txt";
var tempPath = System.IO.Path.GetTempPath();
var tempFilePath = System.IO.Path.Combine(tempPath, fileName);
System.IO.File.WriteAllText(tempFilePath, "Testing drag and drop");
//Create DataObject to drag
DataObject dragData = new DataObject();
dragData.SetData(DataFormats.FileDrop, new string[] { tempFilePath });
//Initiate drag/drop
DragDrop.DoDragDrop(dragSourceElement, dragData, DragDropEffects.Move);
For WinForms example and more details see:
Implement file dragging to the desktop from a .net winforms application?
If you want it to be useful through "drag and drop" you would require some sort of graphical interface that displays the files in a container where they are and then another container to where you want to move them. When you highlight an item with your mouse you add them to your itemList and when your drop them you copy them. Just make sure the List is emptied once in case you remove the highlight.
I am writing a windows forms application in c# that resizes user-input images. I'm currently trying to add functionality that will allow the user to drag and drop one or more images onto the form, display these images in a list (more detail on current implementation below), then allow the user to click on an image on the list at which point the selected image will show up in a picturebox. With this, I will implement 'batch' resizing.
Current implementation :
in the DragDrop event, populate a string array with the dropped files
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
call 'areAllImages' method which iterates through each file in the string array and returns true if all files are valid image file types, else returns false
if areAllImages == false, show an error messagebox
if (areAllImages(files) == false)
{
MessageBox.Show("Only files with .jpg, .jpeg, .gif, .bmp and .png extensions are allowed. Check the file(s) you are attempting to import into the program.");
}
if areAllImages == true, run a few methods that do stuff with the interface (irrelevant to question), then iterate through the array, adding Bitmaps of the images in the array to an imageList and adding the imageList elements into a Listview (I named it imageListView) :
int i = 0;
foreach (string element in files) //files == string array containing my image files
{
string fileName = Path.GetFileName(files[i]);
Bitmap img = new Bitmap(files[i]);
imageList1.Images.Add("Image" + i, img);
imageListView.Items.Add(fileName, i);
i++;
}
I then have an event handler for the ListView that shows whatever item was selected in the ListView in a PictureBox (Named imgBox). Ignore the updatePreviewer and updateDimensions methods - they are irrelevant.
private void imageListView_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in imageListView.SelectedItems)
{
int imgIndex = item.ImageIndex;
if (imgIndex >= 0 && imgIndex < this.imageList1.Images.Count)
{
Bitmap img = new Bitmap(this.imageList1.Images[imgIndex]);
procedures.updatePreviewer(img, imgBox);
updateDimensions(img, heightLabel, widthLabel);
}
}
}
This works - but, the images that get shown in the PictureBox (imgBox) are 16x16 px. I know that this is because the ImageList imageSize is set to 16x16, but I can't find a way to store the drag and dropped image(s) with its/their original dimensions. The maximum is 256, and I'm going to have to allow inputting images much much larger than that.
Question : How could I manipulate this implementation to show images with their original dimensions? If it is not possible/feasible, what are some alternative ways that I could store these images so that I can access them through some sort of list container and show them in a PictureBox with their original dimensions?
Thank you
Solved the problem by adding an array to hold all the filepaths of the imported images and instead of loading the image from the listview index, I load images from the path at the array index equivalent to the item clicked
In my windows application I am using the ImageListView.dll displaying selected images.
I used the following code for Adding the selected images to ImageListView
string folder = Photo_Care.Properties.Settings.Default.LastFolder;
if (Directory.Exists(folder))
openFileDialog1.InitialDirectory = folder;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
folder = Path.GetDirectoryName(openFileDialog1.FileName);
Properties.Settings.Default.LastFolder = folder;
Properties.Settings.Default.Save();
imageListView1.Items.AddRange(openFileDialog1.FileNames);
}
And I can able to remove the selected Image from Image List using the below code.
imageListView1.SuspendLayout();
// Remove selected items
foreach (var item in imageListView1.SelectedItems)
imageListView1.Items.Remove(item);
// Resume layout logic.
imageListView1.ResumeLayout(true);
But Now I want to add the close button right top of the added image instantly for removing the image. Like Below Image
I grabbed the value from database and I am trying to assign those values in Edit Form. But the only problem is with the FileUpload. It don't take the value. Can anyone suggest me what I'm missing here
private void EditForDataByID(int TitleId)
{
ReadmoreController objFormController = new ReadmoreController();
ReadMoreInfo objInfo = objFormController.GetListObjectOfAllArticle(TitleId);
if (objInfo != null)
{
TextTitle.Text = objInfo.Title;
txtSummary.Text = objInfo.Summary;
TextDate.Text = objInfo.Date.ToString();
//FileUpload1.FileName=objInfo.Image; I even tried this but it doesn't work
FileUpload1 = objInfo.Image;
Session["TitleId"] = TitleId;
ListDiv.Visible = false;
form.Visible = true;
BindGrid();
}
}
For client security reason you can not assign value to FileUploadControl as it could cause the uploading of unwanted files from client machine. So let the use pick the file to upload.
If it is allowed then one can steel the important files from client machine like c:\PersonalPasswords
Edit Based on comments
If you need to ensure that user has selected an Image and does not need to change it then you can use a image control and assign image to it. Use the same image control to find if the image is selected or not.