vb.net - 从另一个类编辑文本框 [英] vb.net - editing textbox from another class

查看:96
本文介绍了vb.net - 从另一个类编辑文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了一个简单的问题。我有一个表单form1和一个类dEdit(在一个单独的dEdit.vb文件上)。如何将form1中的textbox1从dEdit中的sub设置为vbnullstring?

我尝试直接从类dEdit更改它(如.. form1.textbox1.text = vbNullstring)但它没有任何效果。



然后我尝试了另一种方法。我在form1中创建了一个子editTBD(只有代码... textbox.text = vbNullstring ....)并从类dEdit调用它(如... form1.editTBD()....)但它没有做任何事情。



任何人都可以告诉我这样做的正确程序。我搜索了其他文章,但没有找到任何满意的答案。我之前已经解决了这个问题,有人可以在Form1中给出一些链接。

I got a simple problem here. I have a form form1 and a class dEdit (on a seperate dEdit.vb file). How can set textbox1 in form1 to vbnullstring from a sub in dEdit?
I tried changing it directly from the class dEdit (as.. form1.textbox1.text = vbNullstring) but it didnot have any effect.

Then i tried a different approach. I made a sub editTBD in form1 (with only code... textbox.text = vbNullstring....) and called it from the class dEdit (as...form1.editTBD().... ) but it didnot do anything either.

Can anyone tell me the proper procedure of doing it. I searched other articles but didnot find any satisfactory answer. I this problem has been solved earlier can someone give some links.

推荐答案

,添加以下行:

in Form1, add this line:
Friend Shared MyInstance As Form1





在Form1中添加此行加载事件:



Add this line in Form1 Load event:

MyInstance = Me





注意:

确保textbox1 (在Form1中声明不是私有的),它必须是Friend或Public才能直接从另一个类修改。



从另一个类更改textbox1:



Note:
be sure textbox1 (declared in Form1 IS NOT Private), it must be Friend or Public to be modified directly from another class.

To change textbox1 from another class:

Sub editTBD()
    ' Check if MyInstance is not null, neither closed/disposed
    If (Form1.MyInstance IsNot Nothing) AndAlso Not Form1.MyInstance.IsDisposed Then
        Form1.MyInstance.TextBox1.Text = vbNullString
    End If
End Sub



将sub editTBD放入dEdit类


put Sub editTBD in dEdit class


OOP的第一条规则是:不要这样做。



难以做到的原因是,如果你这样做,那么类必须知道包含它的表单 - 这意味着你不能使用该表,除了那个表单,是一个坏主意。



允许表单了解类,因为它在向表单添加类时会创建它的实例,但是类应该独立运作。



你应该怎么做?

它实际上很简单:你创建一个活动 [ ^ ]和你的类中的一个属性,表单处理事件,并通过属性获取所需的数据,然后更改它自己的控件。



那样,类不需要知道表单,如果表单可以使用数据,表单只会被更改 - 否则它不会处理事件,而且它仍然有效。
The first rule of OOP is: Don''t do it.

The reason it is difficult to do is that if you do it that way, the class must know about the form that contains it - which means you can''t use the class except on that form, which is a bad idea.

The form is allowed to know about the class, because it creates the instance of it when you add a class to the form, but the class should operate independently.

So how should you do this?
It''s actually quite simple: you create an Event[^] and a Property in your class, and the Form handles the Event, and gets the data it needs via the Property then changes it''s own controls.

That way, the class doesn''t need to know about the form, and the form only gets changed if it can use the data - otherwise it doesn''t handle the event, and it all still works.

这篇关于vb.net - 从另一个类编辑文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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