How to show in the list box, the elements in Runewords struct by select an element from the ComboBox?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <windows.h>
#include <commctrl.h>

#define ARRAYSIZE(sel)  (sizeof(sel) / sizeof(sel[0]))
#define IDC_LIST_BOX    1000
#define IDC_COMBO_BOX1  1001

// Sockets Structure
typedef struct
{
    CHAR SocketsNr[4];
}SOCKETS;

SOCKETS sockets[] =
{
    {TEXT("2")}, {TEXT("3")}, {TEXT("4")}, {TEXT("5")}, {TEXT("6")}, {TEXT("All")}
};

// RuneWords Structure 
typedef struct
{
    CHAR Name[30];
}RUNEWORDS;

RUNEWORDS runewords[] =
{
    {TEXT("Leaf")}, {TEXT("Lore")}, {TEXT("Nadir")}, {TEXT("Rhyme")}, {TEXT("Smoke")}, {TEXT("Stealth")}, {TEXT("Steel")},
    {TEXT("Strength")}, {TEXT("White")}, {TEXT("Zephyr")}, {TEXT("Prudence")}, {TEXT("Splendor")}, {TEXT("Wind")}
};

void createCotrols(HWND);

HWND hMainWindow, hRwList, combo1;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	    case WM_CREATE:
	    {
		createCotrols(hwnd);
			
		CHAR buff[5];
                memset(&buff, 0, sizeof(buff));

                for(UINT i = 0; i < ARRAYSIZE(sockets); i++)
                {
                    strncpy(buff, sockets[i].SocketsNr, sizeof(buff)/sizeof(CHAR));

                    SendMessage(combo1, CB_ADDSTRING, 0, (LPARAM)buff);
                }
	    }break;
		
		case WM_COMMAND:
		{
			switch(LOWORD(wParam))
            {
                case IDC_COMBO_BOX1:
                {
                    if(HIWORD(wParam == CBN_SELCHANGE))
                    {
                        combo1 = GetDlgItem(hwnd, IDC_COMBO_BOX1);

                        int ItemIndex = SendMessage((HWND)lParam, (UINT)CB_GETCURSEL, (WPARAM)0, (LPARAM)0);

                        if(ItemIndex == 0)
                        {
                            hRwList = GetDlgItem(hwnd, IDC_LIST_BOX);

                            for(UINT i = 0; i < ARRAYSIZE(runewords); i++)
                            {
                                int pos = (int)SendMessage(hRwList, LB_ADDSTRING, 0, (LPARAM)runewords[i].Name);

                                SendMessage(hRwList, LB_SETITEMDATA, pos, (LPARAM)i);
                            }
                            SetFocus(hRwList);
                        }
                    }
                }break;
		return (0);
            }
        }break;
		}break;
		
		case WM_CLOSE:
		{
			DestroyWindow(hwnd);
		}break;
		
		case WM_DESTROY:
		{
			PostQuitMessage(0);
		}break;
		
		default:
			return(DefWindowProcA(hwnd, msg, wParam, lParam));
	}
	return(0);
}

void createCotrols(HWND hwnd)
{
	hRwList = CreateWindowA(WC_LISTBOXA, NULL, WS_VISIBLE | WS_CHILD | LBS_STANDARD | LBS_NOTIFY,
                            600, 40, 175, 200, hwnd, (HMENU)IDC_LIST_BOX, NULL, NULL);

    combo1 = CreateWindowA(WC_COMBOBOXA, NULL, WS_VISIBLE | WS_CHILD | CBS_HASSTRINGS | CBS_DROPDOWNLIST,
                           30, 40, 150, 200, hwnd, (HMENU)IDC_COMBO_BOX1, NULL, NULL);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow)
{
    InitCommonControls();

    WNDCLASSA wc = {};
	.
	..
	...
    return (0);
}
Last edited on
I tried this , but selecting 2 or 3 or anything will not show the Runewords struct elements.

I suppose that the case: IDC_COMBO_BOX1 is not even correct..

So.. anyone can help me solve this situation?

I want this to act more like a filter .. I mean what I post in here is a part of my struct. The list items is much bigger, dohh.. I just want to get the ideas how to do it.
I tried MSDN but is not what I want in totally. So my Goal is selecting an item from the Combo Box which has the first struct as elements , to point to another struct, and make it appear in a List Box control.

Thank you..
Last edited on
Okay .. I think I got iy.. but I got some issues if I choose something else after first selection which is 2 in the ComboBox... I mean I got a lot of garbage in the ListBox . I modify the code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case IDC_COMBO_BOX1:
                {
                    switch(HIWORD(wParam))
                    {
                        case CBN_SELCHANGE:
                        {
                            combo1 = GetDlgItem(hwnd, IDC_COMBO_BOX1);

                            int ItemIndex = SendMessage(combo1, CB_GETCURSEL, 0, 0);
                            int j = (int)SendMessage(combo1, LB_GETITEMDATA, ItemIndex, 0);

                            for(int n = 0; n <= j; n++)
                            {
                                if(n == 0)
                                {
                                    hRwList = GetDlgItem(hwnd, IDC_LIST_BOX);

                                    for(UINT i = 0; i < ARRAYSIZE(runewords); i++)
                                    {
                                        int pos = (int)SendMessage(hRwList, LB_ADDSTRING, 0, (LPARAM)runewords[i].Name);

                                        SendMessage(hRwList, LB_SETITEMDATA, pos, (LPARAM)i);
                                    }
                                    SetFocus(hRwList);
                                }break;
                            }
                        }break;
                    }
                }break;
            }
        }break;
Okay I got it.. I just need to clear the ListBox before the items are fill in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
case WM_COMMAND:
        {
            switch(LOWORD(wParam))
            {
                case IDC_COMBO_BOX1:
                {
                    switch(HIWORD(wParam))
                    {
                        case CBN_SELCHANGE:
                        {
                            combo1 = GetDlgItem(hwnd, IDC_COMBO_BOX1);

                            int ItemIndex = SendMessage(combo1, CB_GETCURSEL, 0, 0);
                            int j = (int)SendMessage(combo1, LB_GETITEMDATA, ItemIndex, 0);

                            SendMessage(hRwList, LB_RESETCONTENT, 0, 0); // <- here reset the list

                            for(int n = 0; n <= j; n++)
                            {
                                if(ItemIndex == 0)
                                {
                                    hRwList = GetDlgItem(hwnd, IDC_LIST_BOX);

                                    for(UINT i = 0; i < ARRAYSIZE(runewords); i++)
                                    {
                                        int pos = (int)SendMessage(hRwList, LB_ADDSTRING, 0, (LPARAM)runewords[i].Name);

                                        SendMessage(hRwList, LB_SETITEMDATA, pos, (LPARAM)i);
                                    }
                                    SetFocus(hRwList);
                                }break;
                            }
                        }break;
                    }
                }break;
            }
        }break;
In the end ..I got it on my own.. :)
Topic archived. No new replies allowed.