简体   繁体   English

ShowDialog后如何在表单中执行连续任务

[英]How to execute continuous task in form after ShowDialog

I have C# application, where main form opens another form by calling ShowDialog().我有 C# 应用程序,其中主窗体通过调用 ShowDialog() 打开另一个窗体。 I would like to have textbox in that another form with some text, which is updated once per second.我想在另一种形式的文本框中使用一些文本,每秒更新一次。 Where should I put code updating text box?我应该把代码更新文本框放在哪里?

I know that main form is suspended while ShowDialog is called, until it returns.我知道在调用 ShowDialog 时主窗体被挂起,直到它返回。 So, I think code must be somewhere in the source of opened form, but how to add it there.所以,我认为代码必须在打开表单的源代码中的某个地方,但是如何在那里添加它。

Thanks in advance!提前致谢!

What exactly do you mean with "other form". “其他形式”到底是什么意思。 Because there are multiple solutions...因为有多种解决方案...

Solution A (Its a Different FormClass so its different code)解决方案 A (它是一个不同的 FormClass 所以它的代码不同)

If u have MainForm and SecondForm for example, the secondform will have different code and there you can write your Logic for that textbox..例如,如果您有 MainForm 和 SecondForm,则 secondform 将具有不同的代码,您可以在那里为该文本框编写逻辑..

Solution B (Its a Different FormClass so you have access to the Constructor)解决方案 B (它是一个不同的 FormClass,因此您可以访问构造函数)

Every Form will have a Constructor... as you are creating your form you are using the constructor.. exmpl:每个表单都会有一个构造函数......当你创建你的表单时,你正在使用构造函数......例如:

frmSecond scnd = new frmSecond();

you could rewrite the constructor for the second form and pass in the TextField on create您可以重写第二个表单的构造函数并在创建时传入 TextField

frmSecond scnd = new frmSecond(textVariable);

Solution C (its actually the same Form and you reopen ur mainForm)解决方案 C (它实际上是同一个表单,你重新打开你的主表单)

If this is the case, you wont have different code for the Forms.如果是这种情况,您将不会有不同的表单代码。 but still its possible to change variables of the other Form.但仍然可以更改其他表单的变量。

If u have a Variable for your TextField you can simply do :如果您的 TextField 有一个变量,您可以简单地执行以下操作:

frmMain mainForm = new frmMain();
mainForm.textVariable = "textToShow";
mainForm.showDialog();

Conclusion结论

There are multiple and yes very easy ways and prob even way more for a solution.有多种方法,是的,非常简单的方法,甚至可能有更多的解决方案。 But i hope i could make you Realise what information is needed to solve a question, and that it would be way easier to Answer.但我希望我能让你意识到解决问题需要哪些信息,并且回答起来会更容易。 As people here answer Questions with Fun and if ur Question doesnt provide information to Answer it often People will just not Answer it even if its an Easy Question.因为这里的人们有趣地回答问题,如果你的问题没有提供信息来回答它,人们通常不会回答它,即使它是一个简单的问题。

Just Provide some Code next Time下次只需提供一些代码

I hope one of my Solutions could help you.我希望我的解决方案之一可以帮助您。 Greets问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM