Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm doing this Windows Forms application in C# so when you click this button multiple times, it calculates the average clicks per second you do.
I just yesterday started learning C# and the only language im good at is Lua. In Lua this would be simple, just use a table as they're very dynamic and flexible. I just have no clue how to do this in C#, the MSDN Articles just confuse me.
How would I store the times between clicks? Arrays? I have no clue. This is the button click function I have so far
private void button1_Click(object sender, EventArgs e)
{
//DO STUFF
}
You could store the time of each click, then compute the average of the times between them:
private List<DateTime> clickTimes = new List<DateTime>();
private void button1_Click(object sender, EventArgs e)
{
this.clickTimes.Add(DateTime.Now);
if (this.clickTimes.Count > 2)
{
double averageSeconds = this.clickTimes.Zip(this.clickTimes.Skip(1), (a,b) => (b-a).TotalSeconds)).Average();
// Do something with the average seconds between each click here
}
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working on a Laser system. The system comes with it's own manual, including an SDK reference, but I need help on understanding the definition of a function included in the reference:
and
To use this function, I have this code:
private void BrightnessButton_Click(object sender, EventArgs e)
{
Double setBrightness;
laser.setDispBrightness(out setBrightness);
}
But it keeps giving me this error:
may not be passed with the 'out keyboard'" << I think you meant 'keyword'
They also provided me with the sample code below:
How can I make this work? Any help in understanding how to program just simply the brightness would be appreciated. I am having trouble understanding what to put in for the parameters. Thanks
Only use an out variable when calling the get versions of the function. When calling the set versions of the function, you should have already set a value for the variable:
private void BrightnessButton_Click(object sender, EventArgs e)
{
Double setBrightness = .5; // set to 50%
laser.setDispBrightness(setBrightness);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to set the value of a textblock to what hour it is, I have the following code:
timeHours.Text = DateTime.Now.Hour.ToString();
But the textblock doesn't show what hour it is, it doesn't show anything and stays as the original text, how do I solve this problem?
If you want to show the current hour in a text box, and have it update properly every 60 minutes then you need some sort of background process that changes the value that is displayed in the textbox. You can do this with a simple Timer object and an event.
using System.Timers;
class myclass
{
System.Timers.Timer timer;
public void initialise()
{
timer = new System.Timers.Timer(10000);
timer += new ElapsedEventHandler(_timer_Elapsed);
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
timeHours.Text = DateTime.Now.Hour.ToString();
}
}
This updates the hour every 10 seconds (10000 milliseconds), not very efficient but does the job.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Having hard time dealing with this because every time I navigate to another form my project lags or is unresponsive for a few seconds before it shows the content of the window.
How can I close all the child windows with a method?
I have tried this.Close() but that didn't work.
You may use Application Class as a place of storing a links to the child windows. It creates a singleton Current which is accesable from any place
public partial class App : Application
{
private List<Window> childWindows = new List<Window>();
public List<Window> ChildWindows{get{return childWindows;}}
}
To register new ChildWindow add this into Initialized event handler
private void ChildWindow_Initialized(object sender, EventArgs e)
{
((App)Aplication.Current).Windows.Add(this);
}
on ChildWindow Closing you have to remove link from collection
private void ChildWindow_Closed(object sender, EventArgs e)
{
((App)Aplication.Current).Windows.Remove(this) // here
}
if needs you may replace List with Dictionary to have a possibility of searching by key
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want write a program that :
In textbox type a picturebox name and in other textbox type location number,then press button to move that picturebox that name is equal textbox1 to that location.
my question is how I can send a object(picturebox) to a Function?
If I understand you correctly you want to move a picture box by entering its name into a textbox? Then this is a possibility:
private void button_click(object sender, EventArgs e)
{
string pBoxName = textbox1.Text;
// I don't quite understand what you mean by 'location number'
int newPos = int.Parse(textbox2.Text);
// boxList is a List<PictureBox>
PictureBox pBox = boxList.Where(x => x.Name.Equals(pBoxName)).FirstOrDefault();
if(pBox != null)
{
MoveTheBox(pBox, newPos);
}
}
private void MoveTheBox(PictureBox box, int newPos)
{
// move the box
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm building a program with C# in Windows Forms, and the following question came to me. I have several buttons in my form and when any of them is clicked, I want to be able to store its ID in a single variable that will handle only one ID at a time. I already have a method that does this, but the fact is I don't want to call this method from the event handler of each button:
button1_Click(object senders /* ... yada yada ... */)
Is there a way how I can simplify this with a single method? Is it even possible?
You don't need many Click event handlers for your buttons, just 1 is enough:
private void buttons_Click(object sender, EventArgs e){
Button button = sender as Button;
//do something with the clicked button
//...
}