Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When a slider moves (trackbar1_scroll method) I want another method to be called that will update all the values in text boxes. Currently I have a button click method which calls another calculate method. Ideally I would like this to be called if possible?

public void btnCalculate_Click(object sender, EventArgs e)
{

    double.TryParse(txtMass.Text, out mass);//mass in kg
    double.TryParse(txtHeight.Text, out height);//height in meters
    double.TryParse(txtGravity.Text, out gravity);//gravity 9.81 default


    Calculate(mass, height, gravity);
    string answer = Convert.ToString(potentialEnergy);
    txtAns.Text = answer;



}



private void Incline_Load(object sender, EventArgs e)
{
    ToolTip Tooltip1 = new ToolTip();
    Tooltip1.SetToolTip(this.btnCalculate, "Gives the solution");

}

private void trackBar1_Scroll(object sender, EventArgs e)
{
    txtHeight.Text=trackBar1.Value.ToString();
    //MessageBox.Show(trackBar1.Value.ToString())

    trackBar1.Enabled = btnCalculate_Click();

}


What I have tried:

trackBar1.Enabled = btnCalculate_Click();
Posted
Updated 28-Jul-21 6:17am

Move the code out of the Click handler into a separate method, than call that from both your Click and Scroll handlers.
 
Share this answer
 
As already pointed out in solution 1, simply move the relevant portions of the code to another method and call that.

Few other observations based on the snippet you posted
- You correctly use TryParse to convert the values from text boxes. However, you don't investigate if the parsing fails. TryParse returns a boolean so you should check if it is true or false and act based on that
- It looks like you have a class level variable (potentialEnergy) that you use to transfer data from the method (Calculate). If that is correct, it's considered as a bad habit. Instead, define the Calculate method so that it returns the answer as a return value

Pseudo example
==============
C#
public void btnCalculate_Click(object sender, EventArgs e) {
    DoCalculation();
}

private void trackBar1_Scroll(object sender, EventArgs e)
{
    txtHeight.Text=trackBar1.Value;
    DoCalculation();
}

public void DoCalculation() {
    double mass;
    double height;
    double gravity;
    bool ok = true;

    ok &= double.TryParse(txtMass.Text, out mass);//mass in kg
    ok &= double.TryParse(txtHeight.Text, out height);//height in meters
    ok &= double.TryParse(txtGravity.Text, out gravity);//gravity 9.81 default

    if (!ok) {
       // some error message goes here
       return;
    }
    txtAns.Text = Calculate(mass, height, gravity);
}

public double Calculate(double mass, double height, double gravity) {
   // do the calculation
   return result;
}
 
Share this answer
 
v2
Comments
Paul Carson 2021 28-Jul-21 10:53am    
I've moved "trackBar1.Enabled = btnCalculate_Click();" into a method called Updateall and called that from the trackbar1_scroll method.

I'm getting the message that there is no argument given that corresponds to the required formal parameter 'sender' of incline.btn....
Wendelius 28-Jul-21 12:32pm    
That doesn't look what you would need Consider the pseudo example added to the solution
Paul Carson 2021 29-Jul-21 3:45am    
Thanks a lot for the example
Wendelius 29-Jul-21 9:44am    
You're welcome :)
Interesting that you chose the Scroll event, rather than the ValueChanged event; but, they both do the same thing ... Scroll will be called first if you handle both events (but, why would you ?). Why MS defined two identical events; who knows :)

One suggestion; if you call your calculation functions continuously ... every time the slider moves ... you are going to get a lot of calls which may bot be what the user wanted, and may lead to refresh issues.

Consider calling your calculation code when the Leave event of the trackbar occurs ? Note that just clicking on the Form will not trigger the Leave event: you have to select some other Control.

Sample code:
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
    // deliberately not implemented
}

// see how many slider moves you are NOT handling ?
private void trackBar1_Scroll(object sender, EventArgs e)
{
    Console.WriteLine($"scrolling: {trackBar1.Value}");
}

// value on entry
int entryValue;

private void trackBar1_Enter(object sender, EventArgs e)
{
    entryValue = trackBar1.Value;

    Console.WriteLine($"enter: {trackBar1.Value}");
}

// Leave event
private void trackBar1_Leave(object sender, EventArgs e)
{
    Console.WriteLine($"leave: {trackBar1.Value}");

    if (trackBar1.Value == entryValue) return;

    // do calculations
    // Calculate(trackbar1.Value);
}

// if the user presses Enter, force Leave
// you must set Focus to another Control on the Form
private void trackBar1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter) ActiveControl = button1;
}
 
Share this answer
 
v3
Comments
Paul Carson 2021 29-Jul-21 3:47am    
Yes this looks like a more elegant solution, I'd prefer to calculate less times and still have the same outcome. An example would be great thanks.
BillWoodruff 29-Jul-21 5:31am    
sample code will be added today. cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900