开发者

reading from ComboBox

开发者 https://www.devze.com 2023-02-28 21:09 出处:网络
how can i read the text of a selected value of a comboBox in windows aplication(borland C++) for example:

how can i read the text of a selected value of a comboBox in windows aplication(borland C++) for example: i have combobox which contains 2 values (sum and mult) i want to see if it is sum i have to add the numbers and if it mult i have开发者_JAVA技巧 to multiplicate the numbers so how can i read the value of combobox in this case.


For Windows:

In your window procedure use the WM_COMMAND message and then check for a CBN_SELCHANGE notification. Then use WM_GETTEXT along with WM_GETTEXTLENGTH to receive the selected text like Mark Ingram says. Or you can also use CB_GETCURSEL to receive the identifier of the selected item.

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_COMMAND:
            switch(LOWORD(wParam)) {
                case IDC_COMBO:
                    if (HIWORD(wParam) == CBN_SELCHANGE) {
                        HWND hCtl = GetDlgItem(hWnd, IDC_COMBO);//Get handle for HMENU item
                        if (SendMessage(hCtl, CB_GETCURSEL, 0, 0) == compareValue) {
                            //...
                        }
                    }
                    break;
            }
            break;
        //...
    }
}


Assuming that you are using Windows, you can use the following messages:

WM_GETTEXTLENGTH and WM_GETTEXT.

Firstly, get the length of the selected text, then allocate your buffer to ensure it's large enough, then retrieve the actual text. Easy.

Example:

const UINT length = ::SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
LPTSTR pszText = new TCHAR[length + 1];
::SendMessage(hWnd, WM_GETTEXT, length + 1, pszText);

// pszText will now contain the text you want, do what you want with it

delete[] pszText; // Remember to delete else you will leak.


I never work with c++ with winapplication but i tried it with the c# and hopefully that you want the desired output as i got through your question if it is not right then you should edit your question.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        if (comboBox1.Text == "ADD")  
        {  
            int a = 12, b = 13, c;  
            c = a + b;  
            MessageBox.Show("Result of adding= " + c);  
        }  
        else if (comboBox1.Text == "Multiple")  
        {  
             int x = 3, y = 5, z;  
            z = x * y;  
            MessageBox.Show("Result of multiplication= " + z);  
        }  
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消