locked
combobox with multiple column

    Question

  •  
    Hi,
    I having problem with c# combo box control with multiple columns.
    I have one combox with 2 columns:

    code(char 5)           desc(char10)

    d1                           day1
    d10                         day10


    I using Add items to add in those code and desc, but the column position is out. I was thinking of taking the 5 minus the length of the code, say x = 5 - code.length(), and then put it as space(x), how can i add spaces string in c#? and how to get the CODE only as selected display value instead of code+desc ?

    Is there any simple way to do it? Below is the code
    Thank you!!


    private void Form1_Load(object sender, System.EventArgs e)
    {
    string col1;
    string col2;
    int s;
    string space;
    for (int i=0;i<2;i++)
    {
    if (i==0)
    {
    col1="d1";
    col2="day1";
    }
    else
    {
    col1="d10";
    col2="day10";
    }

    s=5-col1.ToString ().Length ();
    space=new String ("",s);
    comboBox1.Items .Add (col1+space+col2);
    }
    }
    private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    //MessageBox.Show (comboBox1.Text.Substring (0,5));
    }
    Wednesday, August 13, 2008 2:04 AM

Answers

  •  

    Hi, htmlforums,

           I think your codes could work fine if you changed the font of the words in ComboBox properties to some other font type instead of the default one.   I have done some research and testing and find out the most of the fonts could do well here, however, some of them might give us this abnormal display result.

           For your another question, if you just want to get the CODE value instead of CODE + DESC value in your program, Chakshu Sharma has given you a good solution.   Or if you want the text value of the ComboBox be CODE value after selection while the dropdown list parts still are CODE + DESC value, I think it could be some kind of difficult.   Because the Text property of ComboBox control is always set to the corresponding value after we have selected one.   ComboBox itself has a very complex structure here.   After some testing, I think we could not just override the Text property to realize what we want. 

           One lazy way could be set the size of ComboBox just big enough to display the CODE value, really lazy, right?  J  

           Another way I think is to define a new user control inheriting ComboBox, but it is a difficult job, because many methods should be override, however, we should also take the performance into consideration, I don’t think it is easy to make the new control work very efficiently.  

           Have I make it clear?  If you have any difficulties, let me know please.

         

           Thanks,

          

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Saturday, August 16, 2008 6:15 PM
    Moderator
  •  

    Hi, htmlforums,

          Please try the font like MS Mincho, MS Gothic.  J

          As another option, if you want a multiple column ComboBox, I think you could have a check this article on codeproject.  You could try to use this multiple column ComboBox with a DataTable in its dropdown list window.  

         

           Please let me know, if you have any more problems.

     

           Thanks,

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, August 18, 2008 10:18 AM
    Moderator

All replies

  • Use String spaces = new String(' ', s) (there's a space between the two quotes),  instead of  String space=new String ("",s) to create a string of spaces. If I understood properly then you are trying to use space as a delimeter between code and desc so you can get the index of the first space using the  String.IndexOf (' ') and pass this index to the String.Substring(0, index) to get the code string.
    Wednesday, August 13, 2008 6:25 AM
  • hi, i tried your method as u said, although i can get the space as i want, but i don't know why, when i add item into combo,  e.g   

    comboBox1.Items .Add (col1+space+col2);


    its still out of the position although the space value is correct.  Is it we have to do property setting on the combobox itself?


    And any other way to capture the CODE only as the selected value in the combobox? cos i not familiar in using 'indexof'  as u suggested .

    thanks for help
    Thursday, August 14, 2008 2:00 AM
  • I tried the following code and the columns are getting displayed properly for me. In the following code snippet you can see how to use IndexOf() to get the code.


     
    private void Form1_Load(object sender, EventArgs e)  
            {  
                comboBox1.Items.Add("Code1" + " " + "Desc1");  
                comboBox1.Items.Add("Code2" + " " + "Desc2");  
                comboBox1.Items.Add("Code3" + " " + "Desc3");  
                comboBox1.Items.Add("Code4" + " " + "Desc4");  
                string code = GetCode(0);  
            }  
     
            string GetCode(int itemIndex)  
            {  
                string item = (string)comboBox1.Items[itemIndex];  
                string code = item.Substring(0, item.IndexOf(' '));  
                return code;  
            } 
    Saturday, August 16, 2008 9:49 AM
  •  

    Hi, htmlforums,

           I think your codes could work fine if you changed the font of the words in ComboBox properties to some other font type instead of the default one.   I have done some research and testing and find out the most of the fonts could do well here, however, some of them might give us this abnormal display result.

           For your another question, if you just want to get the CODE value instead of CODE + DESC value in your program, Chakshu Sharma has given you a good solution.   Or if you want the text value of the ComboBox be CODE value after selection while the dropdown list parts still are CODE + DESC value, I think it could be some kind of difficult.   Because the Text property of ComboBox control is always set to the corresponding value after we have selected one.   ComboBox itself has a very complex structure here.   After some testing, I think we could not just override the Text property to realize what we want. 

           One lazy way could be set the size of ComboBox just big enough to display the CODE value, really lazy, right?  J  

           Another way I think is to define a new user control inheriting ComboBox, but it is a difficult job, because many methods should be override, however, we should also take the performance into consideration, I don’t think it is easy to make the new control work very efficiently.  

           Have I make it clear?  If you have any difficulties, let me know please.

         

           Thanks,

          

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Saturday, August 16, 2008 6:15 PM
    Moderator
  • Hi Lingzhi,
    I tried many font type in font property setting in combo box, still having the same problem. Can let me know which font type to choose to make the combo box columns display properly?

    I found very hard to work with this combo box control especially when come to two column, maybe i will work with another control, list view, much easier.   Anyway thanks Lingzhi and Chakshu Sharma for helping me  :)
    Monday, August 18, 2008 9:30 AM
  •  

    Hi, htmlforums,

          Please try the font like MS Mincho, MS Gothic.  J

          As another option, if you want a multiple column ComboBox, I think you could have a check this article on codeproject.  You could try to use this multiple column ComboBox with a DataTable in its dropdown list window.  

         

           Please let me know, if you have any more problems.

     

           Thanks,

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, August 18, 2008 10:18 AM
    Moderator
  • hi Lingzhi,
    these 2 font type really works  :)  , and for the multicolumn combo, i will download it and try it out, it looks useful, thanks for your help!
    Tuesday, August 19, 2008 1:04 AM
  • Hai,
    
    I want to show multi column in Combo box in Windows Application. For that I got Ur code,
    
     But I am getting some problem, Please do the needful.
    
    <br/>
    
    namespace Windows_Combo
    
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
          comboBox1.Items.Add("Code1" + " " + "Desc1"); 
          comboBox1.Items.Add("Code2" + " " + "Desc2"); 
          comboBox1.Items.Add("Code3" + " " + "Desc3"); 
          comboBox1.Items.Add("Code4" + " " + "Desc4"); 
          string code = GetCode(0);
        }
          string GetCode(int itemIndex) 
          { 
          string item = (string)comboBox1.Items[itemIndex];
          string code = item.Substring(0, item.IndexOf(' '));
        // I am getting this Error for the above code line (Length cannot //be less than zero.
        //Parameter name: length) while executing the program.
          return code; 
          }
        
      }
        
    }
    
    <br/>
    
    <br/>
    
    Friday, October 29, 2010 7:35 AM