我尝试使用“下一个/上一个”按钮循环浏览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>
答案 0 :(得分:1)
尝试存储当前供应商的供应商列表和索引以在全局变量中显示。然后要显示给next / prev供应商,只需增加/减少当前供应商索引并刷新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;