Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. funcions in C# Windows form

funcions in C# Windows form

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joey_go
    wrote on last edited by
    #1

    Hi All, I have a very basic windows applications form working. I would like to add this public function so that I can call it in the form code. public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor ) { System.DateTime ThisMoment = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ; System.DateTime AfterWards = ThisMoment.Add( duration ) ; while ( AfterWards >= ThisMoment ) { System.Windows.Forms.Application.DoEvents() ; ThisMoment = System.DateTime.Now ; } return System.DateTime.Now ; } Where do I paste this function so I can call it on the windows form. How do I add files with functions to my code.... do I have to encapsulate them in a class? Thanks in advance Joe

    A D realJSOPR J 4 Replies Last reply
    0
    • J joey_go

      Hi All, I have a very basic windows applications form working. I would like to add this public function so that I can call it in the form code. public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor ) { System.DateTime ThisMoment = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ; System.DateTime AfterWards = ThisMoment.Add( duration ) ; while ( AfterWards >= ThisMoment ) { System.Windows.Forms.Application.DoEvents() ; ThisMoment = System.DateTime.Now ; } return System.DateTime.Now ; } Where do I paste this function so I can call it on the windows form. How do I add files with functions to my code.... do I have to encapsulate them in a class? Thanks in advance Joe

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2

      What is that code supposed to do? Disable the form for a certain duration? Instead, set "this.Enabled = false;" and create a timer that sets "this.Enabled = true;" You can create a button on your form, double click it (that will create a button click event handler), then call the code you have above from that method.

      joey_go wrote:

      How do I add files with functions to my code.... do I have to encapsulate them in a class?

      Sounds like you have some reading/Googling to do.

      [Forum Guidelines]

      J 1 Reply Last reply
      0
      • A AspDotNetDev

        What is that code supposed to do? Disable the form for a certain duration? Instead, set "this.Enabled = false;" and create a timer that sets "this.Enabled = true;" You can create a button on your form, double click it (that will create a button click event handler), then call the code you have above from that method.

        joey_go wrote:

        How do I add files with functions to my code.... do I have to encapsulate them in a class?

        Sounds like you have some reading/Googling to do.

        [Forum Guidelines]

        J Offline
        J Offline
        joey_go
        wrote on last edited by
        #3

        My application writes some text to a textbox then I will call a 1 second delay (1000 millisecond) then write different data to the textbox. I'm probably lacking the fundamentals on how to call functions (methods) in C# and where to locate it in the files/classes. In C I would create the function prototype and the function and then call this function within main(). Calling the function on the form isn't the issue... it's how do I add this function (method) in to my Visual studio 2008 C# Windows form application. Any clues would be appreciated. Joe

        M 1 Reply Last reply
        0
        • J joey_go

          My application writes some text to a textbox then I will call a 1 second delay (1000 millisecond) then write different data to the textbox. I'm probably lacking the fundamentals on how to call functions (methods) in C# and where to locate it in the files/classes. In C I would create the function prototype and the function and then call this function within main(). Calling the function on the form isn't the issue... it's how do I add this function (method) in to my Visual studio 2008 C# Windows form application. Any clues would be appreciated. Joe

          M Offline
          M Offline
          mprice214
          wrote on last edited by
          #4

          joey_go wrote:

          Calling the function on the form isn't the issue... it's how do I add this function (method) in to my Visual studio 2008 C# Windows form application.

          Hi Joe, I'm still pretty wet behind the ears, but thought I could take a swing. If you select the "view code" button under the Solution Explorer (2nd from right) in the form [design] view or just double click the header of the form, you'll see the code for the form. You have a namespace and then under that probably "public partial class "formName" : Form, followed by an open curly bracket. Your function needs to be somewhere in between the open and closed curly brackets. You can then call it in Form_Load, button_Click, etc. Please be gentle if I've just wasted your time reading this.... :-\

          1 Reply Last reply
          0
          • J joey_go

            Hi All, I have a very basic windows applications form working. I would like to add this public function so that I can call it in the form code. public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor ) { System.DateTime ThisMoment = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ; System.DateTime AfterWards = ThisMoment.Add( duration ) ; while ( AfterWards >= ThisMoment ) { System.Windows.Forms.Application.DoEvents() ; ThisMoment = System.DateTime.Now ; } return System.DateTime.Now ; } Where do I paste this function so I can call it on the windows form. How do I add files with functions to my code.... do I have to encapsulate them in a class? Thanks in advance Joe

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            To answer your direct question, in C# all methods (functions in C) have to be within a class or struct. To look at a couple of other points, Application.DoEvents is rarely the correct solution. Here you are trying to keep your UI active when in reality it's busy in your while loop. The correct way to achieve this is to start another thread and do the pause on that, when it completes it can use a callback (event in C#) so it can be acted upon. There are a few controls such as System.Windows.Forms.Timer that have the threading taken care of for you invisibly - it has a property called Interval (measured in milliseconds!) and a Tick event.

            using System;
            using System.Windows.Forms;

            public partial class FormMain : Form
            {
            private Timer timer;

            public FormMain()
            {
                InitializeComponent();
                timer = new Timer();
                timer.Enabled = true;
                timer.Tick += new EventHandler(timer\_Tick);
            }
            
            void timer\_Tick(object sender, EventArgs e)
            {
                // Use DateTime.Now
            }
            
            public void Pause(int duration)
            {
                timer.Interval = duration;
                timer.Start();
            }
            

            }

            Inside your form call Pause(...). If you want the timer to stop after each pause, call timer.Stop(); in the Tick event handler. Alternatively you could create a thread yourself and implement what you want, something like:

            using System;
            using System.Threading;
            using System.Windows.Forms;

            public partial class FormMain : Form
            {
            public event EventHandler<PauseEndedEventArgs> PauseEnded;

            public FormMain()
            {
                InitializeComponent();
                PauseEnded += new EventHandler<PauseEndedEventArgs>(FormMain\_PauseEnded);
            }
            
            void FormMain\_PauseEnded(object sender, PauseEndedEventArgs e)
            {
                // e.EndDateTime contains the value
            }
            
            public void BeginPause(int duration)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(DoPause));
                thread.IsBackground = true;
                thread.Start(duration);
            }
            private void DoPause(object obj)
            {
                int duration = (int)obj;
                Thread.Sleep(duration);
                EndPause();
            }
            private void EndPause()
            {
                if (InvokeRequired)
                    Invoke(new MethodInvoker(EndPause));
                else
                    OnPauseEnded(new PauseEndedEventArgs(DateTime.Now));
            }
            protected virtual void OnPau
            
            1 Reply Last reply
            0
            • J joey_go

              Hi All, I have a very basic windows applications form working. I would like to add this public function so that I can call it in the form code. public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor ) { System.DateTime ThisMoment = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ; System.DateTime AfterWards = ThisMoment.Add( duration ) ; while ( AfterWards >= ThisMoment ) { System.Windows.Forms.Application.DoEvents() ; ThisMoment = System.DateTime.Now ; } return System.DateTime.Now ; } Where do I paste this function so I can call it on the windows form. How do I add files with functions to my code.... do I have to encapsulate them in a class? Thanks in advance Joe

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Just create a new static class to hold all your static methods. I often have a class called Globals that serves this purpose.

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              1 Reply Last reply
              0
              • J joey_go

                Hi All, I have a very basic windows applications form working. I would like to add this public function so that I can call it in the form code. public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor ) { System.DateTime ThisMoment = System.DateTime.Now ; System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ; System.DateTime AfterWards = ThisMoment.Add( duration ) ; while ( AfterWards >= ThisMoment ) { System.Windows.Forms.Application.DoEvents() ; ThisMoment = System.DateTime.Now ; } return System.DateTime.Now ; } Where do I paste this function so I can call it on the windows form. How do I add files with functions to my code.... do I have to encapsulate them in a class? Thanks in advance Joe

                J Offline
                J Offline
                joey_go
                wrote on last edited by
                #7

                OK... it's not very good C# code but I ended up using the following code in a console application.

                //Compare seconds
                starttime = DateTime.Now.Second; // Get current recorded seconds from system
                while (starttime == DateTime.Now.Second) { }// compare the recorded seconds to the system seconds
                // and continue to loop until the seconds are different

                Regs Joe

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups