Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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# 组合框,建议文本_C#_Winforms_Combobox - Fatal编程技术网

C# 组合框,建议文本

C# 组合框,建议文本,c#,winforms,combobox,C#,Winforms,Combobox,在我的应用程序中,我有一个组合框,包含系统中安装的所有字体。我想添加一个功能,即用户写完一封信后,其行为如下: 这意味着我不想使用自动完成模式和自动完成源属性 以下是我为CB处理TextUpdate事件的代码: private void tsComboBoxFontChoice_TextUpdate(object sender, EventArgs e) { if (!this.isBackClicked) { int caretPosition = this.

在我的应用程序中,我有一个组合框,包含系统中安装的所有字体。我想添加一个功能,即用户写完一封信后,其行为如下:

这意味着我不想使用自动完成模式和自动完成源属性

以下是我为CB处理TextUpdate事件的代码:

private void tsComboBoxFontChoice_TextUpdate(object sender, EventArgs e)
{
    if (!this.isBackClicked)
    {
        int caretPosition = this.tsComboBoxFontChoice.Text.Length;
        bool isFound = false;
        StringBuilder sbComboBox = new StringBuilder(this.tsComboBoxFontChoice.Text);
        StringBuilder sbTextToAppend = null;

        for (int i = 0; i < this.systemFonts.Families.Length;i++ )
        {
            StringBuilder sb = new StringBuilder(this.systemFonts.Families[i].Name);
            sbTextToAppend = new StringBuilder(this.systemFonts.Families[i].Name);
            int tempStopIndex= sbComboBox.Length;

            if (tempStopIndex <= sb.Length)
            {
                sb.Remove(tempStopIndex,
                this.systemFonts.Families[i].Name.Length - tempStopIndex).ToString(); //A
            }

            if(sbComboBox.ToString() == sb.ToString())
            {
                sbTextToAppend.Remove(0, tempStopIndex).ToString();   //rial
                isFound = true;
                break;
            }
        }
        if (isFound)
        {
            sbComboBox.Append(sbTextToAppend);
            this.tsComboBoxFontChoice.Text = sbComboBox.ToString();
            this.tsComboBoxFontChoice.Select(caretPosition, sbComboBox.Length);
        }
    }
    else
    {
        this.isBackClicked = false;
    }
}
private void tscomboxFontChoice_TextUpdate(对象发送方,事件参数e)
{
如果(!this.isBackClicked)
{
int caretPosition=this.tscomboxFontChoice.Text.Length;
bool isFound=false;
StringBuilder sbComboBox=新的StringBuilder(this.tscomboxFontChoice.Text);
StringBuilder sbTextToAppend=null;
对于(int i=0;i如果(tempStopIndex您可以在创建
StringBuilder
s之前比较键入的内容:

string typed = this.tsComboBoxFontChoice.Text;
for (int i = 0; i < this.systemFonts.Families.Length; i++)
{
    string candidate = this.systemFonts.Families[i].Name;
    if (!candidate.StartsWith(typed))
    {
        continue;
    }

    // it's a match! 
    ...
}
string typed=this.tscomboxFontChoice.Text;
对于(int i=0;i
这样,您就不会为与当前值不匹配的字体计算“要追加的文本”

顺便说一下,我怀疑您是否需要使用
StringBuilder
s。

让我们分解您的解决方案(提取方法):

诸如此类

private void tsComboBoxFontChoice_TextUpdate(object sender, EventArgs e) {
  ComboBox box = sender as ComboBox;

  // To prevent calling the event when we're adding tips for user
  box.TextUpdate -= tsComboBoxFontChoice_TextUpdate;

  try {
    String oldText = box.Text;
    String suggested = suggestedFontName(oldText);

    box.Text = suggested;
    box.Select(oldText.Length, suggested.Length - oldText.Length);
  }
  finally {
    // the text is updated, so let's continue listening TextUpdate event
    box.TextUpdate += tsComboBoxFontChoice_TextUpdate;
  }
}

什么是
systemFonts
?您可以简单地使用
systemFonts.FirstOrDefault(o=>o.StartWith(comboBox.Text))
查找第一个匹配项(或者
null
如果没有匹配项)。您好,感谢这个伟大的答案!它让我了解了lambda表达式,我以前从未使用过它,而且它非常强大。
private void tsComboBoxFontChoice_TextUpdate(object sender, EventArgs e) {
  ComboBox box = sender as ComboBox;

  // To prevent calling the event when we're adding tips for user
  box.TextUpdate -= tsComboBoxFontChoice_TextUpdate;

  try {
    String oldText = box.Text;
    String suggested = suggestedFontName(oldText);

    box.Text = suggested;
    box.Select(oldText.Length, suggested.Length - oldText.Length);
  }
  finally {
    // the text is updated, so let's continue listening TextUpdate event
    box.TextUpdate += tsComboBoxFontChoice_TextUpdate;
  }
}