使用C#中的下一个/上一个按钮显示XML记录
本文关键字:按钮 显示 XML 记录 上一个 下一个 使用 | 更新日期: 2023-09-27 17:59:26
我正试图使用"下一步/上一步"按钮循环浏览我的XML记录。我已经编写了获取第一个供应商的代码,但我用来获取第二个供应商和第三个供应商等的代码不起作用。我已经包含了检索数据的初始代码、我尝试的循环以及我得到的XML文件。
List <Vendor> v = VendorDB.GetVendors();
Vendor vendor1 = v[0];
txtName.Text = vendor1.Name.ToString();
txtAddress.Text = vendor1.Address.ToString();
txtCity.Text = vendor1.City.ToString();
txtZip.Text = vendor1.Zip.ToString();
txtPhone.Text = vendor1.Phone.ToString();
txtSalesYTD.Text = vendor1.YTD.ToString();
txtSalesRep.Text = vendor1.Contact.ToString();
txtComments.Text = vendor1.Comment.ToString();
private void btnNext_Click(object sender, EventArgs e) //try doing if statements for each vendor
{
List<Vendor> v = VendorDB.GetVendors();
for (int i = 0; i < v.Count; i++)
{
Vendor vendor1 = v[i];
txtName.Text = vendor1.Name.ToString();
txtAddress.Text = vendor1.Address.ToString();
txtCity.Text = vendor1.City.ToString();
txtZip.Text = vendor1.Zip.ToString();
txtPhone.Text = vendor1.Phone.ToString();
txtSalesYTD.Text = vendor1.YTD.ToString();
txtSalesRep.Text = vendor1.Contact.ToString();
txtComments.Text = vendor1.Comment.ToString();
}
}
<?xml version="1.0" encoding="utf-8"?>
<Vendors>
<Vendor>
<Name>Scarmado Produce Co.</Name>
<Address>244 Southwest Dr.</Address>
<City>Bryan</City>
<State>Texas</State>
<ZIP>77805</ZIP>
<Phone>9797784456</Phone>
<YTD>2500000</YTD>
<Comment>Great vendor</Comment>
<Contact>James Scarmado</Contact>
<DefaultDiscount>15</DefaultDiscount>
</Vendor>
<Vendor>
<Name>Talbort Restaurant Supplies</Name>
<Address>2533 Broadway Ave.</Address>
<City>New Orleans</City>
<State>LA USA</State>
<ZIP>89554</ZIP>
<Phone>7664028762</Phone>
<YTD>1589000</YTD>
<Comment></Comment>
<Contact>Joan Fishing</Contact>
<DefaultDiscount>20</DefaultDiscount>
</Vendor>
<Vendor>
<Name>Famous Meats</Name>
<Address>222 Swey Ave.</Address>
<City>Bangkok</City>
<State>Thailand</State>
<ZIP>75110</ZIP>
<Phone>883778723</Phone>
<YTD>186000</YTD>
<Comment>Good vendor</Comment>
<Contact>Faye Smith</Contact>
<DefaultDiscount>15</DefaultDiscount>
</Vendor>
<Vendor>
<Name>Duetsch Products</Name>
<Address>253 Hamburg Ave.</Address>
<City>Hamburg</City>
<State>Germany</State>
<ZIP>APO 76632</ZIP>
<Phone>109019834</Phone>
<YTD>258260</YTD>
<Comment>Difficult delivery system</Comment>
<Contact>Ian Friberg</Contact>
<DefaultDiscount>20</DefaultDiscount>
</Vendor>
<Vendor>
<Name>Allegheny Drink Products</Name>
<Address>2862 Hamilton St.</Address>
<City>Dallas</City>
<State>Texas</State>
<ZIP>80256</ZIP>
<Phone>7149872957</Phone>
<YTD>875390</YTD>
<Comment></Comment>
<Contact>Cheryl Montana</Contact>
<DefaultDiscount>15</DefaultDiscount>
</Vendor>
<Vendor>
<Name>Best Cheeses Around</Name>
<Address>2732 Gouda Ct.</Address>
<City>Lincoln</City>
<State>Nebraska USA</State>
<ZIP>67499</ZIP>
<Phone>4028776509</Phone>
<YTD>679270</YTD>
<Comment></Comment>
<Contact>Dave Pfister</Contact>
<DefaultDiscount>15</DefaultDiscount>
</Vendor>
</Vendors>
尝试存储供应商列表和当前供应商的索引以显示在全局变量中。然后显示给下一个/上一个供应商,只需增加/减少当前供应商索引并刷新UI:
//Declare these as global variable :
List<Vendor> vendors;
int currentVendorIndex = 0;
//populate vendors once (f.e in constructor)
vendors = VendorDB.GetVendors();
private void btnNext_Click(object sender, EventArgs e)
{
currentVendorIndex++;
//if index exceeds maximum available index, reset to 0
if(currentVendorIndex == vendors.Count) currentVendorIndex = 0;
Vendor vendor1 = vendors[currentVendorIndex];
txtName.Text = vendor1.Name.ToString();
txtAddress.Text = vendor1.Address.ToString();
txtCity.Text = vendor1.City.ToString();
txtZip.Text = vendor1.Zip.ToString();
txtPhone.Text = vendor1.Phone.ToString();
txtSalesYTD.Text = vendor1.YTD.ToString();
txtSalesRep.Text = vendor1.Contact.ToString();
txtComments.Text = vendor1.Comment.ToString();
}
更新:
对于上一个按钮,您可以使用与更新相同的逻辑,除了增量部分需要替换为减量:
currentVendorIndex--;
//if index decremented below minimum, reset to maximum index
if(currentVendorIndex < 0) currentVendorIndex = vendors.Count-1;