Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding to list box while typing...

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
I would like to allow my user to type into an edit box, and while they're typing (in the OnChange event, in other words,) in a list box below, certain keywords would show up coresponding to what the user types.

for example:
if the user types 'A',
'APPLE', 'ANT', 'ALL', and ALWAYS would show up.

then, if the user tyles 'AL',
'ALL' and 'ALWAYS' would be left, and the other two are removed.

and finally, when the user enters 'ALW',
only 'ALWAYS' would remain.

I know the general theory behind going about this, but I can't get any specific code to work to my advantage. Does anyone know any means to do this, or any place that has how to do this?

Thanks, Cyprus
 
I built a class to compare the strings cause that way
I could code once and call often.
Then I checked the result(s) and acted on the input.
Mine are bool but you could easily either overload them to return a value of 1, 2, 3, 4 allowing a switch/case block
etc. The nice thing here is that I could accept input right
in a drop down box with preloaded values and jump to the
ItemIndex of the nearest match. I did it iterating through the drop down Indexes and truncating S2 (the ItemIndex text)
for comparison with the users key stroke(s) It would take a FAQ to demo all the ways you could do this, if you need more help reply back and I'll see what I can do about putting one together.

The Header file:
//---------------------------------------------------------------------------
class StringHandlerClass // String Handler Class
{
int len; // Generic String Length Measurement
String UserStr, // String being entered by user
MatchStr; // Partial string of Field value to match

private: // User declarations
public: // User declarations
StringHandlerClass( void ); //Constructor
bool StringHandlerClass::WholeField( String S1, String S2 );
bool StringHandlerClass::StartofField( String S1, String S2 );
bool StringHandlerClass::AnyPartofField( String S1, String S2 );
bool StringHandlerClass::EndofField( String S1, String S2 );
};

//---------------------------------------------------------------------------

The Unit:

bool StringHandlerClass::WholeField( String S1, String S2 )
{
if( S1.AnsiCompareIC(S2) == 0 )
return true;
return false;
}
//---------------------------------------------------------------------------
bool StringHandlerClass::StartofField( String S1, String S2 )
{
len = S1.Length(); // Length of USER supplied text
S2 = S2.SetLength(len); // Trunc data to length of user string
if( S1.AnsiCompareIC(S2) == 0 ) // Does it match request?
return true; // Yes, return true
return false; // No, return false
}
//---------------------------------------------------------------------------
bool StringHandlerClass::AnyPartofField( String S1, String S2 )
{
len = S1.Length(); // Length of User string
for( int i = 0; i < len; i++ ) // Search through Field for match
{
MatchStr = S2.SubString(i, len); // Assign current attempt
if( S1.AnsiCompareIC(MatchStr) == 0 )// Does it match?
return true; // Yes, return true
}
return false; // No, return false
}
//---------------------------------------------------------------------------
bool StringHandlerClass::EndofField( String S1, String S2 )
{
int i; // Index to start search from
len = S2.Length(); // Length of user string
i = len - ( S1.Length() - 1); // Index in Field to look for a match
MatchStr = S2.SubString(i,len); // Get string to match
if( S1.AnsiCompareIC(MatchStr) == 0 )// Check for match
return true; // Yes, return true
return false; // No, return false
}// S1.Length()-1 because ansistring counts current pos giving 1 too many chars
//---------------------------------------------------------------------------
 
I built a class to compare the strings cause that way
I could code once and call often.
Then I checked the result(s) and acted on the input.
Mine are bool but you could easily either overload them to return a value of 1, 2, 3, 4 allowing a switch/case block
etc. The nice thing here is that I could accept input right
in a drop down box with preloaded values and jump to the
ItemIndex of the nearest match. I did it iterating through the drop down Indexes and truncating S2 (the ItemIndex text)
for comparison with the users key stroke(s) It would take a FAQ to demo all the ways you could do this, if you need more help reply back and I'll see what I can do about putting one together.

The Header file:
Code:
//---------------------------------------------------------------------------
class StringHandlerClass        // String Handler Class
{
   int len;                     // Generic String Length Measurement
   String UserStr,              // String being entered by user
          MatchStr;             // Partial string of Field value to match

private:                	// User declarations
public:	                	// User declarations
StringHandlerClass( void );     //Constructor
bool StringHandlerClass::WholeField( String S1, String S2 );
bool StringHandlerClass::StartofField( String S1, String S2 );
bool StringHandlerClass::AnyPartofField( String S1, String S2 );
bool StringHandlerClass::EndofField( String S1, String S2 );
};

//---------------------------------------------------------------------------

The Unit:

bool StringHandlerClass::WholeField( String S1, String S2 )
{
   if( S1.AnsiCompareIC(S2) == 0 )
      return true;
   return false;
}
//---------------------------------------------------------------------------
bool StringHandlerClass::StartofField( String S1, String S2 )
{
   len = S1.Length();              // Length of USER supplied text
   S2 = S2.SetLength(len);         // Trunc data to length of user string
   if( S1.AnsiCompareIC(S2) == 0 ) // Does it match request?
      return true;                 // Yes, return true
   return false;                   // No, return false
}
//---------------------------------------------------------------------------
bool StringHandlerClass::AnyPartofField( String S1, String S2 )
{
   len = S1.Length();                      // Length of User string
   for( int i = 0; i < len; i++ )          // Search through Field for match
   {
      MatchStr = S2.SubString(i, len);     // Assign current attempt
      if( S1.AnsiCompareIC(MatchStr) == 0 )// Does it match?
         return true;                      // Yes, return true
   }
   return false;                           // No, return false
}
//---------------------------------------------------------------------------
bool StringHandlerClass::EndofField( String S1, String S2 )
{
   int i;                               // Index to start search from
   len = S2.Length();                   // Length of user string
   i = len - ( S1.Length() - 1);        // Index in Field to look for a match
   MatchStr = S2.SubString(i,len);      // Get string to match
   if( S1.AnsiCompareIC(MatchStr) == 0 )// Check for match
      return true;                      // Yes, return true
   return false;                        // No, return false
}// S1.Length()-1 because ansistring counts current pos giving 1 too many chars
//---------------------------------------------------------------------------

to create an instance of this class is easy:

StringHandlerClass SHC; // SHC could have been anything
StringHandlerClass MickeyMouse; // Would work

then:
if( SHC.EndofField( UserStr, DropDownStr ) == true );
dosomething();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top