C# 我正在尝试创建一个更新按钮,该按钮从列表框中获取一个条目,并存储在文本框中,以便在windowsformsApp中进行编辑
当我选择一个客户条目时,fname、lname和phone将显示在firstname文本框中,我似乎无法将文本框fname分开,因此fname将显示在firstname中,依此类推。我似乎也不能让它更新信息,一旦我编辑它 这是我目前的代码:C# 我正在尝试创建一个更新按钮,该按钮从列表框中获取一个条目,并存储在文本框中,以便在windowsformsApp中进行编辑,c#,arrays,textbox,listbox,windows-forms-designer,C#,Arrays,Textbox,Listbox,Windows Forms Designer,当我选择一个客户条目时,fname、lname和phone将显示在firstname文本框中,我似乎无法将文本框fname分开,因此fname将显示在firstname中,依此类推。我似乎也不能让它更新信息,一旦我编辑它 这是我目前的代码: public partial class Form1 : Form { List<Customer> CustomerDB = new List<Customer>(); private s
public partial class Form1 : Form
{
List<Customer> CustomerDB = new List<Customer>();
private string fName, lName, phone;
public Form1()
{
InitializeComponent();
this.lstBox.SelectedIndexChanged += new System.EventHandler(this.lstBox_SelectedIndexChanged);
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDB();
foreach (Customer Entry in CustomerDB)
{
lstBox.Items.Add(Entry.GetCustomer());
}
}
private void LoadDB()
{
CustomerDB.Add(new Customer { FName ="Jim", LName = "Smith", Phone = "346-2514" });
CustomerDB.Add(new Customer { FName = "Jo", LName = "Baker", Phone = "346-1263" });
CustomerDB.Add(new Customer { FName = "Aimee", LName = "Ellery", Phone = "346-3658" });
CustomerDB.Add(new Customer { FName = "Sam", LName = "Herewini", Phone = "346-9898" });
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnListCustomers_Click(object sender, EventArgs e)
{
DisplayCustomers();
}
private void btnClearList_Click(object sender, EventArgs e)//clearlist button
{
ClearDisplay();
}
private void btnUpdate_Click(object sender, EventArgs e)//update button
{
int index = lstBox.SelectedIndex;
if (index == -1)
{
MessageBox.Show("You must first select the entry you wish to update");
btnAdd.Enabled = false;
}
else
{
//txtBoxFName.Text = lstBox.SelectedItem.ToString();
//txtBoxLName.Text = lstBox.SelectedItem.ToString();
//txtBoxPhone.Text = lstBox.SelectedItem.ToString();
}
//txtBoxFName.Text = lstBox.SelectedItem.ToString();
//else if (index != -1)
//{
// lstBox.Items.RemoveAt(index);
// lstBox.Items.Insert(index, txtBoxFName.Text);
// lstBox.Items.Insert(index, txtBoxLName.Text);
// lstBox.Items.Insert(index, txtBoxPhone.Text);
//}
//lstBox.Items[0] = lstBox.Items[0];
//lstBox.SelectedIndex = -1;//deselects a selection
//txtBoxFName.Text = lstBox.SelectedItem.ToString();
//txtBoxLName.Text = lstBox.SelectedItem.ToString();
//txtBoxPhone.Text = lstBox.SelectedItem.ToString();
//string item = lstBox.GetItemText(lstBox.SelectedItem);
}
private void btnDelete_Click(object sender, EventArgs e)//delete button
{
if (lstBox.SelectedIndex == -1)//if selected index is choosen then can remove entry
{
MessageBox.Show("Please select the entry you wish to delete.");
}
else
{
lstBox.Items.RemoveAt(lstBox.SelectedIndex);
}
}
private void lstBox_SelectedIndexChanged(object sender, EventArgs e)//list box
{
int index = lstBox.SelectedIndex;
if (index != -1)
{
txtBoxFName.Text = lstBox.SelectedItem.ToString(); ;
//txtBoxFName.Text = lstBox.SelectedItem.ToString();
//txtBoxLName.Text = lstBox.SelectedItem.ToString();
//txtBoxPhone.Text = lstBox.SelectedItem.ToString();
}
//fName = txtBoxFName.Text;
//lName = txtBoxLName.Text;
//phone = txtBoxPhone.Text;
//txtBoxFName.Text = lstBox.SelectedItem.ToString();
}
private void btnAdd_Click(object sender, EventArgs e)//add button
{
if (txtBoxFName.Text == string.Empty)
{
MessageBox.Show("Please enter a name in the FirstName text box");
txtBoxFName.Focus();
}
else if (txtBoxLName.Text == string.Empty)
{
MessageBox.Show("Please enter a name in the LastName text box");
txtBoxLName.Focus();
}
else if (txtBoxPhone.Text == string.Empty)
{
MessageBox.Show("Please enter a Phone number in the phone text box");
txtBoxPhone.Focus();
}
else
{
fName = txtBoxFName.Text;
lName = txtBoxLName.Text;
phone = txtBoxPhone.Text;
lstBox.Items.Add(string.Format("{0}\t\t{1}\t\t{2}", fName, lName, phone));
txtBoxFName.Clear();
txtBoxFName.Focus();
txtBoxLName.Clear();
txtBoxFName.Focus();
txtBoxPhone.Clear();
txtBoxFName.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)//clears button clears fname,lname and phone text boxes
{
ClearBoxes();
}
private void ClearBoxes()//method which clears the first name, last name and phone textboxes.
{
txtBoxFName.Clear();
txtBoxFName.Focus();
txtBoxLName.Clear();
txtBoxFName.Focus();
txtBoxPhone.Clear();
txtBoxFName.Focus();
}
private void txtBoxFName_TextChanged(object sender, EventArgs e)
{
fName = txtBoxFName.Text;
}
private void txtBoxLName_TextChanged(object sender, EventArgs e)
{
lName = txtBoxLName.Text;
}
private void txtBoxPhone_TextChanged(object sender, EventArgs e)
{
}
private void txtBoxSearch_TextChanged(object sender, EventArgs e)
{
}
private void btnSearch_Click(object sender, EventArgs e)
{
}
private void ClearDisplay()// method which clears the listbox.
{
lstBox.Items.Clear();
txtBoxSearch.Focus();
}
private void DisplayCustomers()//method, which adds each customer in the CustomerDB list to the listbox on the form, using the GetCustomer method in the class.
{
lstBox.Items.Clear();
foreach (Customer Entry in CustomerDB)
{
lstBox.Items.Add(Entry.GetCustomer());
}
}
}
}
公共部分类表单1:表单
{
List CustomerDB=新列表();
专用字符串fName、lName、phone;
公共表格1()
{
初始化组件();
this.lstBox.SelectedIndexChanged+=新的System.EventHandler(this.lstBox\u SelectedIndexChanged);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
LoadDB();
foreach(CustomerDB中的客户条目)
{
lstBox.Items.Add(Entry.GetCustomer());
}
}
私有void LoadDB()
{
CustomerDB.Add(新客户{FName=“Jim”,LName=“Smith”,Phone=“346-2514”});
CustomerDB.Add(新客户{FName=“Jo”,LName=“Baker”,Phone=“346-1263”});
CustomerDB.Add(新客户{FName=“Aimee”,LName=“Ellery”,Phone=“346-3658”});
CustomerDB.Add(新客户{FName=“Sam”,LName=“Herewini”,Phone=“346-9898”});
}
私有void groupBox1_Enter(对象发送方,事件参数e)
{
}
私有无效btnListCustomers\u单击(对象发送者,事件参数e)
{
显示客户();
}
私有void btnClearList\u单击(对象发送者,事件参数)//清除列表按钮
{
ClearDisplay();
}
私有void btnUpdate\单击(对象发送者,事件参数)//更新按钮
{
int index=lstBox.SelectedIndex;
如果(索引==-1)
{
Show(“您必须首先选择要更新的条目”);
btnAdd.Enabled=false;
}
其他的
{
//txtBoxFName.Text=lstBox.SelectedItem.ToString();
//txtBoxLName.Text=lstBox.SelectedItem.ToString();
//txtBoxPhone.Text=lstBox.SelectedItem.ToString();
}
//txtBoxFName.Text=lstBox.SelectedItem.ToString();
//否则如果(索引!=-1)
//{
//lstBox.Items.RemoveAt(索引);
//lstBox.Items.Insert(索引,txtBoxFName.Text);
//lstBox.Items.Insert(索引,txtBoxLName.Text);
//lstBox.Items.Insert(索引,txtBoxPhone.Text);
//}
//lstBox.Items[0]=lstBox.Items[0];
//lstBox.SelectedIndex=-1;//取消选择所选内容
//txtBoxFName.Text=lstBox.SelectedItem.ToString();
//txtBoxLName.Text=lstBox.SelectedItem.ToString();
//txtBoxPhone.Text=lstBox.SelectedItem.ToString();
//string item=lstBox.GetItemText(lstBox.SelectedItem);
}
私有void btnDelete\u单击(对象发送者,事件参数)//删除按钮
{
if(lstBox.SelectedIndex==-1)//如果选择了所选索引,则可以删除条目
{
MessageBox.Show(“请选择要删除的条目”);
}
其他的
{
lstBox.Items.RemoveAt(lstBox.SelectedIndex);
}
}
私有void lstBox\u SelectedIndexChanged(对象发送方,事件参数e)//列表框
{
int index=lstBox.SelectedIndex;
如果(索引!=-1)
{
txtBoxFName.Text=lstBox.SelectedItem.ToString();
//txtBoxFName.Text=lstBox.SelectedItem.ToString();
//txtBoxLName.Text=lstBox.SelectedItem.ToString();
//txtBoxPhone.Text=lstBox.SelectedItem.ToString();
}
//fName=txtBoxFName.Text;
//lName=txtBoxLName.Text;
//phone=txtBoxPhone.Text;
//txtBoxFName.Text=lstBox.SelectedItem.ToString();
}
私有void btnAdd_单击(对象发送者,事件参数)//添加按钮
{
if(txtBoxFName.Text==string.Empty)
{
MessageBox.Show(“请在FirstName文本框中输入名称”);
txtBoxFName.Focus();
}
else if(txtBoxLName.Text==string.Empty)
{
Show(“请在LastName文本框中输入名称”);
txtBoxLName.Focus();
}
else if(txtBoxPhone.Text==string.Empty)
{
MessageBox.Show(“请在电话文本框中输入电话号码”);
txtBoxPhone.Focus();
}
其他的
{
fName=txtBoxFName.Text;
lName=txtBoxLName.Text;
phone=txtBoxPhone.Text;
添加(string.Format(“{0}\t\t{1}\t\t{2}”,fName,lName,phone));
txtBoxFName.Clear();
txtBoxFName.Focus();
txtBoxLName.Clear();
txtBoxFName.Focus();
txtBoxPhone.Clear();
txtBoxFName.Focus();
}
}
private void btnClear\u Click(object sender,EventArgs e)//清除按钮清除fname、lname和phone文本框
{
ClearBox();
}
private void clearboxs()//清除名字、姓氏和电话文本框的方法。
{
txtBoxFName.Clear();
txtBoxFName.Focus();
txtBoxLName.Clear();
txtBoxFName.Focus();
txtBoxPhone.Clear();
txtBoxFName.Focus();
}
私有void txtBoxFName_TextChanged(对象发送方,EventAr