plutomed Posted July 18, 2009 Share Posted July 18, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/ Share on other sites More sharing options...
Bendude14 Posted July 19, 2009 Share Posted July 19, 2009 why cast it to a control? can you not just use sender.Name? Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-877848 Share on other sites More sharing options...
plutomed Posted July 19, 2009 Author Share Posted July 19, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-877892 Share on other sites More sharing options...
KevinM1 Posted July 19, 2009 Share Posted July 19, 2009 Have you tried casting it as a button? Like: Button ctrl = (Button)sender; I do that in C#, and it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878195 Share on other sites More sharing options...
plutomed Posted July 20, 2009 Author Share Posted July 20, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878548 Share on other sites More sharing options...
KevinM1 Posted July 20, 2009 Share Posted July 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878549 Share on other sites More sharing options...
plutomed Posted July 20, 2009 Author Share Posted July 20, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878551 Share on other sites More sharing options...
KevinM1 Posted July 20, 2009 Share Posted July 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878612 Share on other sites More sharing options...
KevinM1 Posted July 20, 2009 Share Posted July 20, 2009 Hey, I found this article which may help: http://msdn.microsoft.com/en-us/library/yk97tc08.aspx Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-878641 Share on other sites More sharing options...
Ninjakreborn Posted July 20, 2009 Share Posted July 20, 2009 If that doesn't work, try CodeBlocks and Ming. That seems to be a good combination for me. I used VS2008 for awhile but it just wasn't working for me due to various reasons. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879050 Share on other sites More sharing options...
plutomed Posted July 20, 2009 Author Share Posted July 20, 2009 Hey, I found this article which may help: http://msdn.microsoft.com/en-us/library/yk97tc08.aspx If the syntax worked the way it was, why change it. :/ And I tried changing it to a button^ and it said that is wasn't in the right format for System::Void. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879093 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 With more digging, I believe I've found the solution: Button^ ctrl = safe_cast<Button^>(sender); Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879133 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 Yeaaaaaa Thanks so much Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879333 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879384 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 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 To be honest, I'm not sure. My C++ is very rusty. Are you certain you've included the header file? Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879396 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 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' Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879426 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 It looks like you may be dealing with another stack handle thingie. Are you trying to convert from string to int, or string^ to int? Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879449 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 Yea...Its a string^ (string pointer I think). Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879450 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 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^. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879457 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879467 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879490 Share on other sites More sharing options...
KevinM1 Posted July 21, 2009 Share Posted July 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879508 Share on other sites More sharing options...
plutomed Posted July 21, 2009 Author Share Posted July 21, 2009 Thanx so much for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/166440-solved-c-sender/#findComment-879703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.