GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Oct-2004, 09:43
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road

change the caption on button when clicked


Hello,

I have made a rudimentary GUI with MFC. I have a button labeled "start". when this button is clicked, i want to change the label to "stop" and also have it do somethign different when the stop button is clicked (i.e. different from when the "start" button is clicked- although they are the same button). here is some of the relevant code:

CPP / C++ / C Code:
#include "sockGUI.h"
//#include <iostream.h>
//#include <fstream.h>
#include <windows.h>
#define IDC_FIRST_BUTTON 111

// The InitInstance function is called each
// time the application first executes. (Similar to the way openGL works)
char * startButtonLabel="Start";

BOOL CsockGUI::InitInstance()
{
	char * a="hello";
	m_pMainWnd = new CGUIWindow(a);
	m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}

BEGIN_MESSAGE_MAP(CGUIWindow, CFrameWnd)
ON_BN_CLICKED(666, HandleStartButton)
END_MESSAGE_MAP()

// The constructor for the window class
CGUIWindow::CGUIWindow(char *a)
{
	//variables to hold height and length of the window
	int height=300;
	int length=300;


	//variables so that all the coordinates don't have to be changed all the time!
	//if the variable name is prefixed with an 's' it is that dimension for the status
	//box, if it is prefixed with a 'c' it is that dimension for the channel.
	int sTop=10;
	//int sHeight=70;
	int sLeft=10;
	int sWidth=265;
	int sBottom=80;

	int incSC=30;
	int incCC=5;

	int cHeight=15;
	int cLeft=50;
	int cWidth=100;


	//these variables are initialized and declared here. they are used
	// as coordinates for the channel radio buttons and are incremented appropriately.
	//To move the buttons, please change only the above absolute values that I have entered.
	//The radio buttons will move around as a group if only those values are changed.
	int cTop=sBottom+incSC;
	int cBottom=cTop+cHeight;

	// Create the window itself
	Create(NULL,
	"GUI",
	WS_OVERLAPPEDWINDOW,
	CRect(0,0,length,height));

	// Create a static label

	cs = new CStatic();
	cs->Create(a,
	WS_CHILD|WS_VISIBLE|SS_CENTER,
	CRect(sLeft,sTop,sLeft + sWidth,sBottom), this);


	//two step creation process (for each control)
	one=new CButton();
	one->Create("Channel 1", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(cLeft,cTop,cLeft+cWidth,cBottom),
	this, IDC_FIRST_BUTTON);

	//increment the variables appropriately.
	cTop=cBottom+incCC;
	cBottom=cTop + cHeight;

	two=new CButton();
	two->Create("Channel 2", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON+1);

	cTop=cBottom+incCC;
	cBottom=cTop + cHeight;

	three=new CButton();
	three->Create("Channel 3", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON+2);

	cTop=cBottom+incCC;
	cBottom=cTop + cHeight;

	four=new CButton();
	four->Create("Channel 4", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON+3);


	cTop=cBottom+incCC;
	cBottom=cTop + cHeight;

	five=new CButton();
	five->Create("Channel 5", BS_AUTORADIOBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(cLeft, cTop, cLeft + cWidth, cBottom), this, IDC_FIRST_BUTTON+4);


	//now we deal with the start button and use some of the variables declared
	//for the channel radio buttons. The left and right coordinates for this
	//are absolute numbers and may be modified here.
	cTop=cBottom+ (3*incCC);
	cBottom=cTop + (3*cHeight);


	start=new CButton();
	start->Create(startButtonLabel, BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE,
	CRect(30, cTop, 250, cBottom), this, 666);



}

//The following function can be used to handle the event of the start button
//being clicked. At the moment, it just creates an empty text file to see if
//if it works.

void CGUIWindow::HandleStartButton(){
	startButtonLabel="Stop";

	int radioState;
	radioState = getChannel();
	//char * temp=new char [12];
	//temp=itoa(radioState,temp, 10);
	//MessageBox(temp);
	Client theClient;
	theClient.TCPinitial(radioState);
	MessageBox("Before calling datagram client.");
	//cout<<"Before calling datagram client"<<endl;
	theClient.DatagramClient("localhost");
}	

//the following function returns an integer value for the
//channel selected.
int CGUIWindow::getChannel(){

	if (one->GetCheck()==1) return 1;
	if (two->GetCheck()==1) return 2;
	if (three->GetCheck()==1) return 3;
	if (four->GetCheck()==1) return 4;
	if (five->GetCheck()==1) return 5;
	MessageBox("You must select a channel.");
	return -1;
}

thanks!
ps. i read some tutorials on the web on mfc. i'm pretty new to it.. they all seem to be quite basic and say the same thing. do you know of any tutorials on message maps that list all of the things you can do with them? with the relevant functions. for example, there are functions to know when the left button on the mouse has been clicked. it would be nice to be able to read up on this! thanks.
Last edited by LuciWiz : 15-Oct-2004 at 09:53. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 15-Oct-2004, 10:38
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Ok, here's what you can do: first, create a member data of the dialog class

CPP / C++ / C Code:
	unsigned int onStart;

Then, initialize it, as well as your button's text (I suppose you did create a button and mapped a variable on it - I'll call it m_button)

CPP / C++ / C Code:
//	In your OnInitDialog	
	onStart = 1;
	m_button.SetWindowText("Start");

Now, lets implement the OnClick event for your button (just double-click it, or use ClassView - it's your choice really):

CPP / C++ / C Code:
void CTestingDlg::OnButton() 
{
	if ( ! onStart )
	{ 
		m_button.SetWindowText("Start");
	}
	else
	{
		m_button.SetWindowText("Stop");
	}

	onStart ^=  1; // make onStart 0, or 1 respectively
	
}

If you want to check whether your button is set on "start" or "stop" at any point in your application, just check onStart - a value of 0 means it is "stoped", a value of 1 the other thing

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 15-Oct-2004, 11:04
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
Luci, I think i owe you a drink! thanks a lot for taking the time to help me out.

I am still having some problems tho. Here is the code where i create teh button:
CPP / C++ / C Code:
	  start=new CButton();
	  start->Create(startButtonLabel, BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE, 
		  CRect(30, cTop, 250, cBottom), this, 666); 
//	start.SetWindowText("Start");


start.SetWindowText("Start") is a line you wanted me to add in. Its not compiling. It says "error C2228: left of '.SetWindowTextA' must have class/struct/union type". I put the line in the function "CGUIWindow::CGUIWindow(char *a)". is that where you wanted me to put it?
By the way, startButtonLabel is a global variable i created to test things. but, i can replace it with "start" and it displays start. you probably already knew that! lol.. but i thought i would just mention it.
thanks again.
  #4  
Old 18-Oct-2004, 01:30
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by drewdaman
...
CPP / C++ / C Code:
//	start.SetWindowText("Start");


start.SetWindowText("Start") is a line you wanted me to add in. Its not compiling.

I apologize for this. It is really a stupid mistake. I have NO idea how this slipped to me. I tested the code I gave you except for this line (I knew what it does , and I just added it without thinking to much, obviously).
start is a pointer to a button, so when you call a method of the respective object you need something like this:

CPP / C++ / C Code:
(*start).SetWindowText("Start");
// OR   start->SetWindowText("Start"); Recommended!

dsmith warned someone about making this mistake here, and I thought to myself: "Who would make this mistake?"; well, here I am

Sorry again.

Quote:
Originally Posted by drewdaman
By the way, startButtonLabel is a global variable i created to test things. but, i can replace it with "start" and it displays start. you probably already knew that! lol.. but i thought i would just mention it.

Yup, figured it out

Kind regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 20-Oct-2004, 09:46
drewdaman drewdaman is offline
New Member
 
Join Date: Sep 2004
Posts: 14
drewdaman is on a distinguished road
Hi there,

Sorry, i was away from work for a few days.

That worked just great! thanks a ton!

Drew.

PS. I don't know how that got by me either! i was using -> in the line right above it!
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Styling the browse button... erniegerdie Web Design Forum 23 19-Jan-2006 23:25
changing caption of static text control Pandiani MS Visual C++ / MFC Forum 1 20-Sep-2004 02:38
change statement jilshi Web Design Forum 0 26-Apr-2004 23:40
Article by the W3C - Cool URI's don't change jrobbio Web Design Forum 4 28-Apr-2003 07:40
using a button to change table contents. demtro MySQL / PHP Forum 20 02-Mar-2003 16:07

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.