如何操纵新创建的表单实例的控件? [英] How to manipulate controls of a newly created instance of a form?

查看:66
本文介绍了如何操纵新创建的表单实例的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下应该在

TwoSecondTimer.Enabled = True

之后执行公共类Root权限?

is executed in my Public Class Root right?

Public Module Timers
    Public WithEvents TwoSecondTimer As New Timer With {.Interval = 2000}
    Sub TwoSecondTick() Handles TwoSecondTimer.Tick
        Root.AddToMessages("tick")
        Root.AddToMessages(Main_Window.Cash)
        Main_Window.Cash += Main_Window.Income
        Main_Window.CashLbl.Text = "¤ " & FormatNumber(Main_Window.Cash)
    End Sub
End Module





东西是告诉时,计时器未启用。即使默认启用,计时器代码仍然无法正常工作...



... Root.AddToMessages有效,但涉及Main_Window的两行不要吨。 Root是我的第一个表单,Main_Window是通过反射实例化的表单之一。在运行期间没有错误,Cash变量只是没有改变它的默认值而CashLbl.Text永远不会改变。



编辑:

因此无法从我的Root类启用计时器,并且无法从我的计时器模块修改Main_Window控件/变量。我忘记了什么?



如果我以必要的方式修改我的代码以更改TwoSecondTimer和TwoSecondTick为共享,那么我得到错误NullReferenceException。


前一个是通过将Forms.Timer更改为Timers.Timer来解决的,因为计时器是在模块中而不是在表单上创建的。



注意,表格是这样创建的:



Thing is the timer is not enabling when told to. Even if enabled by default, the timer code still doesn't work anyway...

...Root.AddToMessages works, but the two lines involving Main_Window don't. Root is my first form, Main_Window is one of the forms being instantiated through reflection. There are no errors during runtime, the Cash variable just doesn't change from it's default value and CashLbl.Text never changes.


So the timer can't be enabled from my Root class and Main_Window controls/variables can't be modified from my Timers Module. What am I forgetting?

If I modify my code in ways necessary to change TwoSecondTimer and TwoSecondTick to be Shared, then I get the error NullReferenceException.

The previous was solved by changing Forms.Timer to Timers.Timer, since the timers were created in a module instead of on a form.

Note, the forms are created as such:

Dim NewWindow As Form = CType(Activator.CreateInstance(Type.GetType(AssemblyData.GetName & "." & sender.text.replace(" ", "_"), True, True)), Form)





所以如果引用了需要新创建的Main_Window实例,如何创建一个并将其与表单链接以操作表单?



So if a reference to the newly created instances of Main_Window is needed, how could I create one and link it with a form in order to manipulate the form?

推荐答案

您使用的是哪个计时器?如果是System.Windows.Forms.Timer,则必须将它放在Form上才能工作。
Which Timer are you using? If it's the System.Windows.Forms.Timer it has to be placed on a Form in order to work.


ChaosOverlord写道:
ChaosOverlord wrote:



啊,所以如果我要创建很多不同的控件,那么就没有办法避免大量代码来创建所有控件?

另外,如果最好在本地创建这些控件,那么从其他子例程或类调用这些控件的最佳方法是什么?


Ah, so if I am to create a lot of different controls, then there is no way of avoiding a large amount of code to create all of the controls?
Also, if these controls are better to be created locally, then what is the best way to call these controls from other subroutines or classes?

错误。控件数量不会转换为代码量。通过抽象出不同控件和其他元素的常用功能,可以避免大量代码。您的代码只是与不同的算法一样复杂。如果对许多控件使用相同的算法,则主要吸收它们的数量。你必须发现行为的共同元素,永远不要重演。请参阅:不要重复自己 - 维基百科,免费的百科全书 [ ^ ]。



-SA

Wrong. The number of controls is not translated into amount of code. You avoid large amount of code by abstracting out common functionality of different controls and other elements. Your code is only as complicated as different algorithms are. If you use the same algorithm for many controls, the number of them is majorly absorbed. You have to spot common elements of behavior and never ever repeat yourself. Please see: Don't repeat yourself - Wikipedia, the free encyclopedia[^].

—SA


ChaosOverlord_写道:
ChaosOverlord_ wrote:




Dim NewWindow As Form = CType(Activator.CreateInstance(Type.GetType(AssemblyData.GetName & "." & sender.text.replace(" ", "_"), True, True)), Form)

我需要summar反思你的反思问题和误解。



假设你的执行进入入口点( Main )方法在运行时,您的整个过程都加载了N个.NET类型。只需要在(相同)运行时期间引入类型编号N + 1时才需要反射。然后你有这样的选项:1)加载另一个单独开发的程序集,2)通过使用CodeDOM在同一进程中编译它来创建程序集,3)使用 System.Reflection.Emit <动态创建一个新的程序集/ code>。



尽管我全力以赴,但你从未提及过这样的需求。您所有的考虑都是关于使用相同的N类型。你不能更动态地使用它们,因为使用固定的类型集,用构造函数实例化它们已经是动态的。所以,从这一刻起,忘记 Activator ,直到你真的需要反射或 System.Reflection.Emit



-SA

I need to summarize the reflection issue and the misconception you have here.

Let's say, by the moment your execution enters the entry point (Main) method during runtime, your whole process have N .NET types loaded. You only need reflection only at the moment you need to introduce the type number N+1 during (the same) runtime. Then you have the such options as 1) load another assembly developed separately, 2) create an assembly by compiling it in the same process using CodeDOM, 3) dynamically create a new assembly using System.Reflection.Emit.

Despite my all my effort, you never mentioned such need. All your considerations were about using the same N types. You cannot use them more "dynamically", because, with fixed set of type, instantiating them with constructors is already as dynamic as it can be. So, from this moment, forget Activator, until you really need reflection or System.Reflection.Emit.

—SA


这篇关于如何操纵新创建的表单实例的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆