如何在组合框中重新对齐文本 [英] How to re align text in combo box

查看:131
本文介绍了如何在组合框中重新对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我该如何在comboBox上重新对齐文本,对此我有一个问题,因为当我按键盘上的任意键时,碰巧字母是从右到左键入的.示例"hello"将显示为"olleh".有人对我该如何解决这个问题有想法吗?

这是设置我的comboBox项目的代码

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.comboBox1.Items.Clear();
            NpgsqlCommand com = new NpgsqlCommand("SELECT * FROM mem_profile where mem_id like ''%" + comboBox1.Text + "%'' or lname like ''%" + comboBox1.Text + "%'' or fname like ''%" + comboBox1.Text + "%''", conDB.returnConnect());
            NpgsqlDataReader read = com.ExecuteReader();
            ArrayList sgGroup = new ArrayList();
            while (read.Read())
            {
                comboBox1.Items.Add(read[0].ToString() + "-" +read[1].ToString() + ", " + read[3].ToString()+ " "+ read[2].ToString());
            }
            comboBox1.DroppedDown = true;
        }



我不知道为什么当我在键盘上键入按键时,我的文本会从右到左显示.任何帮助,将不胜感激.感谢

解决方案

我认为问题在于您在keypress事件中运行查询,因此,光标总是被重置为位置0,下一个字符将插入开头而不是结尾.

如果您暂时将代码注释掉,是否可以正常工作?

如果尝试在输入文本时运行查询,则应运行后台工作进程并选择文本更改事件而不是按键事件.

我认为您需要重新考虑自己的策略,搜索自动完成"的示例. />

 私有 无效 comboBox1_KeyPress(对象发​​件人,KeyPressEventArgs e)
    {
    comboBox1.Items.Clear();
    } 

将产生相同的效果.
要治愈它,请尝试以下操作:

 私有 无效 comboBox1_KeyPress(对象发​​件人,KeyPressEventArgs e)
    {
     int  posn = comboBox1.SelectionStart;
    comboBox1.Items.Clear();
    comboBox1.SelectionStart = posn;
    } 

这不是完美的-我还没有尝试使用任何选定的文本等.-但这是我知道的唯一方法,可以在ComboBox中获得并设置插入符号的位置


使用那个吗?!

 公共 静态 字符串 ReverseString(字符串 s)
{
    字符 [] arr = s.ToCharArray();
    Array.Reverse(arr);
    返回  字符串(arr);
} 


Hello

How can i re align my text on comboBox, I have a problem regarding this because when i press any keys on my keyboard it happens that the letters are type from right to left. Example "hello" this would appear as "olleh". Anyone has an idea on how can i solve this problem?

Here''s my code on setting my comboBox items

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            this.comboBox1.Items.Clear();
            NpgsqlCommand com = new NpgsqlCommand("SELECT * FROM mem_profile where mem_id like ''%" + comboBox1.Text + "%'' or lname like ''%" + comboBox1.Text + "%'' or fname like ''%" + comboBox1.Text + "%''", conDB.returnConnect());
            NpgsqlDataReader read = com.ExecuteReader();
            ArrayList sgGroup = new ArrayList();
            while (read.Read())
            {
                comboBox1.Items.Add(read[0].ToString() + "-" +read[1].ToString() + ", " + read[3].ToString()+ " "+ read[2].ToString());
            }
            comboBox1.DroppedDown = true;
        }



I dont why my text would appear from right to left when i type my keys on the keyboard. Any help would be appreciated. thanks

解决方案

I think the problem lies in the fact that you are running the query in the keypress event, and as a result of this the cursor is always being reset to position 0 and the next character is being inserted at the start instead of at the end.

if you temporarily comment out your code does it work fine?

If you are trying to run a query while entering text, you should run a background worker process and pick off the text changed event rather than the key press event.

I think you need to rethink your strategy, search for examples of AutoCompletion.


It''s because you are clearing the strings, which moves the caret to the start of the text.

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    comboBox1.Items.Clear();
    }

Will give the same effect.
To cure it try this:

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    int posn = comboBox1.SelectionStart;
    comboBox1.Items.Clear();
    comboBox1.SelectionStart = posn;
    }

It''s not perfect - I haven''t tried it with any selected text etc. - but that''s the only way I know to get and set the caret position in a ComboBox


use that ?!

public static string ReverseString(string s)
{
    char[] arr = s.ToCharArray();
    Array.Reverse(arr);
    return new string(arr);
}


这篇关于如何在组合框中重新对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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