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?
VulpesPosted Apr 14, 2014, 6:17 AM
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.
VulpesPosted Apr 14, 2014, 9:22 AM
Rano AHPosted Apr 14, 2014, 9:19 AM
You are the best
Thank you very much.
I'm wonder! when I am going to be a good developer just like you?!
Rano AHPosted Apr 14, 2014, 2:22 AM
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 AHPosted Apr 14, 2014, 2:22 AM
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.
Munesh SharmaPosted Apr 14, 2014, 1:42 AM
Rano AHPosted Apr 14, 2014, 1:34 AM
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?
VulpesPosted Apr 13, 2014, 4:37 PM
Abhay ShankerPosted Apr 13, 2014, 10:52 AM
http://msdn.microsoft.com/en-us/library/dd997370(v=vs.110).aspx