clistbox gettext error - visual-c++

I am trying to add items from alistbox to cstringarray. But text is null always. plz suggest the changes
int count = m_OutList.GetCount();
for ( i = 0; i <m_OutList.GetCount(); i++)
{
m_OutList.GetText( buf[i], text );
m_selcomponents->Add(text);
// getb //Add();
}

If text is a CString in your example, you need to write m_OutList.GetText(i,text) - you don't need a buffer variable if you pass a CString&. buf[i] is a part of your buffer and has a random value.

Related

How to set maximum length in String value DART

i am trying to set maximum length in string value and put '..' instead of removed chrs like following
String myValue = 'Welcome'
now i need the maximum length is 4 so output like following
'welc..'
how can i handle this ? thanks
The short and incorrect version is:
String abbrevBad(String input, int maxlength) {
if (input.length <= maxLength) return input;
return input.substring(0, maxLength - 2) + "..";
}
(Using .. is not the typographical way to mark an elision. That takes ..., the "ellipsis" symbol.)
A more internationally aware version would count grapheme clusters instead of code units, so it handles complex characters and emojis as a single character, and doesn't break in the middle of one. Might also use the proper ellipsis character.
String abbreviate(String input, int maxLength) {
var it = input.characters.iterator;
for (var i = 0; i <= maxLength; i++) {
if (!it.expandNext()) return input;
}
it.dropLast(2);
return "${it.current}\u2026";
}
That also works for characters which are not single code units:
void main() {
print(abbreviate("argelbargle", 7)); // argelb…
print(abbreviate("πŸ‡©πŸ‡°πŸ‡©πŸ‡°πŸ‡©πŸ‡°πŸ‡©πŸ‡°πŸ‡©πŸ‡°", 4)); // πŸ‡©πŸ‡°πŸ‡©πŸ‡°πŸ‡©πŸ‡°β€¦
}
(If you want to use ... instead of …, just change .dropLast(2) to .dropLast(4) and "…" to "...".)
You need to use RichText and you need to specify the overflow type, just like this:
Flexible(
child: RichText("Very, very, very looong text",
overflow: TextOverflow.ellipsis,
),
);
If the Text widget overflows, some points (...) will appears.

CCombobox string insertion gives gibberish

I created a custom CCustomCombo by extending CComboBox to implement a DrawItem() function. Here's the code for it.
void CCustomCombo::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
ASSERT( lpDrawItemStruct->CtlType == ODT_COMBOBOX );
LPCTSTR lpszText = ( LPCTSTR ) lpDrawItemStruct->itemData;
ASSERT( lpszText != NULL );
if ( lpDrawItemStruct->itemID == -1 || lpszText == NULL)
return;
CDC dc;
dc.Attach( lpDrawItemStruct->hDC );
// Save these value to restore them when done drawing.
COLORREF crOldTextColor = dc.GetTextColor();
COLORREF crOldBkColor = dc.GetBkColor();
// If this item is selected, set the background color
// and the text color to appropriate values. Erase
// the rect by filling it with the background color.
if ( ( lpDrawItemStruct->itemAction & ODA_SELECT ) &&
( lpDrawItemStruct->itemState & ODS_SELECTED ) )
{
dc.SetTextColor( ::GetSysColor( COLOR_HIGHLIGHTTEXT ) );
dc.SetBkColor( ::GetSysColor( COLOR_HIGHLIGHT ) );
dc.FillSolidRect( &lpDrawItemStruct->rcItem, ::GetSysColor( COLOR_HIGHLIGHT ) );
}
else
{
dc.FillSolidRect( &lpDrawItemStruct->rcItem, crOldBkColor );
}
// Draw the text.
dc.DrawText(
lpszText,
( int ) _tcslen( lpszText ),
&lpDrawItemStruct->rcItem,
DT_CENTER | DT_SINGLELINE | DT_VCENTER );
// Reset the background color and the text color back to their
// original values.
dc.SetTextColor( crOldTextColor );
dc.SetBkColor( crOldBkColor );
dc.Detach();
}
creation part -
m_selectionCombo.Create( WS_VSCROLL |
CBS_DROPDOWNLIST | WS_VISIBLE | WS_TABSTOP| CBS_OWNERDRAWFIXED,
rect, &m_wndSelectionBar, ID_TEMP_BTN))
Now the problem is with the adding string items to the combobox. When I'm using string objects, it always shows some unicode gibberish.
m_selectionCombo.InsertString(0, "One"); //works
char * one = "one";
m_selectionCombo.InsertString(0, one ); //works
CString one = "one";
m_selectionCombo.InsertString(0, one ); //shows gibberish
std::string one = "one";
char *cstr = &one[0];
m_wndSelectionBar.m_selectionCombo.InsertString(0, cstr ); //shows gibberish
The same results appear for AddString. The problem is that I have a set of doubles that I have to insert to the combobox. And I have no way of converting them to string without displaying gibberish. I tried half-a-dozen conversion methods and none worked. I'm literally at my wits end!
The funny thing is it worked perfectly before when I used CComboBox and not my CCustomCombo class/CBS_OWNERDRAWFIXED. I tried using CBS_HASSTRINGS, but it displayed nothing, not even the gibberish, so somehow the strings don't even get added in with CBS_HASSTRINGS.
I need the custom Draw method since I plan to highlight some of the dropdown items. I'm using windows 32, VS 2017.
Any help would be highly appreciated.
Thanks.
LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData;
OwnerDraw function is looking at itemData. itemData is assigned using CComboBox::SetItemData. It is not assigned using InsertString or other text functions.
char * one = "one";
m_selectionCombo.InsertString(0, one ); //works
The string and item data are stored in the same memory address when CBS_HASSTRINGS is not set.
See also documentation for CB_SETITEMDATA
If the specified item is in an owner-drawn combo box created without
the CBS_HASSTRINGS style, this message replaces the value in the
lParam parameter of the CB_ADDSTRING or CB_INSERTSTRING message that
added the item to the combo box.
So basically itemData returns a pointer one, and it works fine in this case.
CString one = "one";
m_selectionCombo.InsertString(0, one ); //shows gibberish
This time the string is created on stack, it is destroyed after the function exists. itemData points to invalid address.
Solution:
If you are setting the text using InsertString/AddString then make sure CBS_HASSTRINGS is set. And read the strings using GetLBText. Example:
//LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData; <- remove this
if(lpDrawItemStruct->itemID >= GetCount())
return;
CString str;
GetLBText(lpDrawItemStruct->itemID, str);
LPCTSTR lpszText = str;
Otherwise use SetItemData to setup data, and use itemData to read.

CComboBox FindString empty

The MFC CComboBox allows a person to do an AddString of an empty string. I just proved that by doing a GetCount() before and another after the AddString; Count was 0, then it became 1; and the GUI also seems to reflect it, as its list was an huge empty box, and when adding it became a one-liner.
I also proved it further by doing
int a = m_combo.GetCount();
CString sx= _T("Not empty string");
if(a == 1)
m_combo.GetLBText(0, sx);
TRACE(_T("Start<%s>End"), sx);
and the Output window displays
File.cpp(9) : atlTraceGeneral - Start<>End
so we conclude the sx variable is empty.
Then I do a FindString having a CString m_name variable which is empty:
int idx= m_combo.FindString(-1, m_name);
And it returns CB_ERR!
Is it standard behavior for empty string entries? Official documentation doesn't say anything about it!
If it is, what is the simplest way to override it? Is there some parameter or change in the resources to change the behaviour? If there is not, I am thinking about deriving or composing a class just for the case where the string is Empty!
I did it manually for the empty string and it works!
CString sItem;
int idx_aux= CB_ERR;
// need it because FindString, FindStringExact and SelectSring return CB_ERR when we provide an empty string to them!
if(m_name.IsEmpty())
{
for (int i=0; i<m_combo.GetCount(); i++)
{
m_combo.GetLBText(i, sItem);
if(sItem.IsEmpty())
{
idx_aux= i;
break;
}
}
}
else
idx_aux= m_combo.FindString(-1, m_name);

Get the value from string vecotr and store in another string?

I am trying to store the string value into vector. And then after storing I want to store in string one by one. In first step split the sting by "," and store into vector. And again try to retrive and get into string.
My code:
CString sAssocVal = "test1, test2, test3";
istringstream ss( sAssocVal.GetBuffer(sAssocVal.GetLength()) );
vector<string> words;
string token;
while( std::getline(ss, token, ',') )
{
words.push_back( token );
}
Try to again retrive from vector:
for(int i = 0; i<words.size(); i++)
std::string st= words[i];
But the value of st is getting NULL always.
where I am missing some thing.
AFAIK the problem is the istringstream initialization, a small modification makes your example work:
http://coliru.stacked-crooked.com/a/698818655cbba4e7
You could first convert CString to std::string, there are a lot of topics about that problem
I resolved this is by using this code.
CString st;
for(int i = 0; i<words.size(); i++)
st= words[i].c_str();

Print char[] to messagebox

I am taking the URL value from DocumentComplete and try to copy it to testDest[256]
Here is my code:
char *p= _com_util::ConvertBSTRToString(url->bstrVal);
for (int i = 0; i <= strlen(p); i++)
{
testDest[i] = p[i];
}
My question is, how can I print the testDest value on a messagebox?
The simplest way to create a message box is this:
MessageBox(NULL, testDest, "some title", 0);
If you already have a window and you want to associate the message box with that window, just change the first parameter from NULL to the window handle.
Also, if you're using Unicode, you should convert your char [] to TCHAR [] or otherwise call the ANSI version (MessageBoxA) explicitly.
You can do this
CString cstring( testDest);
AfxMessageBox(testDest);

Resources