最好是在文本框的datagridview复制文本的方式 [英] Better way to copy text in a textbox to datagridview

查看:182
本文介绍了最好是在文本框的datagridview复制文本的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!



我有这个问题,在文本框中每个文本的变化,在datagriview所选项目应该复制它的价值。我有这样的代码,但它落后时,我在文本框中键入(如非常快)。



有没有更好的办法W / O滞后做到这一点?



请帮忙...



下面是我到目前为止有:

 私人无效txtText_TextChanged(对象发件人,EventArgs五)
{
DataGridView1 [2,POS] .value的= txtText.Text;
}


解决方案

您可能需要限制被处理的事件数。做你的要求使您可以使用文本框 验证引发LostFocus 事件呢?



如果不是你可以看看的接收掐死你的框TextChanged 事件。这可以像这样实现:

 的IObservable< EventPattern< EventArgs的>> 。可观察= Observable.FromEventPattern(
txtText,框TextChanged)节流(TimeSpan.FromMilliseconds(500))
.Subscribe(EP = GT; DataGridView1 [2,POS] .value的= txtText.Text; );

您也可以扼杀一个的 定时

 定时器myTimer =新的Timer(); 
myTimer.Interval = 500;
myTimer.Tick = OnTimerTick;

私人无效OnTimerTick(对象o,EventArgs五)
{
myTimer.Stop();
DataGridView1 [2,POS] .value的= txtText.Text;
}

私人无效txtText_TextChanged(对象发件人,EventArgs五)
{
如果myTimer.Start()(myTimer.Enabled!);
}


Good day!

I have this problem that every text change in the text box, selected item in the datagriview should copy its value. I have this code but it lags when I type(like very fast) in the textbox.

Is there any better way to do this w/o lagging?

Please help...

Here's what I have so far:

private void txtText_TextChanged(object sender, EventArgs e)
    {
        DataGridView1[2, pos].Value = txtText.Text;
    }

解决方案

You may need to limit the number of events that are handled. Do your requirements allow you to use the TextBox Validated or LostFocus events instead?

If not you could look into Rx and throttle your TextChanged event. This can be achieved like so:

IObservable<EventPattern<EventArgs>> observable = Observable.FromEventPattern(
  txtText, "TextChanged").Throttle(TimeSpan.FromMilliseconds(500))
  .Subscribe(ep=> DataGridView1[2, pos].Value = txtText.Text;);

You could also throttle with a Timer.

Timer myTimer = new Timer();
myTimer.Interval = 500;
myTimer.Tick = OnTimerTick;

private void OnTimerTick(object o, EventArgs e)
{
  myTimer.Stop();
  DataGridView1[2, pos].Value = txtText.Text;
}

private void txtText_TextChanged(object sender, EventArgs e)
{
   if(!myTimer.Enabled) myTimer.Start();
}

这篇关于最好是在文本框的datagridview复制文本的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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