Calling a windows form function

Calling a function defined by another button

Page 1 of 1

5 Replies - 5913 Views - Last Post: 01 June 2010 - 04:23 AM Rate Topic: -----

#1 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Calling a windows form function

Posted 31 May 2010 - 02:44 PM

Hello,

I am wandering if someone could help me to figure out how to invoke a function controlled by one Windows Form button inside another function triggered by another button:

Example:
When a user presses "Button 1" (cmdequals), the code normally triggered by "Button 2" (cmdclearall) is invoked. Both code snippets are listed below.

public void cmdclearall_Click(object sender, EventArgs e)

        {
             
        if (txtInput.Text.Length != 0 && valHolder1 != 0)
            {
                //if valHolder1 is not 0, then invoke CalculateTotals method.

               CalculateTotals();  //Call the calculate totals method  

                //clear calcFunc
             }
            calcFunc = string.Empty;
            //Reset the decimal flag 
            hasDecimal = false;
         }

public void cmdclearall_Click(object sender, EventArgs e) 
        {
            //Empty the text in the input box 
            txtInput.Text = string.Empty;
            //Clear out both temp values 
            valHolder1 = 0;
            valHolder2 = 0;
            //Set the calc switch to empty 
            calcFunc = string.Empty;
            //Toggle the hasDecimal flag 
            hasDecimal = false;

        }

This post has been edited by JackOfAllTrades: 31 May 2010 - 03:12 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Calling a windows form function

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Calling a windows form function

Posted 31 May 2010 - 02:59 PM

Is that what you mean?

public void cmdclearall_Click(object sender, EventArgs e)
{

    if (txtInput.Text.Length != 0 && valHolder1 != 0)
    {
        //if valHolder1 is not 0, then invoke CalculateTotals method.

        CalculateTotals(); //Call the calculate totals method

        //clear calcFunc
    }

    ClearAll();
}

private void ClearAll()
{
    txtInput.Text = string.Empty;
    valHolder1 = 0;
    valHolder2 = 0;
    calcFunc = string.Empty;
    hasDecimal = false;
}

public void cmdclearall_Click(object sender, EventArgs e)
{
    ClearAll();

} 




Next time, please put your code inside code tags.

This post has been edited by FlashM: 31 May 2010 - 02:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Calling a windows form function

Posted 31 May 2010 - 04:48 PM

public void cmdclearall_Click(object sender, EventArgs e)
{
    if (txtInput.Text.Length != 0 && valHolder1 != 0)
    {
        //if valHolder1 is not 0, then invoke CalculateTotals method.
        CalculateTotals();  //Call the calculate totals method  
    }
    
    yourNewButton_Click();
    calcFunc = string.Empty;
    //Reset the decimal flag 
    hasDecimal = false;
}

public void yourNewButton_Click(object sender, EventArgs e)
{
    MessageBox.Show("This is your second button method!");
}


This should work, although I haven't tested it. It might give you an exception because the _Click() event requires two additional parameters. I'm not at home to test it.
Was This Post Helpful? 1
  • +
  • -

#4 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Re: Calling a windows form function

Posted 31 May 2010 - 06:01 PM

Thanks for the reply! What I'm asking is simply, how does one call a method / piece of code, which is normally triggered by a given button (in this case cmdclearall or "Clear All" button) inside the method normally triggered by another button (cmdequal or "Equal" button)?

public void cmdequal_Click(object sender, EventArgs e)

//now call the code inside public void cmdclearall_Click(object sender, EventArgs e) <does not matter what the code inside it is i.e. entire code normally triggered by "Clear All" button?


Was This Post Helpful? 0
  • +
  • -

#5 andtherus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-May 10

Re: Calling a windows form function

Posted 31 May 2010 - 07:00 PM

I am wandering if someone can help to point out my error. The error is, when user presses the "Equals button" =event:
private void cmdequals_Click(object sender, EventArgs e)


Nothing happens (goal: to display output of the CalculateTotals() function). However, if I first click the following button "Clear All",
private void cmdclearall_Click(object sender, EventArgs e) 


followed by any operation (addition for example), the calculator functions properly! Below I'm giving the select code, including definition of all variables, addition, equals, clearall and CalculateTotals() methods).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Calculator : Form
    {
        public Calculator()
        {
            InitializeComponent();
        }

        private double valHolder1;
        private double valHolder2;
        //Varible to hold temporary values 
        private double tmpValue;
        //True if "." is use else false 
        private bool hasDecimal = false;
        private bool inputStatus = true;
        //variable to hold Operater 
        private string calcFunc;

private void cmdadd_Click(object sender, EventArgs e)
        {
            //Make sure out input box has a value 
            if (txtInput.Text.Length != 0)
            {
                //Check the value of our function flag 
                if (calcFunc == string.Empty)
                {
                    //Flag is empty 
                    //Assign the value in our input box to our holder 
                    valHolder1 = System.Double.Parse(txtInput.Text);
                    //Empty the input box 
                    txtInput.Text = string.Empty;
                }
                else
                {
                    //Flag isnt empty 
                    //Call our calculate totals method 
                    CalculateTotals();
                }
                //Assign a value to our calc function flag 
                calcFunc = "Add";
                //Toggle the decimal flag 
                hasDecimal = false;
            }

        }

private void cmdclearall_Click(object sender, EventArgs e) 
        {
            //Empty the text in the input box 
            txtInput.Text = string.Empty;
            //Clear out both temp values 
            valHolder1 = 0;
            valHolder2 = 0;
            //Set the calc switch to empty 
            calcFunc = string.Empty;
            //Toggle the hasDecimal flag 
            hasDecimal = false;

        }

 private void cmdequals_Click(object sender, EventArgs e) 
        {
            if (txtInput.Text.Length != 0 && valHolder1 != 0)
            {
                
                CalculateTotals();  //Call the calculate totals method  
                           
            }
            
            //Reset the decimal flag  and calcFunc
            calcFunc = string.Empty;
            hasDecimal = false;
        }

 private void CalculateTotals()
        {
           valHolder2 = System.Double.Parse(txtInput.Text); //valHolder2 is set to the current value of the input box (value 2!)
            //determine which calculation we're going to execute 
            //by checking the value of calcFunc 
            switch (calcFunc)
            {
                //addition 
                case "Add":
                    valHolder1 = valHolder1 + valHolder2;
                    break;
                //subtraction 
                case "Subtract":
                    valHolder1 = valHolder1 - valHolder2;
                    break;
                //division 
                case "Divide":
                    valHolder1 = valHolder1 / valHolder2;
                    break;
                //multiplication 
                case "Multiply":
                    valHolder1 = valHolder1 * valHolder2;
                    break;
                //exponents (power of) 
                case "PowerOf":
                    valHolder1 = System.Math.Pow(valHolder1, valHolder2);
                    break;
            }
            txtInput.Text = valHolder1.ToString();
            inputStatus = false;
        }


Was This Post Helpful? 0
  • +
  • -

#6 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Calling a windows form function

Posted 01 June 2010 - 04:23 AM

You can click a button programmatically, or you can call the function and pass it null / empty arguments:

// Better way - click the butto programmatically
btnMyButton.PerformClick();

// Worse way - call the function which handles the button's click events
// If any code in this function relies on the arguments (object sender. EvenArgs e) 
// then this might cause errors or unexpected behaviour
btnMyButton_Click(null, EventArgs.Empty);



But it's for this reason that most people will recommend not putting logic inside of event handlers, because it then gets messy if you want to use the logic without the event ocurring. It's better to put all your logic in its own function, then in the event handler just call that function. Then you can also call the function easily from the rest of your code. E.g:

void Handler_btnMyButton_Click()
{
    // This is not attached to any event, it's a stand-alone function
    // Put your logic here
}

void btnMyButton_Click()
{
    // This is the actual event handler for btnMyButton.Click()
    Handler_btnMyButton_Click();
}

void SomeOtherMethod()
{
    // Now you can call your logic from anywhere
    Handler_btnMyButton_Click();
}



I think this is what you're meaning?

This post has been edited by Charles:): 01 June 2010 - 04:27 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1