0

Working with a WPF application. I am very much wonderin if it is possible to get a function luke the examle below into a class function (im not yet very experianced with C#).

private void counter01_Tick(object sender, EventArgs e)
{
    if (counter01Ticks > 0)
    {
        //subtract 1 each time
        counter01Ticks--;

        //subtrack 1 secon each time
        counter01Span = counter01Span.Subtract(TimeSpan.FromSeconds(1));

        //update the progressbar
        progBar01.Value++;

        //get the % to show 
        progBar01Text.Text = Convert.ToString(Math.Round(((progBar01.Value / progBar01.Maximum) * 100), 0)) + "%";

        //Label1 will show the count down.
        string countDown = counter01Span.ToString();
        TimeRemain01.Content = countDown;
    }
    else
    {
        counter01.Stop();
        resetCounter01();

        WarningMessage msgWarnOne = new WarningMessage();
        msgWarnOne.warnMessage.Text = Properties.Settings.Default.msgScout01;
        msgWarnOne.ShowDialog();  
    }
}

It is just a part of a counter. but i want to add more counters to my application later on. Therefore i marked all the parameters with a number (01) in my code.

So what i do not want to do, i copy-paste the code and change the number for every counter, but rather have the number as a input number or something.

Would that be possible? if i9 would understand it for this small part of code, i think i will be able to do it with the other parts too (above is only the tick form a counter).

@Users that user the answer below: http://www.c-sharpcorner.com/uploadfile/mahesh/user-control-in-wpf/ Has helped me understand it better and migh be usefull to read too.

Dante1986
  • 58,291
  • 13
  • 39
  • 54

2 Answers2

2

Yes you can put all of this (together with your XAML declaration) into a WPF-user control and put multiple of these into other Windows/Controls/...

Just look at the tutorial I linked in - should explain everything you need.

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Ahaa i see. Seems i have a lot to figure our hehe. thanks for the link – Dante1986 Apr 05 '12 at 06:37
  • I dont really have a sender in my case. But i guess it is not possible to change all the "01" to some Var and make it a function where i input that var. Like 01/02/03 ? – Dante1986 Apr 05 '12 at 06:47
  • like a text-find/replace - some kind of preprocessor? No I don't think you would want to do that.... – Random Dev Apr 05 '12 at 06:49
  • It is a application with several counters. You would not advice to copy/paste the code for each counter right? Thats for the first one i named all parameters 01, and for the second i will do all with 02. Unfortunualy im not very experianced with C# yet, so im not really sure how to pick that up. I will try if i can make some sense out of that tooltip and use it somehow in my case. – Dante1986 Apr 05 '12 at 06:53
  • No I would not advise this - I would create a User-control with your UI and one timer doing just what you want and then putting multiple instances of this user-control into your application - so you only write the code and the XAML once – Random Dev Apr 05 '12 at 07:01
  • have been reading this tutorial too: http://www.c-sharpcorner.com/uploadfile/mahesh/user-control-in-wpf/ and it really helpt understranding the principle. I will try this for my solution. thanks again for the help – Dante1986 Apr 05 '12 at 08:52
-1

You can use method Control.FindControl;

Another approach is to make user control - and therefore you will work with only 1 array of controls in every counter_Tick

JleruOHeP
  • 10,106
  • 3
  • 45
  • 71
  • -, `.FindControl` is located in `System.Web.UI`. the op is dealing with wpf ... here's a solution (http://stackoverflow.com/questions/636383/wpf-ways-to-find-controls) for finding controls. btw: and usercontrols are also asp.net specific ... –  Apr 05 '12 at 06:29
  • User controls can be made everywhere. And FindControl is just an example, and it isn`t very good approach – JleruOHeP Apr 05 '12 at 06:33
  • @JleruOHeP, if you think it's not a very goo approach, then why are you suggesting it? – svick Apr 05 '12 at 06:37
  • Just to show the possibility. Maybe I should rephrase myselsf and start 2 paragraph with 'Better' – JleruOHeP Apr 05 '12 at 06:53