2015-04-08 40 views
-2

我想從另一個表格中獲取選定的項目以顯示在文本框上。從另一個表格中獲取列表框

我有兩種形式。 MainFormFoodRegisterForm。在Mainform有一個不同的動物列表框,並在FoodRegisterForm有一個文本框。

當用戶在Mainform列表框中選擇一個項目時,我希望所選項目顯示在RegisterForm上的文本框中。

這裏是我試了一下:

public partial class FoodRegister : Form 
    { 
     private MainForm mainform; 
     public FoodRegister() 
     { 
      mainform = new MainForm(); 
      InitializeComponent(); 
      mainform.Show(); 

     } 

     private void Nametxt_TextChanged(object sender, EventArgs e) 
     { 
      Nametxt.Text = mainform. //Don't know how to go from here 

     } 

    } 

,我試圖讓列表框被命名爲Animallst 的問題是,我不知道如何從mainFormAnimallstFoodregisterForm

更新:

這裏是MainForm

public partial class MainForm : Form 
    { ///<summary> Instance of AnimalManager </summary> 
     private AnimalManager animalmgr = null; 
     private FoodSchedule m_foodManager = new FoodSchedule(); 
     private RecipeManager m_recipeManager = new RecipeManager(); 

     public MainForm() 
     { 
      //Visual Studio initializations 
      InitializeComponent(); 

      //My initializations 
      InitializeGUI(); 
      ///<summary> Fills the combobox with the values of the Enums </summary> 
      Gendercmb.DataSource = Enum.GetValues(typeof(GenderType)); 
      Categorylst.DataSource = Enum.GetValues(typeof(Categorytype)); 
      animalmgr = new AnimalManager(); 
     } 

     private void InitializeGUI() 
     { 
     } 

     //public string GetListBoxSelectedItem() 
     //{ 
     // if (Animallst.SelectedItem != null) 
     //  return Animallst.SelectedItem.ToString(); 
     // return string.Empty; 
     //} 


     /// <summary> 
     /// Read inputs from the user. 
     /// </summary> 
     /// <param name="Animal"></param> 
     private void ReadInput(Animal animal) 
     { 
      //Reads name 
      animal.Name = ReadName(); 
      //Reads age 
      animal.Age = ReadAge(); 
      //Reads gender 
      animal.Gender = this.Gendercmb.GetItemText(this.Gendercmb.SelectedItem); 

      //Reads teeth 
      Mammal mammal = animal as Mammal; 
      if (mammal != null) 
      { 
       mammal.Teeth = ReadTeeth(); 

      } 
      //Reads barklevel 
      Dog dog = animal as Dog; 
      if (dog != null) 
      { 
       dog.BarkLevel = ReadBarklevel(); 

      } 
      //Reads jumplevel 
      Cat cat = animal as Cat; 
      if (cat != null) 
      { 
       cat.Jumplevel = ReadJumpLevel(); 
      } 
      //Reads toxicity 
      Snake snake = animal as Snake; 
      if (snake != null) 
      { 
       snake.ToxicLevel = ReadToxicity(); 
      } 
      //Reads camouflage 
      Lizard lizard = animal as Lizard; 
      if (lizard != null) 
      { 
       lizard.CamouflageLevel = ReadCamouflage(); 
      } 
      //Reads tail 
      Reptile reptile = animal as Reptile; 
      if (reptile != null) 
      { 
       reptile.TailLenght = ReadTail(); 

      } 

     } 
     ///<summary> 
     ///Assigns the textfield to local variables and returns them 
     ///</summary> 
     private int ReadAge() 
     { 
      int age = 0; 

      int.TryParse(Agetxt.Text, out age); 

      return age; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadBarklevel() 
     { 
      int bark = 0; 

      int.TryParse(barktxt.Text, out bark); 

      return bark; 

     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadJumpLevel() 
     { 
      int jump = 0; 

      int.TryParse(jumptxt.Text, out jump); 

      return jump; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadToxicity() 
     { 
      int Toxicity = 0; 

      int.TryParse(Toxicitytxt.Text, out Toxicity); 

      return Toxicity; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadCamouflage() 
     { 
      int Camouflage; 

      int.TryParse(Camouflagetxt.Text, out Camouflage); 

      return Camouflage; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadTeeth() 
     { 
      int teeth = 0; 

      int.TryParse(teethtxt.Text, out teeth); 

      return teeth; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private string ReadName() 
     { 
      string name = ""; 
      name = Nametxt.Text; 
      return name; 
     } 
     ///<summary> Assigns the textfield to local variables and returns them </summary> 
     private int ReadTail() 
     { 
      int tail = 0; 

      int.TryParse(tailtxt.Text, out tail); 

      return tail; 
     } 

     //-- 
     /// <summary> 
     /// Add mammals animals if mammal is selected 
     /// </summary> 
     /// <returns="Either Dog or Cat"></returns> 
     private Mammal addMammal() 
     { 
      Mammal result; //variable 

      switch ((MammalType)Animallst.SelectedIndex) 
      { //result is dog 
       case MammalType.Dog: 
        { 

         result = new Dog(); 
         break; 
        } 
       //result is cat 
       case MammalType.Cat: 
        { 
         result = new Cat(); 
         break; 
        } 

       default: 
        throw new Exception("Animal type not found."); 
      } 

      ReadInput(result); //read inputs 

      return result; //returns the result 
     } 


     /// <summary> 
     /// Add Reptile animals if reptile is selected 
     /// </summary> 
     /// <returns="Either Snake or reptile"></returns> 
     private Reptile AddReptile() 
     { 
      Reptile result; 

      switch ((ReptileType)Animallst.SelectedIndex) 
      { 
       case ReptileType.Snake: 
        { 
         result = new Snake(); 
         break; 
        } 
       case ReptileType.Lizard: 
        { 
         result = new Lizard(); 
         break; 
        } 

       default: 
        throw new Exception("Animal type not found."); 
      } 

      ReadInput(result); 

      return result; 
     } 

     /// <summary> 
     /// When user clicks "Add to list" 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void button1_Click(object sender, EventArgs e) 
     { 
      Resultlst.ClearSelected(); 
      switch ((Categorytype)Categorylst.SelectedIndex) 
      {//if Mammal is selected - Adds the mammal animal selected from above. 
       case Categorytype.Mammal: 
        { 
         Mammal mammal = addMammal(); 
         animalmgr.Add(mammal); 
         break; 
        } 

       //Same but for Reptile animals 
       case Categorytype.Reptile: 
        { 
         Reptile m_reptile = AddReptile(); 
         animalmgr.Add(m_reptile); 
         break; 
        } 


      } 
      UpdateResults(); 
     } 

     /// <summary> 
     /// Uppdates the list 
     /// </summary> 
     private void UpdateResults() 
     { 
      Resultlst.Items.Clear(); //Erase current list 
      //Get one elemnet at a time from manager, and call its 
      //ToString method for info - send to listbox 
      for (int index = 0; index < animalmgr.Count; index++) 
      { 
       Animal animal = animalmgr.GetAt(index); 

       //Adds to the list. 
       Resultlst.Items.Add(animal); 

       Resultlst.DisplayMember = "Description"; 

      } 
     } 

     /// <summary> 
     /// Updates the FoodSchedule 
     /// </summary> 
     private void UpdateFoodSchedule() 
     { 
      //Gets Eater type of selected animal 
      Animal theanimal = (Animal)Resultlst.SelectedItem; 
      if (Resultlst.SelectedIndex < 0) 
      { 
       return; 
      } 

      string eater = ""; 
      if(theanimal.GetEaterType() == EaterType.Carnivore) 
      { 
       eater = "Meat eater"; 
      } 
      else if (theanimal.GetEaterType() == EaterType.Herbivore) 
      { 
       eater = "Plant eater"; 
      } 

      else if (theanimal.GetEaterType() == EaterType.Omnivorous) 
      { 
       eater = "All eater"; 
      } 


      EaterTypetxt.Text = eater; 

      //Gets Food schedule of selected animal 

      FoodSchedule animal = theanimal.GetFoodSchedule(); 

      //Adds to the list. 
      foodlst.DataSource = animal.FoodDescriptionList; 
     } 
     /// <summary> 
     /// Adds the UppdateSchedule to this event handeler so 
     /// that the FoodSchedule updates when an item on the list is selected. 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void Resultlst_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      UpdateFoodSchedule(); 
     } 

     private void foodbtn_Click(object sender, EventArgs e) 
     { 
      FoodRegister foodForm = new FoodRegister(); 
      foodForm.Show(); 
     } 

     private void staffbtn_Click(object sender, EventArgs e) 
     { 
      StaffForm staffForm = new StaffForm(); 
      staffForm.Show(); 
     } 

     private void deletebtn_Click(object sender, EventArgs e) 
     { 
      Delete(); 
      UpdateResults(); 
     } 
     private void Delete() 
     { 
      animalmgr.DeleteAt(Resultlst.SelectedIndex); 
     } 



     } 
+0

可以發佈主要形式的代碼? –

+0

請發佈與您的問題相關的所有相關代碼..也在代碼中您要分配'Nametxt.Text = mainform'您需要根據範圍深入一層,並執行類似於'Nametxt'的操作。 Text = mainform.listbox.SelectItem'例如,但不能看到在MainForm中的代碼 – MethodMan

+0

查看更新的問題 –

回答

1

嘗試這樣做。它可能工作。

public string GetListBoxSelectedItem() 

    { 
     if(yourListBox.SelectedItem != null) 
      return yourListBox.SelectedItem.ToString(); 
     return string.Empty; 
    } 

FoodRegister類:

private void Nametxt_TextChanged(object sender, EventArgs e) 

    { 
     Nametxt.Text = mainform.GetListBoxSelectedItem() 
    } 
0

如何在MainForm中創建一個構造函數,它允許您將您的FoodRegister表單傳遞給它。

例如:

的MainForm

private FoodRegister foodReg { get; set; } 

public MainForm(FoodRegister reg) 
{ 
    foodReg = reg; 
} 

private Animallst_onSelectionChanged(object sender, EventArgs args) 
{ 
    foodReg.Nametxt.Text = (sender as ListBox).SelectedItem.ToString(); 
} 

FoodRegister

public FoodRegister() 
    { 
     mainform = new MainForm(this); 
     InitializeComponent(); 
     mainform.Show(); 

    } 
0

在你的MainForm添加一個方法:

public string GetListBoxSelectedItem() 
{ 
    if(yourListBox.SelectedItem != null) 
     return yourListBox.SelectedItem.ToString(); 
    return string.Empty; 
} 

,並在FoodRegister:

private void Nametxt_TextChanged(object sender, EventArgs e) 
{ 
    Nametxt.Text = mainform.GetListBoxSelectedItem() 
} 
相關問題