YOU CAN CODE!

 

With The Case Of UCanCode.net  Release The Power OF  Visual C++ !   HomeProducts | PurchaseSupport | Downloads  
Download Evaluation
Pricing & Purchase?
E-XD++Visual C++/ MFC Products
Overview
Features Tour 
Electronic Form Solution
Visualization & HMI Solution
Power system HMI Solution
CAD Drawing and Printing Solution

Bar code labeling Solution
Workflow Solution

Coal industry HMI Solution
Instrumentation Gauge Solution

Report Printing Solution
Graphical modeling Solution
GIS mapping solution

Visio graphics solution
Industrial control SCADA &HMI Solution
BPM business process Solution

Industrial monitoring Solution
Flowchart and diagramming Solution
Organization Diagram Solution

Graphic editor Source Code
UML drawing editor Source Code
Map Diagramming Solution

Architectural Graphic Drawing Solution
Request Evaluation
Purchase
ActiveX COM Products
Overview
Download
Purchase
Technical Support
  General Q & A
Discussion Board
Contact Us

Links

Get Ready to Unleash the Power of UCanCode .NET

 


UCanCode Software focuses on general application software development. We provide complete solution for developers. No matter you want to develop a simple database workflow application, or an large flow/diagram based system, our product will provide a complete solution for you. Our product had been used by hundreds of top companies around the world!

"100% source code provided! Free you from not daring to use components because of unable to master the key technology of components!"


VC++ MFC Tutorial: Extend CStringArray and CString

 
 By Anders Eriksson. 

An extended CStringArray class

  • Introduction

    One of the reasons I left C and began to use C++ was CString and even more so CStringArray. These two classes are worth all the other aggravation when going OO. I don't claim that they are perfect. There are things missing in both of those classes and I would like to share this extension of CStringArray that I have made.

  • Finding an string in a CStringArray

    One of the things that I think are missing in CStringArray is a Find method. I find myself wanting to get a specific string out of the array and I know part, or the whole, of it. Not the position in the array. So I added this method. It is very straight forward and uses CString::Find to find the string.

    Example:

    	meStringArray arr;
    	
    	// fill the array with strings using the normal Add()
    	arr.Add("This is a String");
    	arr.Add("The second string");
    	arr.Add("the 3:rd string");
    	
    	// find the third string
       	int i=arr.Find("3:rd");
       	
       	// i is now 2
       	CString sBuf;
       	
       	sBuf=arr.GetAt(i);
       	
       	// sBuf has now the value of "the 3:rd string"
    
    

    Array of NULL terminating strings

    Some SDK-functions and some classes still using an array of NULL terminating strings. e.g. CFileDialog uses this kind of array for the filters. GetProfileString("section",NULL,"",buf,sizeof(buf)); also uses it, and there are many more. So every time you need to use this class/function you have to go back to C and do a lot of loops. Well I finally got tired of it and extended CStringArray with two methods; AddBuf() and GetBuf(). The first will take an array of NULL terminating strings and add it to the StringArray, and the second will do the reverse

    Example GetBuf:

    	CFileDialog dlg;
    	meStringArray filter;
    	
    	// init filter
    	filter.Add("All files");
    	filter.Add("*.*");
    	filter.Add("Doc files");
    	filter.Add("*.doc");
    	filter.Add("Text files");
    	filter.Add("*.txt");
    	
    	// Init file dialog
    	dlg.m_ofn.lpstrFilter=filter.GetBuf();			
    	dlg.m_ofn.lpstrInitialDir=initDir;
    	dlg.m_ofn.Flags |= OFN_SHAREAWARE;
    	int rc=dlg.DoModal();
    	if(rc==IDOK)
    	{
    		m_filepath=dlg.GetPathName();
    		UpdateData(FALSE);
    	}
    

    Example AddBuf:

    	meStringArray arr;
    	char buf[1024];
    	
    	// Get all printer names
    	GetProfileString("devices",NULL,"",buf,sizeof(buf));	
    	
    	// add them to the array
    	arr.AddBuf(buf);
    	
    	// Get the second printer name from the array
    	CString sPrintName = arr.GetAt(2);
    

    Well that's it! It's not a very large class so there is not so much to say. I hope that someone will find use for this and PLEASE let me know what you think! This is the first class I ever published so please say what you think about it, good, bad.

     

     

    Copyright ?1998-2022 UCanCode.Net Software , all rights reserved.
    Other product and company names herein may be the trademarks of their respective owners.

    Please direct your questions or comments to webmaster@ucancode.net