funcions in C# Windows form
-
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 -
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 JoeWhat 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.
-
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.
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
-
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
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.... :-\
-
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 JoeTo 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 asSystem.Windows.Forms.Timer
that have the threading taken care of for you invisibly - it has a property calledInterval
(measured in milliseconds!) and aTick
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, calltimer.Stop();
in theTick
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
-
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 JoeJust 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 -
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 JoeOK... 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 differentRegs Joe