[英]Creating a timer from xml time
我已經從 XML 中提取了一個時間並將其放入列表視圖中,但我希望它倒計時。 0運氣如何做到這一點。
這是我獲取時間和一些圖片的代碼;
Private void ListViewTrainingQue()
{
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
string string2 = "https://api.eveonline.com/char/SkillQueue.xml.aspx?keyID=4602486&&vCODE=BHGVeXQkRLKLkIkZQHdeyUxmUz9EfUwbvGzoc2eO4ZR8kRMYxk8PbD4LMwLF7BvH";
// Add Columns to listview
listView1.Columns.Add("Name", 50);
listView1.Columns.Add("Level", 50);
listView1.Columns.Add("Remaing Time", 100);
listView1.Columns.Add("Bar", 100);
// string array1 = "2015-10-23 13:00";
// Create Array to return values to.
string[] arr = new string[3];
ListViewItem item;
XmlDocument XMLtrans = new XmlDocument();
XMLtrans.Load(string2);
XmlNodeList TRnodelist = XMLtrans.SelectNodes("/eveapi/result/rowset/row");
foreach (XmlNode xmlnode in TRnodelist)
{
string array1 = xmlnode.Attributes["endTime"].InnerText;
var date = DateTime.Parse(array1);
var Timespan = date - DateTime.Now;
if (xmlnode.Attributes["typeID"] != null)
arr[0] = xmlnode.Attributes["typeID"].InnerText;
if (xmlnode.Attributes["level"] != null)
arr[1] = xmlnode.Attributes["level"].InnerText;
if (xmlnode.Attributes["endTime"] != null)
arr[2] = string.Format("{0}h {1}m {2}s", Timespan.Hours, Timespan.Minutes, Timespan.Seconds);
item = new ListViewItem(arr);
listView1.Items.Add(item);
}
}
這是它的外觀;
創建ListViewItem
將提取的DateTime
設置為item
Tag
:
item = new ListViewItem(arr);
item.Tag= date;
然后在表單中添加一個計時器並將Interval
設置為1000
並將其Enabled
為true
並處理Tick
事件:
private void timer1_Tick(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
var timeSpan = ((DateTime)item.Tag) - DateTime.Now;
item.SubItems[2].Text = string.Format("{0}h {1}m {2}s",
timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.