Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 他问什么?此外,为什么这是选择的答案?@Krythic,这是2011年的一个正确答案,并且被广泛接受。我猜事件onChange()在Visual Studio 2015(以及之后?)中被称为TextChanged()。如果您正在寻找这个问题的当前答案,我相_C#_Textbox - Fatal编程技术网

C# 他问什么?此外,为什么这是选择的答案?@Krythic,这是2011年的一个正确答案,并且被广泛接受。我猜事件onChange()在Visual Studio 2015(以及之后?)中被称为TextChanged()。如果您正在寻找这个问题的当前答案,我相

C# 他问什么?此外,为什么这是选择的答案?@Krythic,这是2011年的一个正确答案,并且被广泛接受。我猜事件onChange()在Visual Studio 2015(以及之后?)中被称为TextChanged()。如果您正在寻找这个问题的当前答案,我相,c#,textbox,C#,Textbox,他问什么?此外,为什么这是选择的答案?@Krythic,这是2011年的一个正确答案,并且被广泛接受。我猜事件onChange()在Visual Studio 2015(以及之后?)中被称为TextChanged()。如果您正在寻找这个问题的当前答案,我相信它将捕获“离开”事件如果您希望它在键入时更新源代码,在短暂暂停后,在绑定中还需要UpdateSourceTrigger=PropertyChanged。我喜欢此解决方案,但它不能确定文本框文本是否已更改。因此,唯一协调的方法是使用TextCh


他问什么?此外,为什么这是选择的答案?@Krythic,这是2011年的一个正确答案,并且被广泛接受。我猜事件onChange()在Visual Studio 2015(以及之后?)中被称为TextChanged()。如果您正在寻找这个问题的当前答案,我相信它将捕获“离开”事件如果您希望它在键入时更新源代码,在短暂暂停后,在绑定中还需要
UpdateSourceTrigger=PropertyChanged
。我喜欢此解决方案,但它不能确定文本框文本是否已更改。因此,唯一协调的方法是使用TextChanged来提升标志(布尔值),然后在LostFocus事件中,如果标志为true,则执行您需要执行的任何操作。使用定时器和延迟只是疯狂的编码。很好的解决方案,谢谢
public class MyTextBox : TextBox
{
    private Timer m_delayedTextChangedTimer;

    public event EventHandler DelayedTextChanged;

    public MyTextBox() : base() 
    {
        this.DelayedTextChangedTimeout = 10 * 1000; // 10 seconds
    }

    protected override void Dispose(bool disposing)
    {
        if (m_delayedTextChangedTimer != null)
        {
            m_delayedTextChangedTimer.Stop();
            if (disposing)
                m_delayedTextChangedTimer.Dispose();
        }

        base.Dispose(disposing);            
    }

    public int DelayedTextChangedTimeout { get; set; }

    protected virtual void OnDelayedTextChanged(EventArgs e)
    {
        if (this.DelayedTextChanged != null)
            this.DelayedTextChanged(this, e);
    }

    protected override void OnTextChanged(EventArgs e)
    {
        this.InitializeDelayedTextChangedEvent();
        base.OnTextChanged(e);            
    }                

    private void InitializeDelayedTextChangedEvent()
    {
        if (m_delayedTextChangedTimer != null)
            m_delayedTextChangedTimer.Stop();

        if (m_delayedTextChangedTimer == null || m_delayedTextChangedTimer.Interval != this.DelayedTextChangedTimeout)
        {                
            m_delayedTextChangedTimer = new Timer();
            m_delayedTextChangedTimer.Tick += new EventHandler(HandleDelayedTextChangedTimerTick);
            m_delayedTextChangedTimer.Interval = this.DelayedTextChangedTimeout;
        }

        m_delayedTextChangedTimer.Start();
    }

    private void HandleDelayedTextChangedTimerTick(object sender, EventArgs e)
    {
        Timer timer = sender as Timer;
        timer.Stop();

        this.OnDelayedTextChanged(EventArgs.Empty);
    }
}
    //--- this block deals with user editing the textBoxInputFile --- //
    private Boolean textChanged = false;
    private void textBoxInputFile_TextChanged(object sender, EventArgs e) {
        textChanged = true;
    }
    private void textBoxInputFile_Leave(object sender, EventArgs e) {
        if (textChanged) {
            fileNameChanged();
        }
        textChanged = false;
    }
    private void textBoxInputFile_KeyDown(object sender, KeyEventArgs e) {
        if (textChanged & e.KeyCode == Keys.Enter) {
            fileNameChanged();
        }
        textChanged = false;
    }
    //--- end block  --- //
private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Stop();
    Calculate(); // method to calculate value
}

private void txtNumber_TextChanged(object sender, EventArgs e)
{
    timer1.Stop();
    timer1.Start();
}
<TextBox Text="{Binding Name, Delay=500}" />
public partial class Form2 : Form
    {
        static int VALIDATION_DELAY = 1500;
        System.Threading.Timer timer = null;
        public Form2()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            TextBox origin = sender as TextBox;
            if (!origin.ContainsFocus)
                return;

            DisposeTimer();
            timer = new System.Threading.Timer(TimerElapsed, null, VALIDATION_DELAY, VALIDATION_DELAY);

        }
        private void TimerElapsed(Object obj)
        {
            CheckSyntaxAndReport();
            DisposeTimer();            
        }

        private void DisposeTimer()
        {
            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
        }

        private void CheckSyntaxAndReport()
        {            
            this.Invoke(new Action(() => 
            {
                string s = textBox1.Text.ToUpper(); //Do everything on the UI thread itself
                label1.Text = s; 
            }
                ));            
        }
    }
    private const int millisecondsToWait = 500;
    private static DateTime s_lastTimeOfTyping;
    private void SearchField_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var latestTimeOfTyping = DateTime.Now;
        var text = ((TextBox)sender).Text;
        Task.Run(()=>DelayedCheck(latestTimeOfTyping, text));
        s_lastTimeOfTyping = latestTimeOfTyping;
    }

    private async Task DelayedCheck(DateTime latestTimeOfTyping, string text)
    {
        await Task.Delay(millisecondsToWait);
        if (latestTimeOfTyping.Equals(s_lastTimeOfTyping))
        {
            // Execute your function here after last text change
            // Will need to bring back to the UI if doing UI changes
        }
    }
var FindDelay = 500;//milliseconds
//textBox is your text box element
Observable.FromEventPattern<EventArgs>(textBox, "TextChanged")
    .Select(ea => ((TextBox) ea.Sender).Text)
    .DistinctUntilChanged()
    .Throttle(TimeSpan.FromMilliseconds(FindDelay))
    .Subscribe(text => { 
        //your handler here 
    });
public static class UIExtensionMethods
{
    public static async Task<bool> GetIdle(this TextBox txb)
    {
        string txt = txb.Text;
        await Task.Delay(500);
        return txt == txb.Text;
    }
}
if (await myTextBox.GetIdle()){
    // typing has stopped, do stuff
}
using System;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Threading.Timer;

    internal class DelayedText : IDisposable
    {
        private readonly EventHandler _onTextChangedDelayed;
        private readonly TextBox _textBox;
        private readonly int _period;
        private Timer _timer;

        public DelayedText(TextBox textBox, EventHandler onTextChangedDelayed, int period = 250)
        {
            _textBox = textBox;
            _onTextChangedDelayed = onTextChangedDelayed;
            _textBox.TextChanged += TextBoxOnTextChanged;
            _period = period;
        }

        public void Dispose()
        {
            _timer?.Dispose();
            _timer = null;
        }

        private void TextBoxOnTextChanged(object sender, EventArgs e)
        {
            Dispose();
            _timer = new Timer(TimerElapsed, null, _period, Timeout.Infinite);
        }

        private void TimerElapsed(object state)
        {
            _onTextChangedDelayed(_textBox, EventArgs.Empty);
        }
    }
InitializeComponent();
...
new DelayedText(txtEdit, txtEdit_OnTextChangedDelayed);
private void valueInput_TextChanged(object sender, EventArgs e)
{
    CalculateAfterStopTyping();   
}

Thread delayedCalculationThread;

int delay = 0;
private void CalculateAfterStopTyping()
{
    delay += 200;
    if (delayedCalculationThread != null && delayedCalculationThread.IsAlive)
        return;

    delayedCalculationThread = new Thread(() =>
    {
        while (delay >= 200)
        {
            delay = delay - 200;
            try
            {
                Thread.Sleep(200);
            }
            catch (Exception) {}
        }
        Invoke(new Action(() =>
        {
            // do your calcualation here...
        }));
    });

    delayedCalculationThread.Start();
}
<TextBox Name="Textbox1"
             TextChanged="Textbox1_TextChanged"/>
using System.Threading.Tasks;

public bool isChanging = false;
async private void Textbox1_TextChanged(object sender,
                                        TextChangedEventArgs e)
    {
        // entry flag
        if (isChanging)
        {
            return;
        }
        isChanging = true;
        await Task.Delay(500);

        // do your stuff here or call a function

        // exit flag
        isChanging = false;
    }
<TextBox x:Name="YourTextBox" LostFocus="YourTextBox_LostFocus" />
private void YourTextBox_LostFocus(object sender, RoutedEventArgs e)
{
    //Your code here
}
public static void TextBoxEditCommit(TextBox tb, Action<TextBox>OnEditCommit)
{
    if (OnEditCommit == null) 
            throw new ArgumentException("OnEditCommit delegate is mandatory.");
    //THis delegate fire the OnEditCommit Action
    EventHandler _OnEditCommit = delegate(object sender, EventArgs e) 
            { OnEditCommit(tb); };
    //Edit commit on Enter or Tab
    tb.KeyDown += delegate (object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
        {
            //Temporary remove lostfocus event for avoid double commits
            tb.LostFocus -= _OnEditCommit;
            OnEditCommit(tb);
            tb.LostFocus += _OnEditCommit;
        }
    };
    //Edit commit on LostFocus
    tb.LostFocus += _OnEditCommit;
}
//Check for valid content
UIUtil.TextBoxEditCommit(tbRuleName, (tb) => {
        //Your code here, tb.text is the value collected
            });