hello all

in vb we can use controll array to make the calc

so with in single block we can write our entire code

Is there any way to make calc in c# like vb's controll array ??

pls provide ur suggetion

Recommended Answers

All 6 Replies

What do you mean with control array?
An array of controls? Or something like a TableLayoutPanel?
Perhaps you could show us some code of what you relly mean?

commented: very helpful ppl +1

There is a control array in VB 6.0. In C# or VB.NET you may use single event handler for multiple buttons click.

private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(commonEvent);
            button2.Click += new EventHandler(commonEvent);
            ..
        }

        void commonEvent(object sender, EventArgs e)
        {
            Button b=(Button)sender;
            ...
        }

@adatapost

thanx a lot for ur help

i have try this code & it's really work

my other query is,

how can ditinguish that event is fired by bt1,bt2...

because i wanna make calculatore pro.

so if bt1 is pressed then in textbox 1 should be displayed

& if bt2 is pressed then in textbox 2 should be displayed

& so on ....

There is a control array in VB 6.0. In C# or VB.NET you may use single event handler for multiple buttons click.

private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(commonEvent);
            button2.Click += new EventHandler(commonEvent);
            ..
        }

        void commonEvent(object sender, EventArgs e)
        {
            Button b=(Button)sender;
            ...
        }
private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(commonEvent);
            button2.Click += new EventHandler(commonEvent);
            ..
        }

        void commonEvent(object sender, EventArgs e)
        {
             Button b=(Button)sender;
             if(b==Button1) {
                        ......
             }
     }

You may also compare Text property of buttons

void commonEvent(object sender, EventArgs e)
        {
             Button b=(Button)sender;
             if(b.Text=="1") {
                        ......
             }
            else
             if(b.Text=="2") {
                        ......
             }

As adatapost already pointed out how to do it, this is an extra from a real calculator example. I used a switch statement to find out which memory button I had clicked.

private void MemoryBtn_Click(object sender, EventArgs e)
        {
            if (_CalculatorON)
            {
                Button MemoryBtn = sender as Button;
                string DisplayText = string.Empty;

                switch (MemoryBtn.Text)
                {
                    case "MC": 
                        _MEMORY = 0.0;
                        this.MemLbl.Hide();
                        this.MemoryTxb.Hide();
                        break;
                    case "MR": 
                        _NUMBER = _MEMORY;
                        this.DisplayTxB.Text = AdjustDisplayString(_NUMBER.ToString());
                        break;
                    case "M+":
                        _MEMORY += _ACCUMULATOR == 0 ? _NUMBER : _ACCUMULATOR;
                        this.MemLbl.Show();
                        this.MemoryTxb.Text = AdjustDisplayString(_MEMORY.ToString());
                        this.MemoryTxb.Show();
                        _NewNumber = true;
                        _DecimalPnt = false;
                        break;
                    case "M-":
                        _MEMORY -= _ACCUMULATOR == 0 ? _NUMBER : _ACCUMULATOR; ;
                        this.MemLbl.Show();
                        this.MemoryTxb.Text = AdjustDisplayString(_MEMORY.ToString());
                        this.MemoryTxb.Show();
                        _NewNumber = true;
                        _DecimalPnt = false;
                        break;
                    default:
                        break;
                }
            }
        } //END MemoryBtn_Click
commented: Excellent +10

@adatapost
@ ddanbe

thanx u so much ... :)

As adatapost already pointed out how to do it, this is an extra from a real calculator example. I used a switch statement to find out which memory button I had clicked.

private void MemoryBtn_Click(object sender, EventArgs e)
        {
            if (_CalculatorON)
            {
                Button MemoryBtn = sender as Button;
                string DisplayText = string.Empty;

                switch (MemoryBtn.Text)
                {
                    case "MC": 
                        _MEMORY = 0.0;
                        this.MemLbl.Hide();
                        this.MemoryTxb.Hide();
                        break;
                    case "MR": 
                        _NUMBER = _MEMORY;
                        this.DisplayTxB.Text = AdjustDisplayString(_NUMBER.ToString());
                        break;
                    case "M+":
                        _MEMORY += _ACCUMULATOR == 0 ? _NUMBER : _ACCUMULATOR;
                        this.MemLbl.Show();
                        this.MemoryTxb.Text = AdjustDisplayString(_MEMORY.ToString());
                        this.MemoryTxb.Show();
                        _NewNumber = true;
                        _DecimalPnt = false;
                        break;
                    case "M-":
                        _MEMORY -= _ACCUMULATOR == 0 ? _NUMBER : _ACCUMULATOR; ;
                        this.MemLbl.Show();
                        this.MemoryTxb.Text = AdjustDisplayString(_MEMORY.ToString());
                        this.MemoryTxb.Show();
                        _NewNumber = true;
                        _DecimalPnt = false;
                        break;
                    default:
                        break;
                }
            }
        } //END MemoryBtn_Click
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.