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 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
            • R rahuljin

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

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

              You can try veiwing the text in Edit Plus or other text edit software.

              1 Reply Last reply
              0
              • R rahuljin

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

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

                You can try this, open the "srvc.txt", and select all text, and copy them, and open anthor TextEditor(Edit Plus), and paste. If still there is no new line, then I have no ideas. It's seems that Microsoft define a new line as '\r\n', while in Unix it's '\n'.

                R 1 Reply Last reply
                0
                • C carter2000

                  You can try this, open the "srvc.txt", and select all text, and copy them, and open anthor TextEditor(Edit Plus), and paste. If still there is no new line, then I have no ideas. It's seems that Microsoft define a new line as '\r\n', while in Unix it's '\n'.

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

                  thanks for all the support. finally it is done. the code is not that good ---

                  TCHAR ff\[100\] = {0};
                  CStdioFile 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, 100);
                  		int i = 2 \* lstrlen(ff) - 2;
                  		srvc.SeekToEnd();
                  		int j = 0;
                  		while(i)
                  		{
                  			srvc.Write(&ff\[j++\], 1);
                  			--i;				
                  		}
                  		ZeroMemory(ff, 100);
                  		srvc.SeekToEnd();
                  		srvc.Write("\\n", 1);
                  	}
                  }
                  

                  output is ---

                  Audiosrv
                  AVP
                  BFE
                  BITS

                  also please tell me that is there a way due to which edit box control can take only numeric values and with in a bound, say 1 to 1000 only ???

                  modified on Sunday, July 19, 2009 5:19 PM

                  C 1 Reply Last reply
                  0
                  • R rahuljin

                    thanks for all the support. finally it is done. the code is not that good ---

                    TCHAR ff\[100\] = {0};
                    CStdioFile 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, 100);
                    		int i = 2 \* lstrlen(ff) - 2;
                    		srvc.SeekToEnd();
                    		int j = 0;
                    		while(i)
                    		{
                    			srvc.Write(&ff\[j++\], 1);
                    			--i;				
                    		}
                    		ZeroMemory(ff, 100);
                    		srvc.SeekToEnd();
                    		srvc.Write("\\n", 1);
                    	}
                    }
                    

                    output is ---

                    Audiosrv
                    AVP
                    BFE
                    BITS

                    also please tell me that is there a way due to which edit box control can take only numeric values and with in a bound, say 1 to 1000 only ???

                    modified on Sunday, July 19, 2009 5:19 PM

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

                    you solve the problem by yourself, congratulation :) . maybe you can try replace the follow codes by "srvc.Write(ff, 2 * lstrlen(ff));", it works too in my computer.

                    rahuljin wrote:

                    while(i) { srvc.Write(&ff[j++], 1); --i; }

                    R 1 Reply Last reply
                    0
                    • C carter2000

                      you solve the problem by yourself, congratulation :) . maybe you can try replace the follow codes by "srvc.Write(ff, 2 * lstrlen(ff));", it works too in my computer.

                      rahuljin wrote:

                      while(i) { srvc.Write(&ff[j++], 1); --i; }

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

                      thanks. if you didnt guide and help me, it would be impossible for me. please answer my last question. one more problem found --- in first list control, when i enter an ip say "111.111.111.111", then i cannot enter the ip address "111.111.111.11". also "111.111.111.11" deletes "111.111.111.111" from the list control. here is the code ----

                      BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                      TCHAR ss\[50\] = {0};
                      UpdateData();
                      int i = -1;
                      LVFINDINFO temp;
                      
                      ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                      temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                      
                      \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                      temp.psz = ss;
                      i = ipList.FindItem(&temp);
                       
                      if(radBut1->GetState())
                      {
                      	if(m\_M1 && m\_M2 && m\_M3 && m\_M4 && i == -1)
                      	{
                      		ipList.InsertItem(s++, (LPCTSTR)ss);
                      		ipAdd.ClearAddress();
                      	}
                      }
                      
                      else if(radBut2->GetState())
                      {
                      
                      	if(i != -1)
                      		ipList.DeleteItem(i);
                      	else 
                      		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                      
                      
                      	ipAdd.ClearAddress();
                      

                      modified on Monday, July 20, 2009 3:17 AM

                      C 3 Replies Last reply
                      0
                      • R rahuljin

                        thanks. if you didnt guide and help me, it would be impossible for me. please answer my last question. one more problem found --- in first list control, when i enter an ip say "111.111.111.111", then i cannot enter the ip address "111.111.111.11". also "111.111.111.11" deletes "111.111.111.111" from the list control. here is the code ----

                        BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                        TCHAR ss\[50\] = {0};
                        UpdateData();
                        int i = -1;
                        LVFINDINFO temp;
                        
                        ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                        temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                        
                        \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                        temp.psz = ss;
                        i = ipList.FindItem(&temp);
                         
                        if(radBut1->GetState())
                        {
                        	if(m\_M1 && m\_M2 && m\_M3 && m\_M4 && i == -1)
                        	{
                        		ipList.InsertItem(s++, (LPCTSTR)ss);
                        		ipAdd.ClearAddress();
                        	}
                        }
                        
                        else if(radBut2->GetState())
                        {
                        
                        	if(i != -1)
                        		ipList.DeleteItem(i);
                        	else 
                        		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                        
                        
                        	ipAdd.ClearAddress();
                        

                        modified on Monday, July 20, 2009 3:17 AM

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

                        rahuljin wrote:

                        ipList.DeleteItem(i);

                        What's "s" means? The last index of CListCtrl? So if it's, then here

                        rahuljin wrote:

                        ipList.DeleteItem(i);

                        after deleting a item, you must also descend "s". I don't know if this is the reason.

                        R 1 Reply Last reply
                        0
                        • C carter2000

                          rahuljin wrote:

                          ipList.DeleteItem(i);

                          What's "s" means? The last index of CListCtrl? So if it's, then here

                          rahuljin wrote:

                          ipList.DeleteItem(i);

                          after deleting a item, you must also descend "s". I don't know if this is the reason.

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

                          still the same problem ---

                          ipList.DeleteItem(i);
                          s--;

                          1 Reply Last reply
                          0
                          • R rahuljin

                            thanks. if you didnt guide and help me, it would be impossible for me. please answer my last question. one more problem found --- in first list control, when i enter an ip say "111.111.111.111", then i cannot enter the ip address "111.111.111.11". also "111.111.111.11" deletes "111.111.111.111" from the list control. here is the code ----

                            BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                            TCHAR ss\[50\] = {0};
                            UpdateData();
                            int i = -1;
                            LVFINDINFO temp;
                            
                            ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                            temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                            
                            \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                            temp.psz = ss;
                            i = ipList.FindItem(&temp);
                             
                            if(radBut1->GetState())
                            {
                            	if(m\_M1 && m\_M2 && m\_M3 && m\_M4 && i == -1)
                            	{
                            		ipList.InsertItem(s++, (LPCTSTR)ss);
                            		ipAdd.ClearAddress();
                            	}
                            }
                            
                            else if(radBut2->GetState())
                            {
                            
                            	if(i != -1)
                            		ipList.DeleteItem(i);
                            	else 
                            		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                            
                            
                            	ipAdd.ClearAddress();
                            

                            modified on Monday, July 20, 2009 3:17 AM

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

                            rahuljin wrote:

                            if(i != -1) ipList.DeleteItem(i);

                            Relpace the codes with: if (i != -1 && lstrcmp(ipList.GetItemText(i), temp.psz) == 0) { ipList.DeleteItem(i); }

                            R 1 Reply Last reply
                            0
                            • R rahuljin

                              thanks. if you didnt guide and help me, it would be impossible for me. please answer my last question. one more problem found --- in first list control, when i enter an ip say "111.111.111.111", then i cannot enter the ip address "111.111.111.11". also "111.111.111.11" deletes "111.111.111.111" from the list control. here is the code ----

                              BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                              TCHAR ss\[50\] = {0};
                              UpdateData();
                              int i = -1;
                              LVFINDINFO temp;
                              
                              ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                              temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                              
                              \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                              temp.psz = ss;
                              i = ipList.FindItem(&temp);
                               
                              if(radBut1->GetState())
                              {
                              	if(m\_M1 && m\_M2 && m\_M3 && m\_M4 && i == -1)
                              	{
                              		ipList.InsertItem(s++, (LPCTSTR)ss);
                              		ipAdd.ClearAddress();
                              	}
                              }
                              
                              else if(radBut2->GetState())
                              {
                              
                              	if(i != -1)
                              		ipList.DeleteItem(i);
                              	else 
                              		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                              
                              
                              	ipAdd.ClearAddress();
                              

                              modified on Monday, July 20, 2009 3:17 AM

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

                              BYTE m_M1, m_M2, m_M3, m_M4;
                              TCHAR ss[50] = {0};
                              UpdateData();
                              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);
                              int compare;
                              for (int i = 0; i < ipList.GetItemCount(), ++i)
                              {
                              compare = lstrcmp(ss, ipList.GetItemText(i));
                              if(radBut1->GetState())
                              {
                              if(m_M1 && m_M2 && m_M3 && m_M4 && compare != 0)
                              {
                              ipList.InsertItem(s++, (LPCTSTR)ss);
                              ipAdd.ClearAddress();
                              }
                              }
                              else if(radBut2->GetState())
                              {
                              if(compare == 0)
                              ipList.DeleteItem(i);
                              else
                              MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB_ICONWARNING | MB_OK);
                              ipAdd.ClearAddress();
                              }
                              }

                              This is the final version. :)

                              R 1 Reply Last reply
                              0
                              • C carter2000

                                rahuljin wrote:

                                if(i != -1) ipList.DeleteItem(i);

                                Relpace the codes with: if (i != -1 && lstrcmp(ipList.GetItemText(i), temp.psz) == 0) { ipList.DeleteItem(i); }

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

                                thanks, both the problems are solved.

                                BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                                    TCHAR ss\[50\] = {0};
                                UpdateData();
                                int i = -1;
                                LVFINDINFO temp;
                                
                                    ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                                temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                                
                                \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                                temp.psz = ss;
                                i = ipList.FindItem(&temp);
                                 
                                if(radBut1->GetState())
                                {
                                	if(m\_M1 && (i == -1 || lstrcmp(ipList.GetItemText(i, 0), temp.psz) != 0) )
                                	{
                                		ipList.InsertItem(s++, (LPCTSTR)ss);
                                		ipAdd.ClearAddress();
                                	}
                                }
                                
                                else if(radBut2->GetState())
                                {
                                
                                	if (i != -1 && lstrcmp(ipList.GetItemText(i, 0), temp.psz) == 0)
                                	{
                                		ipList.DeleteItem(i);
                                		s--;
                                	}
                                
                                	else 
                                		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                                
                                
                                	ipAdd.ClearAddress();
                                
                                		
                                }
                                
                                C 1 Reply Last reply
                                0
                                • R rahuljin

                                  thanks, both the problems are solved.

                                  BYTE m\_M1, m\_M2, m\_M3, m\_M4;
                                      TCHAR ss\[50\] = {0};
                                  UpdateData();
                                  int i = -1;
                                  LVFINDINFO temp;
                                  
                                      ipAdd.GetAddress(m\_M1, m\_M2, m\_M3, m\_M4);
                                  temp.flags = LVFI\_PARTIAL|LVFI\_STRING;
                                  
                                  \_stprintf\_s(ss, \_T("%d.%d.%d.%d"), m\_M1, m\_M2, m\_M3, m\_M4);
                                  temp.psz = ss;
                                  i = ipList.FindItem(&temp);
                                   
                                  if(radBut1->GetState())
                                  {
                                  	if(m\_M1 && (i == -1 || lstrcmp(ipList.GetItemText(i, 0), temp.psz) != 0) )
                                  	{
                                  		ipList.InsertItem(s++, (LPCTSTR)ss);
                                  		ipAdd.ClearAddress();
                                  	}
                                  }
                                  
                                  else if(radBut2->GetState())
                                  {
                                  
                                  	if (i != -1 && lstrcmp(ipList.GetItemText(i, 0), temp.psz) == 0)
                                  	{
                                  		ipList.DeleteItem(i);
                                  		s--;
                                  	}
                                  
                                  	else 
                                  		MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB\_ICONWARNING | MB\_OK);
                                  
                                  
                                  	ipAdd.ClearAddress();
                                  
                                  		
                                  }
                                  
                                  C Offline
                                  C Offline
                                  carter2000
                                  wrote on last edited by
                                  #31

                                  I think your code still has some problems. Consider the situation below: There are two items in CListCtrl. 111.111.111.111 111.111.111.11 And you enter 111.111.111.11 to insert the new item, see what happen. If I am right, it will insert successfully!!That may not be what you want.

                                  R 1 Reply Last reply
                                  0
                                  • C carter2000

                                    BYTE m_M1, m_M2, m_M3, m_M4;
                                    TCHAR ss[50] = {0};
                                    UpdateData();
                                    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);
                                    int compare;
                                    for (int i = 0; i < ipList.GetItemCount(), ++i)
                                    {
                                    compare = lstrcmp(ss, ipList.GetItemText(i));
                                    if(radBut1->GetState())
                                    {
                                    if(m_M1 && m_M2 && m_M3 && m_M4 && compare != 0)
                                    {
                                    ipList.InsertItem(s++, (LPCTSTR)ss);
                                    ipAdd.ClearAddress();
                                    }
                                    }
                                    else if(radBut2->GetState())
                                    {
                                    if(compare == 0)
                                    ipList.DeleteItem(i);
                                    else
                                    MessageBox((LPCTSTR)L"Error: Address not found", (LPCTSTR)L"Delete Address", MB_ICONWARNING | MB_OK);
                                    ipAdd.ClearAddress();
                                    }
                                    }

                                    This is the final version. :)

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

                                    got another solution ---

                                    temp.flags = LVFI_STRING;

                                    and everything becomes right.

                                    modified on Monday, July 20, 2009 5:46 AM

                                    C 1 Reply Last reply
                                    0
                                    • R rahuljin

                                      got another solution ---

                                      temp.flags = LVFI_STRING;

                                      and everything becomes right.

                                      modified on Monday, July 20, 2009 5:46 AM

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

                                      You are right. :)

                                      R 1 Reply Last reply
                                      0
                                      • C carter2000

                                        I think your code still has some problems. Consider the situation below: There are two items in CListCtrl. 111.111.111.111 111.111.111.11 And you enter 111.111.111.11 to insert the new item, see what happen. If I am right, it will insert successfully!!That may not be what you want.

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

                                        in that case, 'i' will not be -1 and lstrcmp(ipList.GetItemText(i, 0), temp.psz) will be equal to zero because the item text and temp.psz will be same.

                                        C 1 Reply Last reply
                                        0
                                        • R rahuljin

                                          in that case, 'i' will not be -1 and lstrcmp(ipList.GetItemText(i, 0), temp.psz) will be equal to zero because the item text and temp.psz will be same.

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

                                          Before you changed the "temp.flags = LVFI_PARTIAL|LVFI_STRING;" to "tem.flags=LVFI_STRING;", it's a wrong case exactly. But now it isn't. :)

                                          1 Reply 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