Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to clean this up and the usual "private void turnknob(string knobName) paired with turnknob("one".ToString());" isn't working. Any suggestions? The objective is to reference the turnknob method from each mouse click and just replacing the knobname. Instead of repeating the same code in all the Mouse click events. Any help would be greatly appreciated!

Just so I can feel like I've contributed too, if you place an eclipse in your window and throw an image of a round button on it, with this code, it spins around when you click it. And spins back when you click again.

namespace ShareEventHandler
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private bool knobLoc = false;
 
        private void turnknob(string knobName)
        {
            if (knobLoc == false)
            {
                DoubleAnimation da = new DoubleAnimation(0, 360, new Duration(TimeSpan.FromSeconds(1)));
                RotateTransform rt = new RotateTransform();
                knobName.RenderTransform = rt;
                knobName.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                knobLoc = true;
            }
            else
            {
                DoubleAnimation da = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(1)));
                RotateTransform rt = new RotateTransform();
                knobName.RenderTransform = rt;
                knobName.RenderTransformOrigin = new Point(0.5, 0.5);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                rt.BeginAnimation(RotateTransform.AngleProperty, da);
                knobLoc = false;
            }
        }
 
        private void one_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("one".ToString());
        }
 
        private void two_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("two".ToString());
        }
 
        private void three_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            turnknob("three".ToString());
        }
    }
}
Posted
Comments
Richard Deeming 1-Oct-15 14:56pm    
turnknob("one".ToString());
There is no point calling .ToString() on something which is already a string - particularly a string literal!


knobName.RenderTransform = rt;
knobName.RenderTransformOrigin = new Point(0.5, 0.5);

The knobName parameter is a string. The string class doesn't contain properties called RenderTransform or RenderTransformOrigin, so your code will not compile.


Comparing a bool value to true or false is redundant. Instead of if (knobLoc == false), use if (!knobLock).


Most of the code in the if and else branches is duplicated. You can greatly simplify the code by removing the duplication:
DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(1)));
...
knobLoc = !knobLoc;

As far as I can see you can use the same event handler for all of the knobs.

Try adding the following to the constructor
C#
one.MouseLeftButtonDown += general_MouseLeftButtonDown;
two.MouseLeftButtonDown += general_MouseLeftButtonDown;
three.MouseLeftButtonDown += general_MouseLeftButtonDown;

And add the new event handler
C#
private void general_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
   turnknob(((Control)sender).Name);
}
 
Share this answer
 
v2
Comments
Ralf Meier 1-Oct-15 2:29am    
@Mika:
You should rename your Handler-Method to "general_MouseLeftButtonDown" ...

But I'm not sure that this is working all right because I don't understand where the different "MouseLeftButtonDown" come from ...
In my opinion there could be only one "MouseLeftButtonDown" with different knob (Sender) positions ...
I think, more Information is required ...
Wendelius 1-Oct-15 14:21pm    
Darn, thanks for pointing that out!
This is what happens when you code with notepad... :)

Not sure but I understood that the left button down was the 'click' OP meant that needs to be generalized. I could be wrong though.
Maybe i should start at the beginning. I have 3 knobs that turn when you click them. The knobs turn and turn back like they should. But there's got to be a less redundant way of writing this. My original post was my attempt at cleaning it up, but obviously not working.

With all 3 chunks of code being the same, aside from the name of the knob, how would you put all three under one method and change the name depending on which knob you click? Examples would definitely be appreciated.

Mika, I feel like your solution might be on the right path, but I don't know what to do with the top portion

Here's what the code looks like [edited a bit]:

private bool knobLoc = false;

private void one_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    one.RenderTransformOrigin = new Point(0.5, 0.5);
    one.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}

private void two_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    two.RenderTransformOrigin = new Point(0.5, 0.5);
    two.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}

private void three_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoubleAnimation da = new DoubleAnimation((knobLoc ? 360 : 0), (knobLoc ? 0 : 360), new Duration(TimeSpan.FromSeconds(0.5)));
    RotateTransform rt = new RotateTransform();

    if (!knobLoc) { knobLoc = !knobLoc; }

    else { knobLoc = false; }

    three.RenderTransformOrigin = new Point(0.5, 0.5);
    three.RenderTransform = rt;
    rt.BeginAnimation(RotateTransform.AngleProperty, da);
}
 
Share this answer
 

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