Update ListBox from another class not working - c#

I have a ListBox that is populated with entries from a folder consisting of text files (the list box is taking the names of the files only). I have trouble getting the list box to refresh every time I add files. You add files/errands by pressing a button in the main program, which brings out a second window in which you write your errand and choose its priority (low, medium or high).
The desired effect would be that the list box would update itself when adding new text files/errands, to include it, however, this is not the case at the moment, and I've tried following examples on the net by using DataStore and Binding among others, but none have worked so far. The main program looks like this:
P.S. The program is half-Swedish, but essentially, "Skapa lapp" = "Create errand", which is the only important one here.
And this image below is just to show you how the list box and the errands/text files work together (the text files are added to the list box by a foreach loop).
When creating a new errand (Skapa lapp-button), you will be presented with a new window:
When writing your new errand in this window and choosing a priority level then pressing "Create errand" (or Skapa lapp), the following will happen on that button click (simplified version):
private string mappNamn = #"C:\Errands\";
Lapphantering uppdateraFönster = new Lapphantering();
private void buttonSkapaLapp_Click(object sender, EventArgs e)
{
try
{
//When choosing the low priority radio button, do this:
if (radioButtonLågPrio.Checked)
{
using (var file = new StreamWriter(Path.Combine(mappNamn, "1 - " + textBoxLappText.Text + ".txt")))
{
uppdateraFönster.listBoxLappar.Items.Add(textBoxLappText.Text);
uppdateraFönster.Update(); //This doesn't work.
uppdateraFönster.Refresh(); //Nor does this.
}
}
Back over to the main window (Lapphantering), the list box is only updated when you restart the application all over again and let the main program add the files by initializing the component:
public Lapphantering()
{
InitializeComponent();
//For each file, add new files to the list box.
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Errands\");
FileInfo[] Filer = dinfo.GetFiles("*.txt");
mappNamn = dinfo.FullName;
foreach (FileInfo file in Filer)
{
listBoxLappar.Items.Add(Path.GetFileNameWithoutExtension(file.Name));
}
}
So, how can I refresh/update the list box every time I add a new errand/text file without having to restart the application each time?

Theproblem is that you are creating a new instance of Lapphantering and editting the value in there. Change this in your Program cs:
public static Lapphantering mainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
mainForm = new Lapphantering(); // create instance of Lapphantering
Application.Run(mainForm);
}
then in your other window do this:
if (radioButtonLågPrio.Checked)
{
using (var file = new StreamWriter(Path.Combine(mappNamn, "1 - " + textBoxLappText.Text + ".txt")))
{
Program.mainform.listBoxLappar.Items.Add(textBoxLappText.Text);
Program.mainform.listBoxLappar.Update();
Program.mainform.listBoxLappar.Refresh(); // access mainform in Program
}
}
this should work your are working on one object in the program.cs

You are looking for a file system monitor. This might help: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Related

How to add entries every time a button is pressed

I am making a mobile app using Xamarin Forms and I am writing all of my code including the visual aspects in c# (in the .cs files).
Essentially I need to be able to add a new entry every time a button is pressed and then get the text entered into said entry.
Right now I can create a new Entry and give it a name that I can use to reference it:
private Entry entry1;
Layout.Children.Add(entry1 = new Entry
{
//entry code
});
//when some button is pressed
string entry1Text = entry1.Text;
I want to make it so that every time the user presses a button, it creates a new entry, but I also need to be able to get the text from it. How can I make it so that it creates a new entry with a new name like entry2, entry3, etc... without manually writing out like 10 entries and then making them visible? I need to do this because I don't know how many entries the user will add (could be more than 10).
int numberOfEntries = 1;
void addEntry_Clicked(object sender, EventArgs e)
{
string entryNumber = numberOfEntries.ToString();
//the following 2 lines are what doesn't work with the name of an entry, but is what I want to do
private Entry entry + entryNumber;
Layout.Children.Add(entry + entryNumber = new Entry
{
//entry code
});
numberOfEntries+=1;
}
//some button is pressed
string entryText = (entry + entryNumber).Text;
The problem is I can't add a number to the name of an entry like entry +"2"
Is this even possible for me to do?
you need to keep a separate data structure to track your controls, like this
Dictionary<string,Entry> entries = new Dictionary<string,Entry>();
private void AddEntry(string name)
{
var entry = new Entry();
myLayout.Children.Add(entry);
entries.Add(name,entry);
}
then you can get their value like this
var text = entries["entryA"].Text;

How to fix ListView.LargeImageList showing images twice

I am working on a piece of software, which compares memes and helps users organize memes on their computer. As a part of this I am using Windows.Forms to build a UI. This UI lets the user add folders to be checked for images, which can be compared to a set of known meme templates.
My issue arises when I try to show the user the found images. To do this I am using a ListView and the property LargeImageList to contain a tuple of the image and the name of the image file.
Here is the piece of code in question:
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (var file in files)
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(Image.FromFile(file));
}
foreach (var file in files)
{
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
This is an example of what the user sees when they first load in a folder. When the user tries to load in another folder this happens. It shows the images from the first folder, with the names of the image files from the second folder.
Does anyone know a fix for this issue? I have tried a variety of options in order to get around the issue. All from trying to clear the ImageList used to contain the images, to trying my hand at controlling when the ListView updates. None of this has worked. I have also tried googling the issue, but with no luck of finding a fix.
Thank you in advance.
If you want to show the content of a single folder at the time, then dispose of the objects in your ImageList.
If you instead want to show the content of more than one folder, you need to specify the new index of the image added. You're instead adding a new Item in the ListView using the same index reference:
int i = 0;
//(...)
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
The indexer (i) always starts from 0, thus the ListView Item will use the images in your Imagelist starting from the Image at Index[0] each time. The new images won't ever be shown.
You can use the ImageList.Images.Count value, representing the number of Images already added to the ImageList, as base and increment the indexer starting from this value:
private void button1_Click(object sender, EventArgs e)
{
int i = imageList1.Images.Count;
var ic = new ImageCollection();
var fbd = new FolderBrowserDialog();
fbd.Description = "Select meme folder or image.";
if (fbd.ShowDialog() == DialogResult.OK)
{
foreach (var file in Directory.GetFiles(fbd.SelectedPath))
{
if (!ic.CheckIfImage(file)) continue;
imageList1.Images.Add(new Bitmap(file, true));
listView1.Items.Add($"{Path.GetFileNameWithoutExtension(file)}", i++);
}
}
}
If you allow to remove an Image from the ListView, you should also remove it from the ImageList: this implies that you need to re-index all the ListView Items starting from the Item that follows the one removed.
Remember to dispose of the Images you remove from the ImageList.

Showing strings in ListView columns dynamically - not working

My goal is the following:
The user enters the stuff as shown in this picture. The 3 items (source file, destination directory and if file exists) are saved in a string array. User clicks OK.
After clicking OK the previous window is closed and the user is taken to the main form shown in this picture. The previously mentioned source file and the destination directory are shown in the table.
I am using the following code to show the entered file & directory in the table:
private void okButton_Clicked(object sender, EventArgs e)
{
//saving user's input
userInput = new string[3];
userInput[0] = sourceFileTextBox.Text;
userInput[1] = destinationDirComboBox.SelectedItem.ToString();
userInput[2] = ifFileExistsComboBox.SelectedItem.ToString();
//creating a new ListView object - the object is derived from the ListView Class
and has absolutely nothing in the constructor or anywhere
Classes.ListViewDerivative lvd = new Classes.ListViewDerivative();
ListViewItem item1 = new ListViewItem(userInput[0]);
item1.SubItems.Add(userInput[1]);
lvd.Items.AddRange(new ListViewItem[] { item1 });
this.DialogResult = DialogResult.OK;
this.Hide();
}
It doesn't work. The table is empty no matter where I put this code - I've tried to put this adapted code in the ListViewDerivative constructor, a function in the ListViewDerivative Class and in the editFileEntry(first picture) class. The right text is saved in the array but is not shown in the table. Please help!
the lvd variable is not used anywere. You have to add the 'lvd' -control to your mainview. You can use the Designer for this and in the Dialog Result of your "FilePickerDialog" you can assign the results to your datagrid.
some pseudo code for your MainView
void Config_Clicked()
{
ConfigDlg dlg = new ConfigDlg();
if(dlg.ShowDialog() == OK)
{
this.myListView1.Items.Add(dlg.userInput[0]);
}
}

Can't Add Tab Page To TabControl Inside The Constructor Of My WinForms Application

I have a C# Form application that has a TabControl in the main form. This TabControl is used to display multilple TabPages that contain a CustomControl. This CustomControl is just a Panel with a few buttons and a PictureBox.
Here is a picture of my app when it starts up. As you can see the tab control (the white area) is empty:
If the user clicks the "Add Image" button they are presented with an OpenFileDialog to select the image then the addImage method is called with the selected file:
private void doAddImage()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = Constants.Global.IMAGE_FILE_FILTER();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string imageFileName = openFileDialog.FileName;
addImage(imageFileName);
}
}
private void addImage(string imageFileName)
{
// Create a new bitmap and image preview custom control. Then create a new tab
// page and add the custom control to the tab page.
Bitmap bitMap = new Bitmap(imageFileName);
ImagePreviewCustomControl previewControl = new ImagePreviewCustomControl(bitMap);
previewControl.Dock = DockStyle.Fill;
TabPage tabPage = new TabPage(Path.GetFileNameWithoutExtension(imageFileName));
tabPage.Controls.Add(previewControl);
// Insert the new tab page to the right of the currently selected tab page
int selectedTabIndex = imagesTabControl.SelectedIndex;
imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex + 1;
}
As you can see, in the addImage method I create the Bitmap, CustomControl, and TabPage and then insert it into the TabControl.
I start my application, click the "Add Image" button, everything works just fine.
Here is a picture with a tab page added:
While I am testing my app I don't want to have to add an image manually using the OpenFileDialog every time so in my constructor I just call addImage with some fixed image file name that I want to test with:
public ImageViewerApp()
{
InitializeComponent();
addImage(#"C:\MyImages\Calculator-3-icon.png");
}
The problem I am having is that when I try to add the image in my constructor it doesn't show up in the TabControl. The application starts up blank (like the first picture).
As stated above when the application is already running and I click the "Add Image" button it gets added just fine.
I found a property in the TabControl class called Created which states:
"Gets a value indicating whether the control has been created"
So to try and figure out what's going on I write the value of Created to the console just before I call addImage in the constructor. (I have a custom console for debugging my Form applications.)
public ImageViewerApp()
{
InitializeComponent();
TestConsole.WriteLine(imagesTabControl.Created);
addImage(#"D:\Development\Work\Other\Stardock\Start8\_downloaded\Calculator-3-icon.png");
}
The value of Created just before the call to addImage in the constructor is:
False
I put another console output inside the addImage method:
private void doAddImage()
{
TestConsole.WriteLine(imagesTabControl.Created);
OpenFileDialog openFileDialog = new OpenFileDialog();
...
...
}
The value of Created after the app has started and the user presses the "Add Image" button is:
True
Why is it that the TabControl is not Created inside my constructor (even after the InitializeComponent() call) and the once the application is running it is Created?
=UPDATE========================================================================
Based on the suggestion by Hans Passant I have added the following code to my addImage method:
int selectedTabIndex = -1;
if (imagesTabControl.TabCount > 0)
{
selectedTabIndex = imagesTabControl.SelectedIndex;
}
else
{
selectedTabIndex = imagesTabControl.SelectedIndex + 1;
}
imagesTabControl.TabPages.Insert(selectedTabIndex, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex;
This doesn't work.
===============================================================================
=UPDATE2=======================================================================
int selectedTabIndex = imagesTabControl.SelectedIndex;
if (imagesTabControl.TabCount == 0) selectedTabIndex = -1;
imagesTabControl.TabPages.Insert(selectedTabIndex, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex;
This causes the following Exception:
{"InvalidArgument=Value of '-1' is not valid for 'index'.\r\nParameter name: index"}
===============================================================================
=UPDATE3=======================================================================
I tried the folllowing code:
int selectedTabIndex = imagesTabControl.SelectedIndex;
if (imagesTabControl.TabCount == 0) selectedTabIndex = -1;
imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex + 1;
This one doesn't throw an exception but again no tab page added after calling
addImage in the constructor.
===============================================================================
=UPDATE4=======================================================================
I have kindof given up on adding an image in the constructor. So instead I am using an enum RunMode and a variable RUN_MODE of that type. Then, if RUN_MODE == RunMode.TESTI call a method to add a random image when I click the button. (The OpenFileDialog is not used. I just parse through all the image files in the fixed directory IMAGE_DIRECTORY.
enum RunMode { NORMAL, TEST }
private static string IMAGE_DIRECTORY = #"D:\\Work\Images";
...
...
private void doAddImage()
{
if (RUN_MODE == RunMode.TEST)
{
addRandomImage();
return;
}
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = Constants.Global.IMAGE_FILE_FILTER();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string imageFileName = openFileDialog.FileName;
addImage(imageFileName);
}
}
private void addRandomImage()
{
string[] allFiles = Directory.GetFiles(IMAGE_DIRECTORY);
List<string> imageFileNames = new List<string>();
foreach (string file in allFiles)
{
bool isImageFile = Constants.Global.IMAGE_EXTENSIONS.Contains(Path.GetExtension(file));
if (isImageFile)
{
imageFileNames.Add(file);
}
}
int randomIndex = new Random().Next(imageFileNames.Count);
addImage(imageFileNames.ElementAt(randomIndex));
}
This works. Now when I click the "Add Image" button during TEST_MODE I skip the
OpenFileDialog and just add a random image.
I would like to understand the issues with TabControl but at this point I just
need to continue development. My current solution works great.
As I person who like to understand everything I would like to use other people's
suggestions so I will keep monitoring this question for a solution.
===============================================================================
JonP's answer gave me the idea to just wait for the window handle to be created before inserting the tab, so I tried some events occuring between Form construction and Tab Control display.
I found it to work with both the Load or Shown events:
Right-click on the Form (the root, not child controls) in the Designer view > Properties > Events (flash icon) > Behavior > enter a method name for the Load or Shown event and confirm. To generate a Load event callback you can also double-click on the Form itself. This should generate something like this:
this.Load += new System.EventHandler(this.Form1_Load);
// or
this.Shown += new System.EventHandler(this.Form1_Shown);
Setup the tabs in the callback:
private void Form1_Load(object sender, EventArgs e)
{
// Add image (this will call imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage))
// This must be done on Load event because Insert requires
// the window handle, which is not ready in the constructor
addImage(#"path_to_image.png");
}
I have had this problem too and have found a workaround; I think it must be a bug with Insert():
Don't use Insert(), it usually does nothing, use Add() instead; this reliably adds a TabPage to the end of the collection.
After adding it swap it with the tab position where you actually want it.
imagesTabControl.TabPages.Add(tabPage);
// Now swap the two tabs:
imagesTabControl.TabPages[imagesTabControl.TabCount - 1] = imagesTabControl.TabPages[selectedTabIndex + 1];
imagesTabControl.TabPage[selectedTabIndex + 1] = tabPage;
Your mileage may vary of course :-)
Stop Press! An even better fix is to read the class's Handle member before calling Insert():
var handle = imagesTabControl.Handle;
Insert() works perfectly after you do that. Obvious isn't it???? The help page for Handle has this possible relevant Remark showing that the object actually does something when you read Handle:
The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.
You could remove the TabControl from the designer and then instead just manually create the TabControl programmatically and add it to the Form immediately after InitializeComponent(). Then after you create the TabControl, call addImage(). Something like:
InitializeComponent();
TabControl tc = new TabControl();
tc.Location = new Point(10, 10);
tc.Size = new Size(100, 100);
tc.Visible = true;
tc.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
this.Controls.Add(tc)
addImage("c:\pathToImage\image.bmp");

I need to know how to take the selected item of a comboBox and make it appear on a windows form application?

I have a windows form application with a ComboBox on it and I have some strings in the box. I need to know how when I select one of the strings and press my create button, how can i make that name show up on another windows form application in the panel I created.
Here is the code for adding a customer
public partial class AddOrderForm : Form
{
private SalesForm parent;
public AddOrderForm(SalesForm s)
{
InitializeComponent();
parent = s;
Customer[] allCusts = parent.data.getAllCustomers();
for (int i = 0; i < allCusts.Length; i++)
{
Text = allCusts[i].getName();
newCustomerDropDown.Items.Add(Text);
newCustomerDropDown.Text = Text;
newCustomerDropDown.SelectedIndex = 0;
}
now when i click the create order button I want the information above to be labeled on my other windows form application.
private void newOrderButton_Click(object sender, EventArgs e)
{
//get the info from the text boxes
int Index = newCustomerDropDown.SelectedIndex;
Customer newCustomer = parent.data.getCustomerAtIndex(Index);
//make a new order that holds that info
Order brandSpankingNewOrder = new Order(newCustomer);
//add the order to the data manager
parent.data.addOrder(brandSpankingNewOrder);
//tell daddy to reload his orders
parent.loadOrders();
//close myself
this.Dispose();
}
The context is not very clear to me, but if I got it right, you open an instance of AddOrderForm from an instance of SalesForm, and when you click newOrderButton you want to update something on SalesForm with data from AddOrderForm.
If this is the case, there are many ways to obtain it, but maybe the one that requires the fewer changes to your code is this one (even if I don't like it too much).
Make the controls you need to modify in SalesForm public or at least internal (look at the Modifiers property in the Design section of the properties for the controls). This will allow you to write something like this (supposing customerTxt is a TextBox in SalesForm):
parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;

Categories