Numeric Sorting using ComboBox in VB
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Numeric Sorting using ComboBox in VB

  1. #1
    Join Date
    Feb 2006
    Posts
    17

    Talking Numeric Sorting using ComboBox in VB

    I m facing problem of sorting using comboBox.
    I want to sort the data if sorted property true and in list if i m adding 1,5,12,7 then in runtime it will give output 1,5,7,12 in comboBox.
    Combobox i want to sort it in numeric way not according to string.
    I have used one button in my project.
    I want to know while adding items to the list, the list is resorted according to text.

    Here i m putting my code in Borland C++. Not much difference according to VB. While adding items in the list in last two lines it is resorted. I have sort the array then displaying a[i] value in this code. In messagebox data is sorted if i m putting 1,5,12,7 in list then in messagebox a[i] will give 1,5,7,12.
    But in the end while i m adding data it is sorted according to order in the list.

    I have done it in VB it is creating same problem. I have first created in VB then Borland C++. Please see last three lines.
    Code:
    void __fastcall TForm1::QIButton8Click(TObject *Sender)
    {
    int *a;
    int cnt = QIComboBox1->Items->Count;
    a = new int[cnt*sizeof(int)];
    int i,j,k;
    for(i=0;i<cnt;i++)
    {
       a[i] = StrToInt(QIComboBox1->Items->Strings[i]);
       Application->MessageBoxA(IntToStr(a[i]).c_str(),"value of a[i]",0);
      }
    
    for(i=0;i<cnt;i++)
    {
      for(j=i+1;j<cnt+1;j++)
      {
        if(a[i]>a[j])
         {
           k=a[i];
           a[i]=a[j];
           a[j]=k;
          }
        }
     Application->MessageBoxA(IntToStr(a[i]).c_str(),"value of a[i]",0);
    
      /////// here value is sorted 1,5,7,12
     }
    
    QIComboBox1->Clear();
    for(i=0;i<cnt;i++)
    {
       QIComboBox1->Items->Add(a[i]); 
         Here Problem is there  //// But while adding items it is    resorted again.
      }
    delete a;
    }
    plz give me solution in VB so i will implement in Borland C++.

    reply it's urgent.
    Last edited by WizBang; June 28th, 2006 at 04:01 AM. Reason: Added [code] tags

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Numeric Sorting using ComboBox in VB

    If your items are being added in the order that you want them displayed, then why don't you just set the Sorted property to false?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Feb 2006
    Posts
    17

    Talking Re: Numeric Sorting using ComboBox in VB

    But i want to sort it in Sorted property= true only and they give us like ouput 1,5,7,12 in combobox.
    I have done it using Sorted property = false but i want in Sorted property=true only.
    So, using sorted property = true, while i have sort the array and i m adding items in the end it is resorted so it is main problem.

    reply urgent.

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Numeric Sorting using ComboBox in VB

    Well, in VB you can supply the Index in the call to the AddItem method. Then the items will be in the order you add them even if the Sorted property is True.
    Code:
    Dim I As Long
    For I = 1 To 20
      Combo1.AddItem I, I - 1
    Next
    Can you apply this to Borland C++?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Feb 2006
    Posts
    17

    Talking Re: Numeric Sorting using ComboBox in VB

    Hi WizBang,

    I can not understand what are u telling?

    plz reply me.

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Numeric Sorting using ComboBox in VB

    There is an optional parameter for the AddItem method, so yo can specify the Index (the position in the list) where the item will be added. Can you do this in Borland C++?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Feb 2006
    Posts
    17

    Talking Re: Numeric Sorting using ComboBox in VB

    Hi WizBang,

    In Borland C++ in my code last three lines, while i m adding it i want to add a[i] value. Because that is sorted value.
    But this is not working it.

    Can u give me for another solution?
    Because in VB also i have put messagebox a[i] that is sorted it but while i m adding items a(i) it is resorted.
    I want solution at runtime because in list property different values are added like 1,5,12,7,9 ..... etc.


    So, reply its urgent.

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Numeric Sorting using ComboBox in VB

    Did you try the example I showed? If you cannot use it, please explain why.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Feb 2006
    Posts
    17

    Re: Numeric Sorting using ComboBox in VB

    Hi all,
    i want to sort data if in list values are 1,12,5,7,Z,A then using sorting 1,5,7,12,A,Z values in combobox.
    Actually in VB IsNumeric function is there but i m using Borland C++.
    In that no this function is there.
    so what to do?
    i m putting my code.

    void __fastcall Sort()
    {

    char a[100][100];
    int cnt = QIComboBox1->Items->Count;
    int i,j,k;
    for(i=0;i<cnt;i++)
    {
    strcpy(a[i],QIComboBox1->Items->Strings[i].c_str());
    Application->MessageBoxA((a[i]),"value of a[i]",0);
    ///////// It will give message 1,12,5,7,Z,A
    }

    for(i=0;i<cnt-1;i++)
    {
    for(j=i+1;j<cnt;j++)
    {
    if(strcmp(a[i],a[j])>0)
    {
    char temp[40];
    strcpy(temp,a[i]);
    strcpy(a[i],a[j]);
    strcpy(a[j],temp);
    }
    }
    }

    for(i=0;i<cnt;i++)
    {
    Application->MessageBoxA((a[i]),"values after sorting",0);
    }

    using strcmp function i have tried but they sort it by ascii code. so not giving right output.
    I want output if i add in list 1,12,5,7,Z,A then in comboBox 1,5,7,12,A,Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center