每个周期自动控制一个textBox [英] Automatic control of a textBox each period

查看:85
本文介绍了每个周期自动控制一个textBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是C#的初学者.我想制作一个可以捕捉的功能
通过签入textBox来获取文本文件的内容
每2分钟输入一次文本内容.当在文本框中输入的值等于1时,将触发警报.我设法在texteBox中捕获了文本文件的内容.当它等于1时表示警报下降.我的问题是如何每2分钟控制一次此文本框.这里是我的代码:

Hello, I am a beginner in c #. I want to make a function that captures
the contents of a text file by checking in a textBox
text content every 2 minutes.when the value entered in the textBox is equal to 1 an alarm will be triggered. I managed to capture content of the text file in the texteBox. when it equal to 1 is an alarm declenche.My problem is how to control this textBox every 2 minutes.here my code:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace streamReader
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Creating a new stream-reader and opening the file.
System.IO.StreamReader StreamReader1 = new System.IO.StreamReader(@"..//..//déclenche priorité//priorité.txt");

//Reading Line from file
string newLine;
while ((newLine = StreamReader1.ReadLine()) != null)


//Writing the read line to textBox
textBox1.AppendText(newLine);

//Close the file
StreamReader1.Close();
}






private void textBox1_TextChanged(object sender, EventArgs e)
{


if (textBox1.Text == "1")
{
SoundPlayer player = new SoundPlayer();
string chemin = "..//..//Sequences//alarme.wav";
player.SoundLocation = chemin;
player.Play();
}

}


}
}


感谢您对我的帮助.


thank you for helping me

推荐答案

请参阅此CP文章中有关Timer的内容. 初学者使用计时器的指南 [所有有关.NET计时器的信息-比较 [ System.Windows.Forms.Timer [ ^ ]并设置计时器.间隔和滴答事件处理程序.
然后只需使用timer1.Start();
See this CP article about Timer. Pre-beginner''s guide to using a timer[^] and All about .NET Timers - A Comparison[^]

The basic is. Create timer and setup. Tell interval, the method to be called and then start the timer.


Drag and drop a System.Windows.Forms.Timer[^] in the designer and setup the timer. Interval and Tick event handler.
Then just start the timer with timer1.Start();


启动计时器.最好使用线程而不是计时器解决此问题.实际上,它要容易得多,并且结果也更可靠.切勿在可以使用线程的地方使用计时器.

请查看我过去的答案,其中的解释如下:
C#中的计时器线程 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].


—SA
This problem is best solved using threads, not timers. It''s actually much easier and gives more robust results. Never use a timer where a thread can be used.

Please see my past answer where this is explained:
Timer Threading in C#[^].
Please see a collection of my past answers on threads here:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].


—SA


这篇关于每个周期自动控制一个textBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆