常乐居

常乐居

8:类型转换和数据转换

学了一些基础的东东总想搞出点小东东出来玩一下,先学着做个计算器..不过碰到的问题可多了..

先是第一个问题;这是VB.NET里的代码.如果想多个控件同一个事件的话就一直在Handles后加就行了..


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    
End Sub


 可以到了C++


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


 这下可好  Handles ..XX??在那里啊......

找了老半天...总算..


this->button1->Click += gcnew System::EventHandler(this&Form1::button1_Click);


 。。。感觉像委拖给这个过程一样..只是猜测的不知道C++的委拖是不是这样的呵~~

那乘下的其它按扭...就这样加吧...

结下来开始写按钮0-9的事件吧...先把上面的buttonon1_Click事件加到0-9的按钮Click事件里去;

接下来....VB.NET里转换


  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        
MsgBox(CType(sender, Button).Text) ''VB里的类型转换..
    End Sub


 这里注意VB.NET 里的Ctype可以转换数据类型.也可以转换控件类型但是


    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
                 
this->textBox1->Text = this->textBox1->Text + safe_cast<System::Windows::Forms::Button^>(sender)->Text->ToString();
             }


 但C++里控制转换..safe_cast<>() 这转换的可真多啊...还有  (类型)对像 不知道什么情况下用了先做了先...

把字符型转成数值型...可搞苦我了。。。上面的类型转换都不行....结果在看到C#的一篇文单后...试了一下结果...


private: System::Void button11_Click(System::Object^  sender, System::EventArgs^  e) {
             KB 
=KB+System::Decimal::Parse(this->textBox1->Text->ToString());
             
this->textBox1->Clear();
             }


 还真得行了...

 解决了这二个问题接下来就开始做计算器了..


 

posted on 2009-03-23 12:00  常乐  阅读(130)  评论(0编辑  收藏  举报

导航