Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. search a list control

search a list control

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
40 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahuljin
    wrote on last edited by
    #1

    i have to list control in a mfc dialog application. first one is ipList. i want to search an item in the list.

    int i = 0;
    TCHAR ss[50] = {0};
    ipAdd.GetAddress(m_M1, m_M2, m_M3, m_M4);
    _stprintf_s(ss, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4);

    for(int j=0; j < ipList.GetItemCount(); j++)
    {
    if(ipList.GetItem(j) != ss)
    i=0;
    else
    i = 1;
    }

    it gives the error ---

    error C2664: 'CListCtrl::GetItem' : cannot convert parameter 1 from 'int' to 'LVITEMW *'

    what i should do now ? btw thanks

    C N 3 Replies Last reply
    0
    • R rahuljin

      i have to list control in a mfc dialog application. first one is ipList. i want to search an item in the list.

      int i = 0;
      TCHAR ss[50] = {0};
      ipAdd.GetAddress(m_M1, m_M2, m_M3, m_M4);
      _stprintf_s(ss, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4);

      for(int j=0; j < ipList.GetItemCount(); j++)
      {
      if(ipList.GetItem(j) != ss)
      i=0;
      else
      i = 1;
      }

      it gives the error ---

      error C2664: 'CListCtrl::GetItem' : cannot convert parameter 1 from 'int' to 'LVITEMW *'

      what i should do now ? btw thanks

      C Offline
      C Offline
      carter2000
      wrote on last edited by
      #2

      Hi, GetItem has the declaration like this:BOOL GetItem(LVITEM* pItem) const; So it can't accept a int type parameter. besides, it return a BOOL value; I think you must misunderstand the function.

      R 1 Reply Last reply
      0
      • R rahuljin

        i have to list control in a mfc dialog application. first one is ipList. i want to search an item in the list.

        int i = 0;
        TCHAR ss[50] = {0};
        ipAdd.GetAddress(m_M1, m_M2, m_M3, m_M4);
        _stprintf_s(ss, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4);

        for(int j=0; j < ipList.GetItemCount(); j++)
        {
        if(ipList.GetItem(j) != ss)
        i=0;
        else
        i = 1;
        }

        it gives the error ---

        error C2664: 'CListCtrl::GetItem' : cannot convert parameter 1 from 'int' to 'LVITEMW *'

        what i should do now ? btw thanks

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        In addition to the above comment, you can use the FindItem() function of list control directly. No need to loop.

        nave [OpenedFileFinder] [My Blog]

        R 1 Reply Last reply
        0
        • C carter2000

          Hi, GetItem has the declaration like this:BOOL GetItem(LVITEM* pItem) const; So it can't accept a int type parameter. besides, it return a BOOL value; I think you must misunderstand the function.

          R Offline
          R Offline
          rahuljin
          wrote on last edited by
          #4

          thanks for the reply. please tell me how to search an item in list control.

          C 1 Reply Last reply
          0
          • R rahuljin

            thanks for the reply. please tell me how to search an item in list control.

            C Offline
            C Offline
            carter2000
            wrote on last edited by
            #5

            Use FindItem() to get the index of the list control you want. int FindItem(LVFINDINFO* pFindInfo, int nStart = -1) const; ipAdd.GetAddress(m_M1, m_M2, m_M3, m_M4); LVFINDINFO temp; temp.flags = LVFI_PARTIAL|LVFI_STRING; _stprintf_s(temp.psz, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4); //I'm not sure if this would work int i = ipList.FindItem(&temp);

            R 1 Reply Last reply
            0
            • C carter2000

              Use FindItem() to get the index of the list control you want. int FindItem(LVFINDINFO* pFindInfo, int nStart = -1) const; ipAdd.GetAddress(m_M1, m_M2, m_M3, m_M4); LVFINDINFO temp; temp.flags = LVFI_PARTIAL|LVFI_STRING; _stprintf_s(temp.psz, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4); //I'm not sure if this would work int i = ipList.FindItem(&temp);

              R Offline
              R Offline
              rahuljin
              wrote on last edited by
              #6

              this code is giving error ----

              _stprintf_s(temp.psz, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4);

              error

              error C2664: 'int swprintf_s(wchar_t *,size_t,const wchar_t *,...)' : cannot convert parameter 1 from 'LPCWSTR' to 'wchar_t *'

              C 1 Reply Last reply
              0
              • N Naveen

                In addition to the above comment, you can use the FindItem() function of list control directly. No need to loop.

                nave [OpenedFileFinder] [My Blog]

                R Offline
                R Offline
                rahuljin
                wrote on last edited by
                #7

                thanks for the reply.

                1 Reply Last reply
                0
                • R rahuljin

                  this code is giving error ----

                  _stprintf_s(temp.psz, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4);

                  error

                  error C2664: 'int swprintf_s(wchar_t *,size_t,const wchar_t *,...)' : cannot convert parameter 1 from 'LPCWSTR' to 'wchar_t *'

                  C Offline
                  C Offline
                  carter2000
                  wrote on last edited by
                  #8

                  I'm sorry for that. You can try the code below. wchar_t ss[100]; _stprintf_s(ss, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4); //I'm not sure if this would work temp.psz = ss; Hope it can run.

                  R 1 Reply Last reply
                  0
                  • C carter2000

                    I'm sorry for that. You can try the code below. wchar_t ss[100]; _stprintf_s(ss, _T("%d : %d : %d : %d"), m_M1, m_M2, m_M3, m_M4); //I'm not sure if this would work temp.psz = ss; Hope it can run.

                    R Offline
                    R Offline
                    rahuljin
                    wrote on last edited by
                    #9

                    thanks, it is working now. also, in the second list control in application, there is check box in first column for ach item. please tell me how to get the items which are checked ?

                    C 1 Reply Last reply
                    0
                    • R rahuljin

                      thanks, it is working now. also, in the second list control in application, there is check box in first column for ach item. please tell me how to get the items which are checked ?

                      C Offline
                      C Offline
                      carter2000
                      wrote on last edited by
                      #10

                      But I know you can add 'LVN_ITEMCHANGED' Message handling functions for the CListCtrl, when checkbox's state is changed, the function would be activated, and you can check if the checkbox is be selected in the Message handling functions.

                      R 1 Reply Last reply
                      0
                      • C carter2000

                        But I know you can add 'LVN_ITEMCHANGED' Message handling functions for the CListCtrl, when checkbox's state is changed, the function would be activated, and you can check if the checkbox is be selected in the Message handling functions.

                        R Offline
                        R Offline
                        rahuljin
                        wrote on last edited by
                        #11

                        thanks. i am facing a new problem. i am trying to save the items of list control which are checked to a text file. here is the code ---

                        TCHAR ff\[100\] = {0};
                        CFile srvc;
                            srvc.Open(\_T("c:\\\\srvc.txt"), CFile::modeCreate|CFile::modeReadWrite);
                        for (int nItem = 0; nItem < srvcList.GetItemCount(); nItem++)
                        {
                        	if (srvcList.GetCheck(nItem))
                        	{
                        		
                        		srvcList.GetItemText(nItem, 0, (LPTSTR)ff, 98);
                        		wcscat\_s(ff, L"\\n");
                        		srvc.SeekToEnd();
                        		srvc.Write(ff, 100);
                        		ZeroMemory(ff, 100);
                        		
                        	}
                        
                        }
                        

                        the content of the file is not right ---

                        A e L o o k u p S v c
                        þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA p p H o s t S v c
                        þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA V P
                        þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB F E
                        þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB I T S
                        þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþ

                        there is extra space between each letter and the symble 'þ'. there is also no new line even with the use of '\n'.

                        modified on Saturday, July 18, 2009 11:05 PM

                        C 2 Replies Last reply
                        0
                        • R rahuljin

                          thanks. i am facing a new problem. i am trying to save the items of list control which are checked to a text file. here is the code ---

                          TCHAR ff\[100\] = {0};
                          CFile srvc;
                              srvc.Open(\_T("c:\\\\srvc.txt"), CFile::modeCreate|CFile::modeReadWrite);
                          for (int nItem = 0; nItem < srvcList.GetItemCount(); nItem++)
                          {
                          	if (srvcList.GetCheck(nItem))
                          	{
                          		
                          		srvcList.GetItemText(nItem, 0, (LPTSTR)ff, 98);
                          		wcscat\_s(ff, L"\\n");
                          		srvc.SeekToEnd();
                          		srvc.Write(ff, 100);
                          		ZeroMemory(ff, 100);
                          		
                          	}
                          
                          }
                          

                          the content of the file is not right ---

                          A e L o o k u p S v c
                          þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA p p H o s t S v c
                          þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA V P
                          þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB F E
                          þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB I T S
                          þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþ

                          there is extra space between each letter and the symble 'þ'. there is also no new line even with the use of '\n'.

                          modified on Saturday, July 18, 2009 11:05 PM

                          C Offline
                          C Offline
                          carter2000
                          wrote on last edited by
                          #12

                          Hi. I use " CString temp(ff); srvc.Write(ff, 2 * temp.GetLength() - 1); " instead of your "srvc.Write(ff, 100);", and it's works OK in my computer. Maybe you can try it, but it must not be a good solution. If it couldn't work, tell me and I would try to find another solution.

                          R 1 Reply Last reply
                          0
                          • R rahuljin

                            thanks. i am facing a new problem. i am trying to save the items of list control which are checked to a text file. here is the code ---

                            TCHAR ff\[100\] = {0};
                            CFile srvc;
                                srvc.Open(\_T("c:\\\\srvc.txt"), CFile::modeCreate|CFile::modeReadWrite);
                            for (int nItem = 0; nItem < srvcList.GetItemCount(); nItem++)
                            {
                            	if (srvcList.GetCheck(nItem))
                            	{
                            		
                            		srvcList.GetItemText(nItem, 0, (LPTSTR)ff, 98);
                            		wcscat\_s(ff, L"\\n");
                            		srvc.SeekToEnd();
                            		srvc.Write(ff, 100);
                            		ZeroMemory(ff, 100);
                            		
                            	}
                            
                            }
                            

                            the content of the file is not right ---

                            A e L o o k u p S v c
                            þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA p p H o s t S v c
                            þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþA V P
                            þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB F E
                            þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþB I T S
                            þþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþþ

                            there is extra space between each letter and the symble 'þ'. there is also no new line even with the use of '\n'.

                            modified on Saturday, July 18, 2009 11:05 PM

                            C Offline
                            C Offline
                            carter2000
                            wrote on last edited by
                            #13

                            srvc.Write(ff, 2 * lstrlen(ff) - 1); This can work in my computer too, maybe it's better than the last one I gave you.

                            R 1 Reply Last reply
                            0
                            • C carter2000

                              Hi. I use " CString temp(ff); srvc.Write(ff, 2 * temp.GetLength() - 1); " instead of your "srvc.Write(ff, 100);", and it's works OK in my computer. Maybe you can try it, but it must not be a good solution. If it couldn't work, tell me and I would try to find another solution.

                              R Offline
                              R Offline
                              rahuljin
                              wrote on last edited by
                              #14

                              now the content of the file is this ----

                              A p p M g m t A V P B F E

                              the content should be like this --- AppMgmt AVP BFE

                              1 Reply Last reply
                              0
                              • C carter2000

                                srvc.Write(ff, 2 * lstrlen(ff) - 1); This can work in my computer too, maybe it's better than the last one I gave you.

                                R Offline
                                R Offline
                                rahuljin
                                wrote on last edited by
                                #15

                                same problem as other. there is a extra space between each letter and no new line.

                                C 2 Replies Last reply
                                0
                                • R rahuljin

                                  same problem as other. there is a extra space between each letter and no new line.

                                  C Offline
                                  C Offline
                                  carter2000
                                  wrote on last edited by
                                  #16

                                  My OS is winows XP. And if I view the text by Notepad.exe, it's the same as you. But if I use anothor software to view the text, it just be OK.

                                  1 Reply Last reply
                                  0
                                  • R rahuljin

                                    same problem as other. there is a extra space between each letter and no new line.

                                    C Offline
                                    C Offline
                                    carter2000
                                    wrote on last edited by
                                    #17

                                    wcscat_s(ff, L"\n"); Em, try to replace the code by 1. wcscat_s(ff, L"\r\n"); 2. wcscat_s(ff, L"\r"); 3. wcscat_s(ff, L"\n\r");

                                    R 1 Reply Last reply
                                    0
                                    • C carter2000

                                      wcscat_s(ff, L"\n"); Em, try to replace the code by 1. wcscat_s(ff, L"\r\n"); 2. wcscat_s(ff, L"\r"); 3. wcscat_s(ff, L"\n\r");

                                      R Offline
                                      R Offline
                                      rahuljin
                                      wrote on last edited by
                                      #18

                                      i do some changes ----

                                      for (int nItem = 0; nItem < srvcList.GetItemCount(); nItem++)
                                      {
                                      if (srvcList.GetCheck(nItem))
                                      {
                                      srvcList.GetItemText(nItem, 0, (LPTSTR)ff, 98);
                                      //wcscat_s(ff, L"\r\n");
                                      srvc.SeekToEnd();
                                      int i = 2 * lstrlen(ff) - 2;
                                      int j = 0;
                                      while(i)
                                      {
                                      srvc.Write(&ff[j], 1);
                                      i--;
                                      j++;
                                      }
                                      }
                                      }

                                      the content is ---

                                      AVP BFE BITS

                                      if i use wcscat_s(ff, L"\r\n"), then

                                      AVP
                                      þþþBFE
                                      þþþBITS
                                      þþþþ

                                      this text file will be used by other program, therefore cant compromise with it.

                                      C 1 Reply Last reply
                                      0
                                      • R rahuljin

                                        i do some changes ----

                                        for (int nItem = 0; nItem < srvcList.GetItemCount(); nItem++)
                                        {
                                        if (srvcList.GetCheck(nItem))
                                        {
                                        srvcList.GetItemText(nItem, 0, (LPTSTR)ff, 98);
                                        //wcscat_s(ff, L"\r\n");
                                        srvc.SeekToEnd();
                                        int i = 2 * lstrlen(ff) - 2;
                                        int j = 0;
                                        while(i)
                                        {
                                        srvc.Write(&ff[j], 1);
                                        i--;
                                        j++;
                                        }
                                        }
                                        }

                                        the content is ---

                                        AVP BFE BITS

                                        if i use wcscat_s(ff, L"\r\n"), then

                                        AVP
                                        þþþBFE
                                        þþþBITS
                                        þþþþ

                                        this text file will be used by other program, therefore cant compromise with it.

                                        C Offline
                                        C Offline
                                        carter2000
                                        wrote on last edited by
                                        #19

                                        It looks inefficient :)

                                        R 1 Reply Last reply
                                        0
                                        • C carter2000

                                          It looks inefficient :)

                                          R Offline
                                          R Offline
                                          rahuljin
                                          wrote on last edited by
                                          #20

                                          still there is a problem, there is no new line. i want the content of the text file like this ----- AVP BFE BITS

                                          C 2 Replies Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups