Hi,
I created two forms, first one contains a command button that open another form to list all the files inside a specific directory/folder
private void showItemsInTheDirectory()
{
DirectoryInfo currentDirectory = new DirectoryInfo(@"C:\Users\UserName\MyDocuments");
FileInfo[] files = currentDirectory.GetFiles();
foreach (FileInfo file in files)
{
ListView listView1 = new ListView();
ListViewItem item = new ListViewItem();
item.Text = file.Name;
listView1.Items.Add(item);
}
Form1 form1 = new Form1();
form1.Show();
}
This reads all the files inside the directory and open a another form but It doesn't list all the file names. What should I add?

VulpesRank #55 · 29.2K reputationPosted Apr 14, 2014, 2:17 AM EDT
This could be done by adding the handler (manually) to Form1 or, more conveniently, by using a lambda expression within the calling code:
listView1.View = View.List;
listView1.Location = new System.Drawing.Point(0, 0);
listView1.Name = "listView1";
listView1.Size = new System.Drawing.Size(200, 150);
Form1 form1 = new Form1();
form1.Controls.Add(listView1);
listView1.SelectedIndexChanged += (object sender, EventArgs e)
=>
{
if (listView1.SelectedIndices.Count > 0)
{
form1.textBox1.Text = listView1.SelectedItems[0].Text;
}
else
{
form1.textBox1.Text = "";
}
};
form1.Show();
I note that you've changed the TextBox's Modifiers property to public and so you should be able to access it directly (as I've done above) without going through the Controls collection.
Abhay ShankerRank #375 · 6.7K reputationPosted Apr 13, 2014, 6:52 AM EDT
http://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx
VulpesRank #55 · 29.2K reputationPosted Apr 13, 2014, 12:37 PM EDT
Rano AH125 reputationPosted Apr 13, 2014, 9:34 PM EDT
Hi Vulpes
Yes. this is great.
But, what if one of those listed files has been selected. I need a textBox in the same form that shows the selected file.
Can you help me on this, plz?
Munesh SharmaRank #174 · 13.9K reputationPosted Apr 13, 2014, 9:42 PM EDT
Rano AH125 reputationPosted Apr 13, 2014, 10:22 PM EDT
Hi
I cannot access the textBox in the other form although I set the access modifier to public!
What should I do? Accessing controls from one form to another is challenging :-( I need to access to write a code that
allows the Textbox1 to display the selected file from the listView.
Rano AH125 reputationPosted Apr 13, 2014, 10:22 PM EDT
Hi
I cannot access the textBox in the other form although I set the access modifier to public!
What should I do? Accessing controls from one form to another is challenging :-( I need to access to write a code that
allows the Textbox1 to display the selected file from the listView.
Rano AH125 reputationPosted Apr 14, 2014, 5:19 AM EDT
You are the best
Thank you very much.
I'm wonder! when I am going to be a good developer just like you?!
VulpesRank #55 · 29.2K reputationPosted Apr 14, 2014, 5:22 AM EDT