0

나는 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"); 
    } 
} 
+0

에서 [의 FileDialog (http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.aspx) 클래스 시도를 사용하여 수행 할 수 있습니다. – Brian

답변

2

, 내가 올바른 방향을 알려줄 단지 것입니다.

  • 그런 다음 경로를 작성하는 것입니다 FileToWrite에 액세스 public string FileToWrite {get; set;}
  • 같은 사용자 컨트롤에 속성을 추가합니다.

당신은 할 수 없습니다 (또는해야하지) 사용자 컨트롤에서 호출 양식 코드

private void btnSave_Click(object sender, EventArgs e) 
{ 
    StreamWriter sw = new StreamWriter(FileToWrite); 
} 
+0

도와 주셔서 감사합니다. 그러나 이것은 프로젝트 나 관련 클래스 작업이 아닙니다. 이것은 자기 훈련입니다. 똑같은 질문이있어서, 부정 행위 나 뭐 그런 것도 아니 었습니다 (어쨌든 수업을 통해 속임수를 쓴다면 인생에서 도움이되지 않습니다). 우리의 교과서는 사용자 제어에 초점을 두지 않았습니다. 문제는 세트를 생성하거나 얻지 못하는 것입니다. 문제는 Form에서 ListBox.SelectedItem.Text를 호출하여 userControl에있는 btnSave에 놓아야 할 때 발생합니다. Form에 userControl1을 호출하는 방법을 알고 있습니다. '하지만 form에서 userControl을 호출하는 방법을 모르겠습니다. 다시 한번 감사드립니다 :) –

0

. 다른 메커니즘이 필요합니다. 양식에서 컨트롤에 항상 선택된 항목을 알려야하거나 사용자 컨트롤이 정보를 요청해야합니다.

이 두

이벤트

관련 문제