我希望从另一个中获取所选项目以显示在文本框中。
我有两种形式。 MainForm
和FoodRegisterForm
。在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
问题是我不知道如何从Animallst
到mainForm
FoodregisterForm
更新
以下是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 :(得分: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()
}
答案 1 :(得分: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();
}
答案 2 :(得分:0)
在你的主窗体中添加一个方法:
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()
}