2016-07-24 6 views
0

Visual Studio를 사용하여 Windows 양식 응용 프로그램을 만듭니다. 내 시스템은 대학 모듈 정보를 작성하고 보는 역할을 담당합니다. 그러나 현재는 시스템이 부팅 될 때 Tool Strip Menu를 채울 것입니다. 새 항목을 만들 때이 목록에 새 항목을 추가하는 방법이 필요합니다. 새 레코드는 다른 형식으로 만들어졌으며 Form2를 만드는 중 하나에서 Form1의 ToolStripDropdown에 항목을 추가하는 방법을 알 수 없습니다 (아래 그림 참조). ToolStripMenuItem을 공용으로 단순히 만들려고했지만 작동하지 않습니다. 누구든지 나를 도울 수 있습니까? 드롭에 항목을 추가하는다른 양식의 ToolStripMenuItem 드롭 다운 목록에 항목 추가

The ToolStrip as present of ToolStrip1

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace Mod_Note_2._0 
{ 
    public partial class Form2 : Form 
    { 
     public string newmodcode; 
     public string baseText = "Enter information related to the module section here"; 

     public string newmodtitle; 
     public string newmodsyn; 
     public string newmodlo; 
     public string newmodassign; 

     public Form2() 
     { 
      InitializeComponent(); 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      newmodcode = NewModuleCode.Text; 

      //Adds the file path and name to the module code to create the file names 
      newmodtitle = newmodcode + "title.txt"; 
      newmodsyn = newmodcode + "synopsis.txt"; 
      newmodlo = newmodcode + "LOs.txt"; 
      newmodassign = newmodcode + "assignments.txt"; 

      //Adds the file path the new module code so it can create the file 
      string newmodcodepath = newmodcode + "code.txt"; 

      //Creates the new files with the filler text as default 
      File.WriteAllText(newmodcodepath, newmodcode); 
      File.WriteAllText(newmodtitle, baseText); 
      File.WriteAllText(newmodsyn, baseText); 
      File.WriteAllText(newmodlo, baseText); 
      File.WriteAllText(newmodassign, baseText); 

현재 코드 : 여기 대답

ToolStripItem newDropDownItem = new ToolStripMenuItem(); 
       newDropDownItem.Text = newmodcode; 

       Form1.modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem); 
  Close(); 
     } 

     //Simple cancelation sequence closing the entry form 
     private void button2_Click(object sender, EventArgs e) 
     { 
      Close(); 
     } 
    } 
} 
+0

당신은 두 번째 형태의 첫 번째 양식에 대한 참조가 필요합니다. 이렇게 : http://stackoverflow.com/a/38460510/3185569 – user3185569

답변

0

(https://stackoverflow.com/a/32916403/5378924), 하나에서 일부 컨트롤을 추가하는 방법 양식을 다른 양식.

를 Form1 :

public partial class Form1 : Form 
{ 
    public static Form1 Instance { get; private set; } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
      Instance = this; 
    } 
} 

형식 2는 :

ToolStripItem newDropDownItem = new ToolStripMenuItem(); 
newDropDownItem.Text = newmodcode; 
Form1.Instance.modulesToolStripMenuItem.DropDownItems.Add(newDropDownItem); 
+0

이 방법은 다음 Form1의 인스턴스를 연 후 Instance' 속성은 새로운 폼을 가리킬 것입니다. –

+0

[이 옵션들] (http://stackoverflow.com/a/38552832/3110834)을 보시면 도움이 될 것입니다. –

+0

@RezaAghaei Form1은 사용자가 응용 프로그램을 종료 할 때까지 영원히 머물러 있다고 생각합니다. Form1은 응용 프로그램의 시작점이며 한 번만 열리고 닫힙니다. –