Jump to content

[SOLVED] C++ sender


Recommended Posts

I have a function for a press of a button on a form.

 

private: System::Void GridControl(System::Object^ sender, System::EventArgs^ e) {

			  //String^ Details = sender->ToString()->Substring(34);

			  Control ctrl = (Control)sender;
			  
			  MessageBox::Show(ctrl.Name, "",MessageBoxButtons::OK);
		  }

 

When I try to compile the program it wont let me. I get the error:

 

error C2440: 'type cast' : cannot convert from 'System::Object ^' to 'System::Windows::Forms::Control'

 

All I'm trying to do is get the name of the button that called the function.

 

I found this way of doing it on several places on Google, I dunno if it is correct or not but my compiler doesn't like it.

Link to comment
Share on other sites

No. When I try sender.Name I get the error C2228 Left of '.Name' must have a class/struct/union.

Also When I try sender->Name I get error C2039 Name is not a member of System::Object

 

When I echo out what sender is: sender->ToString(); I get: System.Windows.Forms.Button, Text:<Text of button>

Link to comment
Share on other sites

I think its just VS2008 being an arse!

 

Tried it like that and still didn't work.

 

error C2440: 'type cast' : cannot convert from 'System::Object ^' to 'System::Windows::Forms::Button'

 

Hmm...what does the '^' denote?  I'm not familiar with that marker/operator.

Link to comment
Share on other sites

I don't really know...

 

But if I don't have it there I get the error:

 

error C3352: 'void Battleshipssssssss::Form1::GridControl(System::Object,System::EventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)'

 

Also I have always used "element.property", now I'm having to use "element->property". Do you know the difference?

Link to comment
Share on other sites

I don't really know...

 

But if I don't have it there I get the error:

 

error C3352: 'void Battleshipssssssss::Form1::GridControl(System::Object,System::EventArgs ^)' : the specified function does not match the delegate type 'void (System::Object ^,System::EventArgs ^)'

 

Also I have always used "element.property", now I'm having to use "element->property". Do you know the difference?

 

Well, for the first problem, I'd try keeping the method signature the same as what it was, but cast the sender as a Button^ (whatever that's supposed to mean).  The worst that can happen is another compiler error.

 

For the second, the -> operator is used to dereference a pointer.  The element variable is a pointer of some type, and you're dereferencing it to get a hold of its property.

 

I wonder if the '^' is a Microsoft/.NET/Visual Studio thing to represent a pointer.  Usually it's denoted by a '*', like say int* num, but the other operator looks to be used in a similar fashion here.

Link to comment
Share on other sites

Ok, I'm having another little problem...

 

I'm getting the error c2065 GameBoardP2 undeclared identifier. It blatently is identified as when I hover over that line in the code it pops up saying char GameBoardP2[9][9].

 

GameBoardP2 is declared in a header file.

 

Could you give me any ideas  :confused:

Link to comment
Share on other sites

Ok, I'm having another little problem...

 

I'm getting the error c2065 GameBoardP2 undeclared identifier. It blatently is identified as when I hover over that line in the code it pops up saying char GameBoardP2[9][9].

 

GameBoardP2 is declared in a header file.

 

Could you give me any ideas  :confused:

 

To be honest, I'm not sure.  My C++ is very rusty.  Are you certain you've included the header file?

Link to comment
Share on other sites

Ok, I re-compiled it and it worked. :-[

 

I'm now having a problem converting from a string to a int.

 

I'm not having a good day lol.

 

C2664: 'std::basic_istringstream<_Elem,_Traits,_Alloc>::basic_istringstream(std::ios_base::openmode)' : cannot convert parameter 1 from 'System::String ^' to 'std::ios_base::openmode'

Link to comment
Share on other sites

Yea...Its a string^ (string pointer I think).

 

It's sort of a pointer.  It's a handle to an object (this time, a string) on the managed (read: .NET) heap.  Try using the same safe_cast thing I showed you before to turn it into an int^.

Link to comment
Share on other sites

I just thought you basically don't know what I'm trying to do.

 

I have a form with a load of buttons. When I click one I want it to update an array at the point of the name of the button. Each button has the name eg.12.

 

The array looks like this:

 

char GameBoardP1[9][9] = {

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0}

};

 

So if I press the button named 12, I want it to go to the array at GameBoardP1[1][2].

 

The way I have doe it so far is:

private: System::Void GridControl(System::Object^ sender, System::EventArgs^ e) {

			  Button^ ctrl = safe_cast<Button^>(sender);
			  String^ Details = ctrl->Name->ToString();

			  MessageBox::Show(Details[2],"",MessageBoxButtons::OK); //I'm trying to get Details[2] to a const char*

			  /*
			  switch(GameBoardP2[strtol(Details[2], NULL, 10)][strtol(Details[3], NULL, 10)])
			  {
			  case 66:
				  MessageBox::Show("Hit", "Response:", MessageBoxButtons::OK);
				  ctrl->Text = "Hit";
				  lblInfo->AppendText("Player 1 just hit a ship.\n");
				  lblInfo->ScrollToCaret();
				  break;

			  default:
				  MessageBox::Show("Miss", "Response:", MessageBoxButtons::OK);
				  ctrl->Text = "Miss";
				  lblInfo->AppendText("Player 1 just missed.\n");
				  lblInfo->ScrollToCaret();
				  break;
			  }
			  */

			  ctrl->Enabled = false;
		  }		  }

Link to comment
Share on other sites

I tried that and nope, didn't want to do it.

 

Can you tell me what an l-value is?

 

Gawd I have a lot to learn lol. All languages are so different. :(

 

*edit*

 

Is it a Left hand side value?

 

Yup, an l-value is a left hand value.  It generally means that it's something that can be assigned to, but not always (const variables are considered l-values).  l-values can be used as r-values, but not vice versa.

 

More info: http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05lvalue.htm

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.