ok well i'm having issues sending a char array to a listbox. To do a little explanation on the code itself... The point of this part is to take a hexidecimal string and convert it to a alphabetical string (this part works and has been tested in a console application). I then have a vShowText function that i used to add items into my listbox. It has worked thus far and i have even added char arrays to the listbox using it. In this particular case i can't seem to make the char array show up into the listbox, it just inserts a line of D like characters. This is sorta difficult to explain so i hope i'm doing a good enough job of it. I will post code to help explain more below.

here is my vShowText function that is being used to insert text into the listbox.

void vShowText(HWND	hChildHandle, char *szText)
{
	int Line;
	
	// add string to the listbox
	SendMessage(hChildHandle,LB_ADDSTRING,0,(LPARAM)szText);
	
	// determine number of items in listbox
	Line = SendMessage(hChildHandle,LB_GETCOUNT,0,0);
	
	// flag last item as the selected item, to scroll listbox down
	SendMessage(hChildHandle,LB_SETCURSEL,Line-1,0);
	
	// unflag all items to eliminate negative highlite
	SendMessage(hChildHandle,LB_SETCURSEL,-1,0);
}

below is where my problem is. This is the actual code to change the hex string into a alphabetical one. When the loop is done going through the hex string, ptrBuffer is reset to point to the first element in the char buffer[100] array. Then vShowText is called to display the contents of the buffer array into the listbox. I have also tried to just do vShowText(hlistbox, buffer). Both of these methods does the same thing and inserts a line of D like characters into the listbox... so it looks something like this DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD. Only they aren't actual D's.

also i am able to just make a char array and then later call vShowText with the array as a parameter and it displays in the listbox fine. Example is below.

//create a char array
char myArray[100] = "Hello World!";

//later in the program
vShowText(hlistbox, myArray);


the actual code snipplet:

case MENU_FILE_TRANS:
{
	char *ptrBuffer = &buffer[0];

	for (int i = 0; i < RecvBytes; i+=2)
	{
		int firstvalue = RecvData[i] - '0';
		int secondvalue;
		//if RecvData[i+1] is a letter convert it to integer, otherwise use it.
			switch(RecvData[i+1])
			{
			case 'A':
			{
				secondvalue = 10;
                 
			}break;
			case 'B':
			{
				secondvalue = 11;
                 
			}break;
			case 'C':
			{
				secondvalue = 12;
                 
			}break;
			case 'D':
			{
				secondvalue = 13;
                 
			}break;
			case 'E':
			{
				secondvalue = 14;
                 
			}break;
			case 'F':
			{
				secondvalue = 15;
                 
			}break;
			default:
				secondvalue = RecvData[i+1] - '0';
			break;
		}
         
         
		//convert the two values into decimal form
		newval =  16 * firstvalue + secondvalue;
         
		//change newval into a character
		*ptrBuffer = char(newval);

		//SendMessage(hlistbox,LB_ADDSTRING,0,(LPARAM)ptrBuffer);
		ptrBuffer++;

		//vShowText(hlistbox, ptrBuffer);
	}

	ptrBuffer = &buffer[0];


	vShowText(hlistbox, ptrBuffer);
	

}break;

I need some help man I can't figure this out. I am using code blocks with wxwidgets and I cant figure out how to CASE and SWITCH. Here is my code. All I have is a list box with some items in it and a button. I just want to be able to select an item and then press a button and have it do something like launch internet explorer or anything where each item will do something different. If I can get it to work I can figure it out from there. Thanks in advance for any help on this.

void ListBoxFrame::OnButton3Click(wxCommandEvent& event)
{

           for (int i = 0; i < ListBox1.IsSelected.Count; i++)
            {
                switch (ListBox1.IsSelected[i].ToString())
                {
                    case "Foobar":
                        wxLogMessage(_T("Listbox item foobar selected"));
                        break;
                    case "Bazquirk":
                        wxLogMessage(_T("Listbox item Bazquirk selected"));
                        break;
                    case "Widgets":
                        wxLogMessage(_T("Listbox item Widgets selected"));
                        break;
                    case "Gadgets":
                        wxLogMessage(_T("Listbox item Gadgets selected"));

                        break;
                }
            }
}

Recommended Answers

All 5 Replies

I you can't use a switch statement on strings, you have to use an if and else ifs

void ListBoxFrame::OnButton3Click(wxCommandEvent& event)
{
	wxString s;
	
	for (int i = 0; i < ListBox1.IsSelected.Count; i++)
	{
		s = ListBox1.IsSelected[i].ToString();

		if (s == "Foobar")
			wxLogMessage(_T("Listbox item foobar selected"));

		else if (s == "Bazquirk")
			wxLogMessage(_T("Listbox item Bazquirk selected"));

		else if (s == "Widgets")
			wxLogMessage(_T("Listbox item Widgets selected"));

		else if (s == "Gadgets")
			wxLogMessage(_T("Listbox item Gadgets selected"));
	}
}

This is awesome but it is throwing errors.

C:\Users\ME\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|118|error: request for member 'IsSelected' in '((ListBox_IF_ElseFrame*)this)->ListBox_IF_ElseFrame::ListBox1', which is of non-class type 'wxListBox*


C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp||In member function 'void ListBox_IF_ElseFrame::OnButton1Click(wxCommandEvent&)':|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|121|error: request for member 'IsSelected' in '((ListBox_IF_ElseFrame*)this)->ListBox_IF_ElseFrame::ListBox1', which is of non-class type 'wxListBox*'|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|123|error: request for member 'IsSelected' in '((ListBox_IF_ElseFrame*)this)->ListBox_IF_ElseFrame::ListBox1', which is of non-class type 'wxListBox*'|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|125|error: ambiguous overload for 'operator==' in 's == "Foobar"'|
C:\wxWidgets2.8\include\wx\string.h|1560|note: candidates are: bool operator==(const wxString&, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1564|note: bool operator==(const wxChar*, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1598|note: bool operator==(const wxString&, const wxWCharBuffer&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1633|note: bool operator==(wxChar, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1634|note: bool operator==(const wxString&, wxChar) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1044|note: bool operator==(long int, const wxLongLong&) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1057|note: bool operator==(long unsigned int, const wxULongLong&) <near match>|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|128|error: ambiguous overload for 'operator==' in 's == "Bazquirk"'|
C:\wxWidgets2.8\include\wx\string.h|1560|note: candidates are: bool operator==(const wxString&, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1564|note: bool operator==(const wxChar*, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1598|note: bool operator==(const wxString&, const wxWCharBuffer&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1633|note: bool operator==(wxChar, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1634|note: bool operator==(const wxString&, wxChar) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1044|note: bool operator==(long int, const wxLongLong&) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1057|note: bool operator==(long unsigned int, const wxULongLong&) <near match>|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|131|error: ambiguous overload for 'operator==' in 's == "Widgets"'|
C:\wxWidgets2.8\include\wx\string.h|1560|note: candidates are: bool operator==(const wxString&, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1564|note: bool operator==(const wxChar*, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1598|note: bool operator==(const wxString&, const wxWCharBuffer&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1633|note: bool operator==(wxChar, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1634|note: bool operator==(const wxString&, wxChar) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1044|note: bool operator==(long int, const wxLongLong&) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1057|note: bool operator==(long unsigned int, const wxULongLong&) <near match>|
C:\Users\Papayrus\Desktop\ListBox_IF_Else\ListBox_IF_ElseMain.cpp|134|error: ambiguous overload for 'operator==' in 's == "Gadgets"'|
C:\wxWidgets2.8\include\wx\string.h|1560|note: candidates are: bool operator==(const wxString&, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1564|note: bool operator==(const wxChar*, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1598|note: bool operator==(const wxString&, const wxWCharBuffer&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1633|note: bool operator==(wxChar, const wxString&) <near match>|
C:\wxWidgets2.8\include\wx\string.h|1634|note: bool operator==(const wxString&, wxChar) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1044|note: bool operator==(long int, const wxLongLong&) <near match>|
C:\wxWidgets2.8\include\wx\longlong.h|1057|note: bool operator==(long unsigned int, const wxULongLong&) <near match>|
||=== Build finished: 6 errors, 0 warnings ===|

I need some help man I can't figure this out. I am using code blocks with wxwidgets and I cant figure out how to CASE and SWITCH. Here is my code. All I have is a list box with some items in it and a button. I just want to be able to select an item and then press a button and have it do something like launch internet explorer or anything where each item will do something different. If I can get it to work I can figure it out from there. Thanks in advance for any help on this.

void ListBoxFrame::OnButton3Click(wxCommandEvent& event)
{

           for (int i = 0; i < ListBox1.IsSelected.Count; i++)
            {
                switch (ListBox1.IsSelected[i].ToString())
                {
                    case "Foobar":
                        wxLogMessage(_T("Listbox item foobar selected"));
                        break;
                    case "Bazquirk":
                        wxLogMessage(_T("Listbox item Bazquirk selected"));
                        break;
                    case "Widgets":
                        wxLogMessage(_T("Listbox item Widgets selected"));
                        break;
                    case "Gadgets":
                        wxLogMessage(_T("Listbox item Gadgets selected"));

                        break;
                }
            }
}

ListBox1.IsSelected.Count
ListBox1.IsSelected.ToString()
^ This seems strange to me. You're using ListBox1.IsSelected both like an object and like an array. If that's what you meant to do then nevermind my butting in.
-Greywolf

Actually I like this method anyway how can I make it work?

You could try casting the wxString to a cstring and using a function like strcmp to compare the strings. Or maybe you could do something like if (s == wxString("Foobar")) I am not too familiar with wxwidgets, so I am not exactly sure what will work, but those seem like plausible options.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.