用户控制并重用它们 [英] User controls and reusing them

查看:64
本文介绍了用户控制并重用它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(VB Winforms)

我想知道是否有一种方法可以重用用户控件。



我有几个用户控件我想在我的整个应用程序中使用但我遇到了一个障碍,其中:

(1)如果我在声明部分声明用户控件然后将其添加到Panel控件,那么我清除Panel中的所有控件我无法重用用户控件,



(2)如果我

  Dim  ucUserControl 作为  _ucUserControl 

在过程或函数中我不能从另一个表单传递信息。我发现的是我在用户控件中从Form传递给TextBox的信息存在,但它没有显示在TextBox的文本中。



I知道这与全球和本地范围有关但我发现很难找到一个有意义且可持续的解决方案,以便能够有效地重复使用用户控制。



指导意见赞赏。



谢谢

T



我是什么尝试过:



尝试全局和本地(程序和函数)声明,只有全局声明有效,但一旦用户控制被清除,你需要重新声明它,一旦发生这种情况,它就会失去全局范围,信息也不会显示。

解决方案

似乎我可能在我的方式中找到了错误 - 而这不是对我有意义。



我有

  .ucUserControl.Textb ox1.text 

一旦删除我。它工作了!!



我不知道为什么,但我确信在这个疯狂的背后有一些理论。



只是为了得到更好的反馈,因为我可以直接思考。



可以有效地重用UserControls之后,例如:



而不是全局声明(我通常会这样做)我现在几乎可以在我的代码中的任何地方声明它:

 私有  Sub  GetData()
Dim ucMyControl As New _ucMyControl
使用 ucMyControl
.Dock = DockStyle.Fill
.Visible = True
结束 使用

' 添加到主面板
pnlMain.Controls.Clear
pnlMain.Controls.Add(ucMyControl)

' 填充
ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text
' etc .....

End Sub





您现在可以在其他任何地方重用该用户控件从父控件中清除它后你的程序。



请记住,永远不要使用我。当提到用户控制时。



问候,

T



编辑:

新骗局!!!!没有什么是有意义的!



如果我使用

 。 ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text 

然后MyControl.TextBox的Text属性有一个值但是没有显示。



如果我使用

 ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text 

,那么MyControl的 Text 属性。 TextBox有 NO 值但它显示正确的信息。



即使我直接指定一个值,例如:

 ucMyControl.TextBox1.Text =   12345 

12345的值显示但该值未存储在控件的 Text 属性中。



任何想法为什么会这样正在发生什么?


继续解决方案1 ​​:(解决方案1变得过于冗长)



我已确定:

  .ucMyControl.TextBox1.Text =   12345 



会将12345的值存储在 Text 属性中 BUT 不显示!!



我还确定:

 ucMyControl.TextBox1 .Text =   12345 



将显示值12345不将其存储在文本属性中!!



我现在可以确认:

  .ucMyCotrol.TextBox1.Text =   12345 
ucMyControl.TextBox1.Text = .ucMyControl.TextBox1.Text



也将值12345存储在其 Text 属性中显示值12345。



A对这个异常的回答是什么? (这是我做错了什么,而应该去睡觉......睡在它上面)



问候,

T


(VB Winforms)
I want to know if there is a way that one can reuse user controls.

I have several user controls I want to use throughout my application but I have run into a snag where:
(1) If I declare the user control in the declaration section and then add it to a Panel control, the moment I clear all controls from the Panel I am not able to reuse the user control,

(2) If I

Dim ucUserControl As New _ucUserControl

within a Procedure or Function I am unable to pass information to it from another Form for instance. what I have found was that the information I passed from a Form to a TextBox in the User Control exists but it is not showing in the text of the TextBox.

I know it has something to do with Global and Local scope but I find it difficult to find a meaningful and sustainable solution to be able to reuse User Controls effectively.

Guidance will be appreciated.

Thanks
T

What I have tried:

Tried both Global and Local (Procedures and Functions) declarations and only the Global declaration works but once the User Control is cleared you need to redeclare it and as soon as that happens it loses Global scope and information does not show.

解决方案

Seems I may have found the err in my ways - and that does not make sense to me.

I had

Me.ucUserControl.Textbox1.text

and as soon as I remove the "Me." it worked !!

I have no idea why but I am sure there is some theory behind the madness in this one.

Just to give better feedback now that I can think straight again.

One can effective reuse UserControls after it has been cleared before e.g.:

Instead of the Global declaration (which I normally would do) I can now declare it almost anywhere in my code:

Private Sub GetData()
   Dim ucMyControl As New _ucMyControl
   With ucMyControl
      .Dock = DockStyle.Fill
      .Visible = True
  End With

   'Add to Main Panel
   pnlMain.Controls.Clear
   pnlMain.Controls.Add(ucMyControl)

   'Populate
   ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text
   'etc.....

End Sub



You can now reuse that user control anywhere else in your program after you have cleared it from the parent control.

Just remember, never to use "Me." when referring to the User Control.

Regards,
T

EDIT:
New snag!!!! Nothing makes sense at all!!

If I use

Me.ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text

then the Text Property of MyControl.TextBox has a value but it is not displayed.

If I use

ucMyControl.TextBox1.Text = ExternalForm.TextBox1.Text

then the Text Property of MyControl.TextBox has NO value however it displays the correct information.

Even if I assign a value directly e.g:

ucMyControl.TextBox1.Text = "12345"

the value of "12345" is displayed but the value is not stored in the Text property of the control.

Any idea why this is happening ?


Continued from Solution 1: (Solution 1 getting too lengthy)

I have established that:

Me.ucMyControl.TextBox1.Text = "12345"


will store the value of "12345" in its Text property BUT not display it!!

I have also established that:

ucMyControl.TextBox1.Text = "12345"


will display the value "12345" BUT not store it in the Text property!!

I can now confirm that:

Me.ucMyCotrol.TextBox1.Text = "12345"
ucMyControl.TextBox1.Text = Me.ucMyControl.TextBox1.Text


will both store the value "12345" in its Text property as well as display the value "12345".

Anyone with an answer to this anomaly? (Is it something I am doing wrong and should rather go to bed....and sleep over it)

Regards,
T


这篇关于用户控制并重用它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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