나는 1 년차 테스트를 준비하고 있으며 "텍스트 상자와 단추가있는 사용자 정의 컨트롤 만들기 및 폼에서 listBox 만들기"라는 질문에 대해 알고 있습니다. . 이제 일어날 일은 listBox가 dir의 파일을 나열하는 것입니다. 사용자 정의 컨트롤의 textBox는이 파일 (.txt)의 내용을 읽고 사용자가 텍스트 상자를 편집 할 수있게하고, 클릭 할 때 저장 단추는 원본 파일을 덮어 씁니다. 이제는 전체 textBox 읽기가 StreamReader를 사용하게되었습니다. 그러나 사용자 정의 컨트롤에서 Button을 가져 와서 ListBox에서 dir 경로를 가져올 수 없습니다.양식 컨트롤을 사용자 정의 컨트롤로 가져 오기 C#
StreamWriter를 사용하여 덮어 써야합니다. FileDialog를 사용할 수 없습니다. ListBox에서 파일 이름을 가져와야하고 UserControl에있는 btnSave에 있어야하는 StreamWriter 경로로로드됩니다.
내가 만든 속성으로 userControl userControl1.Text
속성으로 반대를 시도했습니다. 그런 다음 양식 Form1.ItemsName
에서 userControl처럼 호출하려고 시도했지만 작동하지 않아서 긁어 냈습니다. 이제 완전한 코더 블록을 가지고 있습니다.
그리고 난처한 상황입니다. 텍스트 상자 및 버튼 (btnSave) :
의 UserControl 구성 목록 상자와 UserControl을 : 나는 listBox.SelectedItem.Text
양식으로 구성되어 양식에서 데이터를 얻을 수 있도록 해당 UserControl에 btnSave을 얻을 수있는 모든 것을 시도했다.
Form1 코드 :
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을 코드 : 그것은 할당/프로젝트 작업이기 때문에
// 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");
}
}
에서 [의 FileDialog (http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.aspx) 클래스 시도를 사용하여 수행 할 수 있습니다. – Brian