vb to C++

Started by
2 comments, last by traxxas25 20 years, 4 months ago
hey i just moved over from visual basic too c++ and im making a little prog to see what i can do, in a portion of my progr i want the user to enter the word "press" and press the button next to it and then the textbox would say "pressed" in vb the code would be: If text1.text = "press" then text1.text = "pressed" now, in C++ i put in this code: if(textBox5->Text == "press") { textBox5->set_Text("pressed") } but that doesnt work, it compiles but doesnt work in the program, please help if possibal, thanks guys
Advertisement
quote:Original post by traxxas25
hey i just moved over from visual basic too c++ and im making a little prog to see what i can do, in a portion of my progr i want the user to enter the word "press" and press the button next to it and then the textbox would say "pressed"
in vb the code would be:

If text1.text = "press" then text1.text = "pressed"

now, in C++ i put in this code:

if(textBox5->Text == "press")
{
textBox5->set_Text("pressed")
}

but that doesnt work, it compiles but doesnt work in the program, please help if possibal, thanks guys


Hokai. Visual Basic is designed to work with graphical user interfaces right from the start. Text boxes and the like. That sort of thing is inbuilt right into the language. In C++, it is NOT. You have to do everything yourself. That means actually getting GUIs to work yourself. Or, more accurately, make use of Microsoft's existing GUI code. But, basically, GUIs are a huge pain in the arse in C++.

Second thing: in VB, strings are primitive data types, just like numbers. As in you can just use them and compare them as you need. This is not the case in C++. That means that string1 == string2 will not have the desired effect, and you'll have to use alternate means. Any good C\C++ book will explain why this won't work, what this will actually do, and what you'll have to do to get what you want (see point three).

Third thing: C++ is NOT VB. It could not be more different from VB. There is no possible way you can quickly learn the syntax of C++ and expect VB things to work in it. I strongly suggest you get yourself a good C++ book (suggestions can be found on tihs website) and start from scratch. You will notice similarities with VB which will help you learn the language, but don't start with any assumptions about what C++ can or can't do.

[edited by - SoulSkorpion on December 10, 2003 12:59:03 AM]
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
1. to compare strings use strcmp defined in string.h, you cant just use == to compare strings. (strings are type char* (usually), so you would be comparing an address to a literal string)

2. for forms you have 2 basic options (there''s more but if you are new you shouldnt go into them yet). use the standard win32 functions, or use the mfc.

of course create a resource script and add it to your project, then create a dialog with IDD_MAINAPP for it''s id. Keep the standard ok and cancel buttons it makes for you, then create one more button, set the text to whatever you want, but set the id to IDC_BUTTON1. Then create a input box with ID IDC_TEXTBOX1 . you should be able to find your way from here.

#include <windows.h>#include <string.h>#include "resource.h"void OnButton1Down(HWND hDlgWnd);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){	DialogBoxParam(hInstance,(LPCTSTR)IDD_MAINAPP,NULL, (DLGPROC)DlgProc,  0);} LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	switch (message)	{		case WM_INITDIALOG:			OnInitDialog(hDlg,courses,&currentID);			return TRUE;		case WM_COMMAND:			switch(LOWORD(wParam))			{			case IDC_BUTTON1:				OnButton1Down(hDlg);				break;			case IDOK:			case IDCANCEL:				if(OnExit(hDlg,courses,&currentID)==TRUE)				{					EndDialog(hDlg, LOWORD(wParam));					return TRUE;				}				break;			}			break;	}    return FALSE;}void OnButton1Down(HWND hDlgWnd){	char string1[80]={0};	GetDlgItemText(hDlgWnd,IDC_TEXTBOX1,string1,80);	if(strcmp(string1,"Press")==0)	{		SetDlgItemText(hDlgWnd,IDC_TEXTBOX1,"Pressed");	}}
acctualy no C++ standard libraries ends with a .h so you should include string without the .h.



[edited by - kappa on December 11, 2003 4:48:25 AM]

This topic is closed to new replies.

Advertisement