事件不是第一次触发

Event is not firing for the first time

在 RichTextBox 中,我试图通过按下句点('.')触发一个事件,但它第一次不起作用。

如果我写 "Lorem Ipsum.",它不起作用但是如果我写 "Lorem Ipsum .""Lorem Ipsum.."没关系

PS:我还添加了 KelimeGuncelle 方法和 GetWordGroupInstances() 字典。

这是区块:

private void rtbMakale_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.OemPeriod)
            {
                kelimeGuncelle();
            }
        }

kelimeGuncelle 方法:

void kelimeGuncelle()
        {
            Dictionary<String, int> TekliKelimeGruplari = GetWordGroupInstances(1);
            foreach (var item in TekliKelimeGruplari)
            {
                for (int i = 0; i < lstKelimeler.Items.Count; i++)
                {
                    var kelime = lstKelimeler.Items[i];
                    string guncellenecekKelime = kelime.ToString().Remove(kelime.ToString().IndexOf(" ( ") - 1);
                    string gelenKelime = item.Key;
                    string _guncellenecekKelime = kelime.ToString();

                    int pFrom = _guncellenecekKelime.IndexOf("(") + 1;
                    int pTo = _guncellenecekKelime.LastIndexOf("/");
                    int guncellenecekSayi = Convert.ToInt32(_guncellenecekKelime.Substring(pFrom, pTo - pFrom));

                    int kFrom = _guncellenecekKelime.IndexOf("/") + 1;
                    int kTo = _guncellenecekKelime.LastIndexOf(")");
                    int toplamYazilacakSayi = Convert.ToInt32(_guncellenecekKelime.Substring(kFrom, kTo - kFrom));

                    int kelimeninSirasi = lstKelimeler.Items.IndexOf(kelime);
                    if (Equals(guncellenecekKelime, gelenKelime))
                    {
                        guncellenecekSayi = item.Value;
                        lstKelimeler.Items.RemoveAt(kelimeninSirasi);
                        lstKelimeler.Items.Insert(kelimeninSirasi, guncellenecekKelime + "  ( " + guncellenecekSayi + "/" + toplamYazilacakSayi + " )");
                        //lstKelimeler.Refresh();
                    }

                    if (rtbMakale.Text.Contains(guncellenecekKelime) == false)
                    {
                        lstKelimeler.Items.RemoveAt(kelimeninSirasi);
                        lstKelimeler.Items.Insert(kelimeninSirasi, guncellenecekKelime + "  ( 0/" + toplamYazilacakSayi + " )");
                        //lstKelimeler.Refresh();
                    }
                }
            }
            TekliKelimeGruplari.Clear();
        }

和 GetWordGroupInstances:

Dictionary<String, int> GetWordGroupInstances(int GroupSize)
        {

            Dictionary<String, int> WordGroupInstances = new Dictionary<string, int>();

            String[] sourceText = GetSourceText().Split(' ');
            int pointer = 0;
            StringBuilder groupBuilder = new StringBuilder();
            while (pointer < sourceText.Length - GroupSize)
            {
                groupBuilder.Clear();
                int offset = pointer + GroupSize;
                for (int i = pointer; i < offset; i++)
                {
                    groupBuilder.Append(" ").Append(sourceText[i]);
                }

                String key = groupBuilder.ToString().Substring(1);
                if (!WordGroupInstances.ContainsKey(key))
                {
                    WordGroupInstances.Add(key, 1);
                }
                else
                {
                    WordGroupInstances[key]++;
                }

                pointer += 1;
            }

            return WordGroupInstances;

        }

尝试使用 MessageBox.Show("Test");为了测试,也许你的 kelimeGuncelle() 方法有问题。

   private void rtbMakale_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.OemPeriod)
        {
            MessageBox.Show("Test");
        }
    }

我明白了!

在 GetWordGroupInstances 中,我更改了 while 循环。这里:

            while (pointer <= sourceText.Length - GroupSize)