Reputation: 1
I am preparing for my 1st year test and one of the questions I know about is "Create a user control with a textBox and a button, and on form create a listBox" Okay. Now what has to happen is the listBox lists files from a dir. and the textBox in User Control reads the contents of this file(.txt) and the user is allowed to edit the text box, and the save button when click must then overwrite the original file. Now I got the whole textBox reading going using StreamReader. But I can't get the Button in the User Control to get the dir path from the ListBox.
I have to overwrite it using StreamWriter, can't use the FileDialog. It has to get the file name from the ListBox, and then that gets loaded into the StreamWriter dir path which needs to be in btnSave which lies in the UserControl.
I tried doing the vice versa with the userControl userControl1.Text
property with a property I made. Then tried to call it like the userControl from the Form Form1.ItemsName
but didn't work so I scraped that. Now Im having complete coders block.
And Im stumped. I have tried every thing to get the btnSave in the userControl to be able to get the data from the Form listBox.SelectedItem.Text
Form consists of: listBox and UserControl
userControl consists of: textBox and Button(btnSave).
Form1 code:
private void Form1_Load(object sender, EventArgs e)
{
// Reads the content from the file,
//listing other files in dir.
StreamReader sr = new StreamReader(@"C:\C# Resources\Numbers.txt");
string line = sr.ReadLine();
try
{
// Adds content from Numbers.txt into the ListBox
while (line != null)
{
this.listBox1.Items.Add(line);
line = sr.ReadLine();
}
sr.Close();
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message.ToString());
}
finally
{
sr.Close();
}
}
private void listBox1_Click(object sender, EventArgs e)
{
// Reads the name of the file in the list box, looking for the file in the dir.
StreamReader file = new StreamReader(@"C:\C# Resources\" + listBox1.SelectedItem.ToString() + ".txt");
string all = file.ReadToEnd();
//Loads the ListBox_click file's content into the userControl textBox
userControl11.Text = all.ToString();
}
}
UserControl code:
// Creates method so the it can be loaded into form
public string Text
{
set
{
textBox2.Text = value;
}
get
{
return textBox2.Text;
}
}
//Has to overwrite/Save the content changed by the user in TextBox to the same
//directory/file it came from.
private void btnSave_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(@"C:\C# Resources\" + /*Method to get the file path from the Form1 ListBox*/ + ".txt");
}
}
Upvotes: 0
Views: 576
Reputation: 9985
You can't (or shouldn't) call form code from with the user control. You need another mechanism, either the form should let the control know the selected item at all times, or the user control should request the information.
Both of these can be done using events
Upvotes: 0
Reputation: 20585
Since it is a assignment/project work, i will just point you in the right direction.
public string FileToWrite {get; set;}
FileToWrite
which will be the path to write.private void btnSave_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(FileToWrite);
}
Upvotes: 2