This question already has answers here:
best practices to call methods in the parent form and access gui elements in parent form in c#
(3 answers)
Implementing MVC with Windows Forms
(6 answers)
Closed 4 years ago.
I've got a form class (Form1) and another class (Calculator).
In Calculator I've got this method:
public int Add(int number1, int number2)
{
return number1 + number2;
}
On the form, when you click a button, the Add method should be invoked and the text of a label should be updated to the result.
What is the correct way of updating the label? I'm not asking for the easiest way (I know tons of them), I wanna know how this is done in big Windows Form projects.
I've seen lots of things how to do it (with Threads, Delegates, and the Invoke() methods, etc), but I'm not quite sure how to correctly do it myself.
Thanks in advance!
EDIT: Oh noo the downvoting :( Please help me I'm desperate
label1.Text = calculatorinstance.Add(x, y).ToString();
or
label1.Text = $"{calculatorinstance.Add(x, y)}";
Your talk of Threads, Delegates, Invoke is only relevant if you plan on using background or long running processes. You don't mention that you are doing these long running processes so the above should suffice.
Related
This question already has answers here:
Execute specified function every X seconds
(7 answers)
Closed 1 year ago.
Uhm.. hello,
After year I tried c# without any tutorials. I made a calculator.. everything is works fine, but I want to add more things to it.. For example: when you use your numpad, it will work as well. But that's the problem.. I remember, that in Unity it was public void onUpdate(). But in forms.. it does not work. How to make method, which is updating every second and checking for example if bool is true. BTW, public void Update() does not work as well.. Where should I type it ?
The Update method is specific to the Unity engine and therefore does not work on WinForms. What you want is a timer.
This question already has answers here:
How do I update the GUI from another thread?
(47 answers)
Closed 6 years ago.
Simple question, I had an application in console mode. Every thing would write in the console using Console.WriteLine. However, now I have a form and a listbox. I know easily how to do it, however, what is the proper way to do it?
You can access the UI from another thread using delegate, simple and neat:
this.Invoke((MethodInvoker)delegate
{
textBox1.Value = "your text";
});
This question already has answers here:
Opening process and changing window position
(3 answers)
Closed 3 years ago.
I recently posted this question but didn't get any response on it:
Need help opening 2 windows explorer windows to two different spots
I still would like any help that can be given in this case. One thing that I thought might have been confusing about my previous post is that both of the examples I noted were using SetWindowPos. I really don't care about the method used to solve this problem only that A) it uses c# and B) that it meets the desired criteria as posted in my previous post. Please, if there is further clarification needed please ask me. Thanks!
Use a mouse. Click the corner of the window and drag it until it is the desired size.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to overlay an application over a full-screened program?
Is it possible in C# to inject a text to process (like fraps for example), but without using any .dll injections?
Thanks in advance for responses.
#update
"A text" means some fast refreshing labels or something, which will show informations e.g.:
Name:Test
Pos: x=123,y=456,z=0
Level: Unknown
Something.....
You can use automation to send keyboard actions and suchlike to another program. Otherwise if there is no exposed API then things look bleak. See this question for an overview on the methods you use to send keystrokes.
EDIT: What you're asking for is not injection, it's an overlay. What you're looking to do is take control of the display buffer so that your overlay always has a higher z-index than whatever is being rendered. Take a look at this answer
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Execute function at specific time
I want to run certain function at certain time.I tried Timer control.it's not working. My question is:How can I run a function every day at the 19:00 in C#?
Is there any way to check the time and have a Timer object?
Timer code:
int Interval(TimeSpan gorevZamani)
{
if ((gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds > 0.0)
return (int)(gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds;
else
return (int)((gorevZamani - DateTime.Now.TimeOfDay).Add(TimeSpan.FromDays(1)).TotalMilliseconds);
}
in the question set as possible duplicate: C# Execute function at specific time people suggest to use either Quartz.NET or windows Task Scheduler.
Both options could eventually serve the purpose but I believe, as I suggested already few times in similar previous questions, Windows Task Scheduler is better because you no code anything for it and let Windows do the scheduling for you and you focus only on the real business case of your application, which is what Windows cannot do for you, then rely on existing technologies to glue things together and don't have to debug or reinvent what has been done and is available for you anyway.
Use a scheduled task. A good way to do this is the at command, documented on MSDN here.