简体   繁体   English

组合框中SelectedIndexChanged上的格式化文本

[英]Formatted text on SelectedIndexChanged in Combobox

In ComboBox.Items , there are three options, 0.25, 0.50, 0.75 . ComboBox.Items ,有三个选项0.25, 0.50, 0.75 Now user writes a value in ComboBox , eg 1 , and now selects one of those items, the desired comboBox.Text = 1.25 (selecting first option). 现在,用户在ComboBox写入一个值,例如1 ,然后选择其中一个项目,即所需的comboBox.Text = 1.25 (选择第一个选项)。

public string cmbBxText = string.Empty;

private void LengthCmbBx_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.lengthCmbBx.Text = 
            (Convert.ToDouble(cmbBxText) 
            + Convert.ToDouble(this.lengthCmbBx.SelectedItem)).ToString();
    }

    private void lengthCmbBx_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode != Keys.Up
            && e.KeyCode != Keys.Right
            && e.KeyCode != Keys.Left
            && e.KeyCode != Keys.Down)
        {
            cmbBxText = this.lengthCmbBx.Text;
        }
    }

This code sets the Text = 0.25 . 此代码将Text = 0.25设置为Text = 0.25 I want output like entered value + selected item eg 1.25 when user enters 1 and selects 0.25 . 当用户输入1并选择0.25时,我希望输出类似entered value + selected item例如1.25 And when I debug the above code, the SelectedIndexChanged event run twice, the break point shows comboBox.Text = 1.25 but not on form. 当我调试上面的代码时, SelectedIndexChanged事件运行两次,断点显示comboBox.Text = 1.25但不在窗体上。

Please try it out and tell me, whether you are looking for the same. 请尝试一下,并告诉我您是否正在寻找相同的产品。

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    { 

            index = comboBox1.Items.IndexOf(comboBox1.Text);

            string temp = comboBox1.Text;
            StringBuilder newSB = new StringBuilder(temp);
            newSB = newSB.Remove(0, 1);
            string newStr =  cmbBxText.ToString() + newSB.ToString();
            cmbBxText.Clear();
            comboBox1.Items.RemoveAt(index);
            comboBox1.Items.Insert(index, newStr); 

    } 

Simply, 只是,

    private void CmbBx_TextChanged(object sender, EventArgs e)
    {
        if (this.cmbBx.Text != string.Empty 
            && !this.cmbBx.Text.Contains(".25")
            && !this.cmbBx.Text.Contains(".50")
            && !this.cmbBx.Text.Contains(".75"))
        {
            this.cmbBx.Items.Clear();

            this.cmbBx.Items.Add(this.cmbBx.Text + ".25");
            this.cmbBx.Items.Add(this.cmbBx.Text + ".50");
            this.cmbBx.Items.Add(this.cmbBx.Text + ".75");
        }

        if (this.cmbBx.Text == string.Empty)
        {
            this.cmbBx.Items.Clear();
        }

        SendKeys.Send("{F4}");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM