[英]C# Form, trying to change random pictureBox to a random image with timer
[英]Trying to change output for a timer in a form in c#
我需要在C#中使表單具有計時器,並具有將成為計時器顯示的標簽。 該標簽首先需要是一個通用標簽,說它是一個計數器,但是當計時器啟動時,它需要顯示計數。 目前,我將顯示的數字調小了,但是它需要是可以調整計數的控件,它確實可以做到。 它只是不能成為唯一的計數器。
這是我的作業:
創建一個Windows應用程序。 在主窗體中,創建一個名為“ lTickCount”的標簽。 創建一個名為“ tPeriodic”的計時器,並選擇一個數字控件。 每次計時器“滴答”增加一個整數時,在lTickCount中將該整數值顯示為字符串。 使用數字控件以交互方式更改計時器的更新速率。
我認為我已經正確地完成了所有工作,除了粗體部分。 最后,我嘗試在標簽和櫃台上都加上一個字符串。 我知道我不應該兩者兼有,我只是想向您展示我試圖幫助獲得更好反饋的兩件事:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "AAAAAAAA AAAAAAAA ########";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
TickCounter.Text = "The timer has started";
tPeriodic.Enabled = true;
}
else
{
TickCounter.Text = "The timer has ended";
tPeriodic.Enabled = false;
}
}
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
TickCounter.Text = TickCounter.Value.ToString();
}
private void tPeriodic_Tick(object sender, EventArgs e)
{
TickCounter.Value += 1;
}
private void label1_Click(object sender, EventArgs e)
{
TickCounter.Text = TickCounter.Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
有人可以幫助我弄清楚我做錯了什么,並以正確的方式指出我嗎?
如果要嘗試添加到字符串(標簽值),則需要先將其轉換為Integer。
有幾種方法可以做到這一點:
TextCount.Text = (Convert.ToInt32(TextCount.Text) + 1).ToString();
是一種方法,當然您仍然可以使用+ =或任何其他數學語法來基本加+1。
您還可以使用tryParse,實際上,這可能應該首先用於驗證您擁有整數:
int count;
if (int.TryParse(TextCount.Text, out count))
{
count++;
TextCount.Text = count.ToString();
}
int count;
int tmrInterval = 1000; //1 sec
private void tPeriodic_Tick(object sender, EventArgs e)
{
count++;
lTickCount.Text = count.ToString();
}
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
if (TickCounter.Value == 0)
{
return; // or stop the timer
}
tPeriodic.Interval = TickCounter.Value * tmrInterval;
}
tPeriodic.Interval是到下一個刻度的時間(以毫秒為單位)。 您正在根據tmrInterval和數字控件的值更新計時器間隔。 您可以更改時間間隔或我自己編寫的公式。
瓦爾特
好的,我發現:
tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value);
似乎在numericupdown類中起作用。
謝謝您的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.