2016-12-30 35 views
1
private void button1_Click(object sender, EventArgs e) //add data to listBox 
{ 
    if (String.IsNullOrWhiteSpace(textBox1.Text) || String.IsNullOrWhiteSpace(textBox2.Text)) 
    { 
     MessageBox.Show("Proszę uzupełnić wszystkie pola, aby wprowadzić dane", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     return; 
    } 
    listBox1.Items.Add(textBox4.Text + " " + textBox1.Text + " " + textBox2.Text); 
    listBox1.DisplayMember = textBox4.Text; 
} 

private void button2_Click(object sender, EventArgs e) //delete data from listbox 
{ 
    if(listBox1.SelectedIndex == -1) 
     { 
     MessageBox.Show("Proszę zaznaczyć pozycję by usunąć", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     return; 
     } 
    listBox1.Items.Remove(listBox1.SelectedItem); 
} 

private void button3_Click(object sender, EventArgs e) //save to XML button 
{ 
    DataSet ds = new DataSet(); 
    DataTable dt = new DataTable(); 
    dt.TableName = "Tabela"; 
    dt.Columns.Add("Name"); 
    dt.Columns.Add("Surname"); 
    dt.Columns.Add("PESEL"); 
    dt.Columns.Add("Room"); 
    ds.Tables.Add(dt); 

    foreach (string item in listBox1.Items) 
    { 
     DataRow dr = ds.Tables["Tabela"].NewRow(); 
     dr["Name"] = textBox1.Text; 
     dr["Surname"] = textBox2.Text; 
     dr["PESEL"] = textBox3.Text; 
     dr["Room"] = textBox4.Text; 
     ds.Tables["Tabela"].Rows.Add(dr); 
    } 
    ds.WriteXml("D:\\data.xml"); 
} 

private void button4_Click(object sender, EventArgs e) //read from XML button 
    { 
     DataSet ds = new DataSet(); 
     ds.ReadXml("D:\\data.xml"); 

     foreach (DataRow item in ds.Tables["Tabela"].Rows) 
     { 
     listBox1.Items.Add(string.Format("{0} {1} {2}", item["Room"],  item["Name"], item["Surname"])); 
    } 
} 

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DataSet ds = new DataSet(); 
    ds.ReadXml("D:\\data.xml"); 

    foreach (DataRow item in ds.Tables["Tabela"].Rows) 
    { 
     textBox1.Text = item["Name"].ToString(); 
     textBox2.Text = item["Surname"].ToString(); 
     textBox3.Text = item["PESEL"].ToString(); 
     textBox4.Text = item["Room"].ToString(); 
    } 
} 

保存和讀取XML文件時出現問題。當我將從textBoxes添加一些數據到列表框時,只保存了我添加的最後一個索引,但是多次。Visual C#程序只保存最後一個索引到XML文件

例子:

<Tabela> 
    <Name>John</Name> 
    <Surname>Johnson</Surname> 
    <PESEL>123465789</PESEL> 
    <Room>21</Room> 
</Tabela> 
<Tabela> 
    <Name>John</Name> 
    <Surname>Johnson</Surname> 
    <PESEL>123465789</PESEL> 
    <Room>21</Room> 
</Tabela> 
<Tabela> 
    <Name>John</Name> 
    <Surname>Johnson</Surname> 
    <PESEL>123465789</PESEL> 
    <Room>21</Room> 
</Tabela> 

;當我從XML加載數據列表框工作正常,但是當我在文本框中選擇一個指數從這份名單僅出現最後一個索引,而不是我點擊了。

回答

0

您正在使用與代表您的主要業務ListBox控件一個很不錯的辦法實體。這是非常糟糕的...

實際上,爲了使您的解決方案能夠正常工作,每次在預期的文本框中添加新的項目數據時都應該保存到xml文件中,並避免在保存時循環列表框項目...

然後,當發生SelectedIndexChanged事件時,顯示所選項目的詳細信息的方法也是錯誤的,因爲您沒有指向選擇的正確列表框項目,而是指向文件中最後保存的項目每次你在你的列表框中選擇一個新的項目。

因此,更好的方法可能是聲明一個類,這將是你的實體,這個類的一個集合,這將是你的ListBox的數據源:

public class Booking{ 

    string Name; 
    string Surname; 
    string PESEL; //your key? remember to manage duplicates if you insert manually (better if it were generated automatically as unique data (Guid)) 
    string Room; 
} 

System.Collections.Generic.List<Booking> bookings; //instantiate in Form constructor method 

那麼,你的UI控件的事件處理程序將管理這個對象以做你的作業,等等:

當您添加一個新項目:

if (String.IsNullOrWhiteSpace(textBox1.Text) || String.IsNullOrWhiteSpace(textBox2.Text)) 
    { 
     MessageBox.Show("Proszę uzupełnić wszystkie pola, aby wprowadzić dane", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     return; 
    } 

    bookings.Add(new Booking{ 
     Name = textBox1.Text, 
     Surname = textBox2.Text, 
     PESEL = textBox3.Text, 
     Room = textBox4.Text 
    }); 

    listBox.DataSource = null; 
    listBox.DataSource = bookings; //use the datasource property 
    listBox.DisplayMember = "Room"; 
    listBox.ValueMember = "PESEL"; 

當你刪除一個項目:

if (listBox.SelectedIndex == -1) { 
     MessageBox.Show("Proszę zaznaczyć pozycję by usunąć", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
     return; 
    } 

    bookings.Remove(bookings.Find(c => c.PESEL == listBox.SelectedValue as string)); 
    listBox.DataSource = null; 
    listBox.DataSource = bookings; //use the datasource property 
    listBox.DisplayMember = "Room"; 
    listBox.ValueMember = "PESEL"; 

當您保存您的項目:

DataSet ds = new DataSet(); 
DataTable dt = new DataTable(); 
dt.TableName = "Tabela"; 
dt.Columns.Add("Name"); 
dt.Columns.Add("Surname"); 
dt.Columns.Add("PESEL"); 
dt.Columns.Add("Room"); 
ds.Tables.Add(dt); 

foreach (Booking item in bookings) 
{ 
    DataRow dr = ds.Tables["Tabela"].NewRow(); 
    dr["Name"] = item.Name; 
    dr["Surname"] = item.Surname; 
    dr["PESEL"] = item.PESEL; 
    dr["Room"] = item.Room; 
    ds.Tables["Tabela"].Rows.Add(dr); 
} 
ds.WriteXml("D:\\data.xml"); 

當您從文件

//attention to eventual new records appended to the list you may lose them 

    DataSet ds = new DataSet(); 
    ds.ReadXml("D:\\data.xml"); 

    bookings.Clear(); //<<== avoids dirty loading (duplicates) 

    foreach (DataRow item in ds.Tables["Tabela"].Rows) { 
     bookings.Add(new Booking() { 
      Name = item["Name"].ToString(), 
      Surname = item["Surname"].ToString(), 
      PESEL = item["PESEL"].ToString(), 
      Room = item["Room"].ToString() 
     }); 
    } 

    listBox.DataSource = null; 
    listBox.DataSource = bookings; //use the datasource property 
    listBox.DisplayMember = "Room"; 
    listBox.ValueMember = "PESEL"; 

加載當你想顯示選擇列表框的詳細信息TEM:

 Booking booking = bookings.Find(c => c.PESEL == listBox.SelectedValue as string); 

     if (booking == null) return; 

     textBox1.Text = booking.Name; 
     textBox2.Text = booking.Surname; 
     textBox3.Text = booking.PESEL; 
     textBox4.Text = booking.Room; 

您可重構關於列表框清爽的重複語句..

2

在你的代碼有

dr["Name"] = textBox1.Text; 
dr["Surname"] = textBox2.Text; 
dr["PESEL"] = textBox3.Text; 
dr["Room"] = textBox4.Text; 

對於ListBox中的項目每個itteration。由於文本框不會改變,它確實會爲列表框中的每個項目添加一個。

即時通訊思想你需要做一些項目,在你的for循環,而不是文本框

1
foreach (string item in listBox1.Items) 
{ 
    DataRow dr = ds.Tables["Tabela"].NewRow(); 
    dr["Name"] = textBox1.Text; 
    dr["Surname"] = textBox2.Text; 
    dr["PESEL"] = textBox3.Text; 
    dr["Room"] = textBox4.Text; 
    ds.Tables["Tabela"].Rows.Add(dr); 
} 

我不知道你想做的事做,但你可能會想改變一些東西像從列表或其他內容讀取和寫入。

如果您告訴我們您正在使用的技術(asp.net,wpf等),我們可能會幫助您。

0

變化以這種方式listBox1_SelectedIndexChanged方法所選擇的列表框的值被用作過濾器的表,這樣的事情:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DataSet ds = new DataSet(); 
    ds.ReadXml("D:\\data.xml"); 

    //split selected item by spaces 
    var values = listBox1.SelectedItem.ToString().Split(' '); 
    var selectedRow = ds.Tables["Tabela"].AsEnumerable() 
     .Where 
      (r => r["Room"].ToString() == values[0] && 
      r["Name"].ToString() == values[1] && 
      r["Surname"].ToString() == values[2]).FirstOrDefault(); 

    if (selectedRow != null) 
    { 
     textBox1.Text = selectedRow["Name"].ToString(); 
     textBox2.Text = selectedRow["Surname"].ToString(); 
     textBox3.Text = selectedRow["PESEL"].ToString(); 
     textBox4.Text = selectedRow["Room"].ToString(); 
    } 
} 
0

我做了什麼錯?

public partial class Form1 : Form 
{ 
    public class Hotel 
    { 

     string Name; 
     string Surname; 
     string PESEL; 
     string Room; 
    } 
    System.Collections.Generic.List<Hotel> hotel; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     if (String.IsNullOrWhiteSpace(textBox1.Text) || String.IsNullOrWhiteSpace(textBox2.Text)) 
     { 
      MessageBox.Show("Proszę uzupełnić wszystkie pola, aby wprowadzić dane", "Błąd!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      return; 
     } 
     //listBox1.Items.Add(textBox4.Text + " " + textBox1.Text + " " + textBox2.Text); 

     hotel.Add(new Hotel 
     { 
      Name = textBox1.Text, 
      Surname = textBox2.Text, 
      PESEL = textBox3.Text, 
      Room = textBox4.Text 
     }); 

     listBox1.DataSource = null; 
     listBox1.DataSource = hotel; //use the datasource property 
     listBox1.DisplayMember = "Room"; 
     listBox1.ValueMember = "PESEL"; 
    } 

構建之後,我得到這個:

Errors

相關問題