另一个有约束力的问题......更简单...... [英] Another Binding Question... Even Simpler...

查看:45
本文介绍了另一个有约束力的问题......更简单......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这不起作用:

------------------------------- ----------------------------

Windows窗体有三个控件:

TextBox1

BindButton

ShowTextButton

ChangeTextButton


代码:


Public Class SimpleClass


私有myText as String


Public Sub New(NewText as String)

myText = NewText

End Sub


Public Property Text()As String

Get

返回myText

结束获取

设置(值为字符串)

myText = Value

结束集

结束财产


结束班


私人mySimpleClass作为新的SimpleClass(你好)


Private Sub BindButtonClick(...)...

TextBox1.DataBindings.Add(" Text",mySimpleClass," Text")

End Sub


Private Sub ShowTextButtonClick(...)...

MsgBox(mySimpleClass.Text)

End Sub


Private Sub changeTextButtonClick(...)...

mySimpleClass.Textx =再见

结束子

--------------------------- --------------------------------


好​​的......这里'发生了什么......


1.运行程序。没问题

2.单击ShowText按钮。 "你好"显示。好的。

3.单击BindButton。 TextBox1显示Hello。还是很好。

4.输入世界字样。到TextBox1。

5.单击ShowText按钮。 "世界"显示。这么好......

6.单击ChangeText按钮。问题:TextBox1仍显示

World。不好。

7.单击ShowText按钮。 "再见"显示。正如预期的那样。


为什么这种单行为?如何让我的SimpleClass通知需要更改的

文本框?

--Zorpy


***已发送通过Devdex http://www.devdex.com ***

不要只是参加USENET ......获得奖励!

Why Doesn''t THIS work:
-----------------------------------------------------------
A Windows Form has three controls:
TextBox1
BindButton
ShowTextButton
ChangeTextButton

Code:

Public Class SimpleClass

Private myText as String

Public Sub New(NewText as String)
myText = NewText
End Sub

Public Property Text() As String
Get
Return myText
End Get
Set(Value as String)
myText = Value
End Set
End Property

End Class

Private mySimpleClass as new SimpleClass("Hello")

Private Sub BindButtonClick(...)...
TextBox1.DataBindings.Add("Text", mySimpleClass, "Text")
End Sub

Private Sub ShowTextButtonClick(...)...
MsgBox(mySimpleClass.Text)
End Sub

Private Sub changeTextButtonClick(...)...
mySimpleClass.Textx = "Goodbye"
End Sub
-----------------------------------------------------------

Ok... Here''s what happens...

1. Run the program. No problem
2. Click The ShowText Button. "Hello" is shown. Fine.
3. Click the BindButton. TextBox1 shows "Hello". Still Fine.
4. Type "World" into TextBox1.
5. Click the ShowText Button. "World" is shown. So Far So Good...
6. Click the ChangeText Button. PROBLEM: TextBox1 still shows
"World". NOT Good.
7. Click the ShowText Button. "GoodBye" is shown. As Expected.

Why this one-way behavior? How do I get my SimpleClass to notify the
Textbox it needs to change?
--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

它因为你需要通知绑定管理器而工作在表格上

发生了变化。


声明公共事件TextChanged作为事件处理程序


在你的设置属性,如果文本与

之前的文本不同,则通过调用提升此事件的子文件来提升事件TextChanged。


然后它将起作用。


-


OHM(特里伯恩斯)

。 。 。单手人。 。


当你不知道自己在做什么时,过得很快


" ZorpiedoMan" <否******** @ Beatles.com>在消息中写道

新闻:Ov ************** @ TK2MSFTNGP09.phx.gbl ...
It doesent work because you need to inform the binding manager on the form
that something has changed.

Declare a public Event TextChanged As Event Handler

In your Set Property, Raise the event if the text is different from before
by calling a sub which raises this event TextChanged.

Then it will work.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don''t know what you''re doing

"ZorpiedoMan" <No********@Beatles.com> wrote in message
news:Ov**************@TK2MSFTNGP09.phx.gbl...
为什么不这项工作:
------------------------------------------- ----------------
一个Windows窗体有三个控件:
TextBox1
BindButton
ShowTextButton
ChangeTextButton

代码:

公共类SimpleClass

私有myText as String

公共子新(NewText as String)
myText = NewText
End Sub

公共属性Text()As String
获取
返回myText
End Get
Set(Value as String )
myText = Value
End Set
End Property



私有mySimpleClass为新的SimpleClass(Hello)<私有Sub BindButtonClick(...)......
TextBox1.DataBindings.Add(" Text",mySimpleClass," Text")
End Sub

Private Sub ShowTextButtonClick(... )......
MsgBox(mySimpleClass.Text)
End Sub

私人子改变文本按钮点击(...)...
mySimpleClass.Textx =再见 ;
End Sub
--------------------------------------- --------------------

好的...这就是发生的事情......

1 。运行程序。没问题
2.单击ShowText按钮。 "你好"显示。好的。
3.单击BindButton。 TextBox1显示Hello。还好。
4.输入世界字样。到TextBox1。
5.单击ShowText按钮。 "世界"显示。这么好......
6.单击ChangeText按钮。问题:TextBox1仍显示
World。不好。
7.单击ShowText按钮。 "再见"显示。如预期的那样。

为什么这种单行为?如何让我的SimpleClass通知它需要更改的文本框?

--Zorpy

***通过Devdex发送 http://www.devdex.com ***
不要只是参加USENET .. .get为它奖励!
Why Doesn''t THIS work:
-----------------------------------------------------------
A Windows Form has three controls:
TextBox1
BindButton
ShowTextButton
ChangeTextButton

Code:

Public Class SimpleClass

Private myText as String

Public Sub New(NewText as String)
myText = NewText
End Sub

Public Property Text() As String
Get
Return myText
End Get
Set(Value as String)
myText = Value
End Set
End Property

End Class

Private mySimpleClass as new SimpleClass("Hello")

Private Sub BindButtonClick(...)...
TextBox1.DataBindings.Add("Text", mySimpleClass, "Text")
End Sub

Private Sub ShowTextButtonClick(...)...
MsgBox(mySimpleClass.Text)
End Sub

Private Sub changeTextButtonClick(...)...
mySimpleClass.Textx = "Goodbye"
End Sub
-----------------------------------------------------------

Ok... Here''s what happens...

1. Run the program. No problem
2. Click The ShowText Button. "Hello" is shown. Fine.
3. Click the BindButton. TextBox1 shows "Hello". Still Fine.
4. Type "World" into TextBox1.
5. Click the ShowText Button. "World" is shown. So Far So Good...
6. Click the ChangeText Button. PROBLEM: TextBox1 still shows
"World". NOT Good.
7. Click the ShowText Button. "GoodBye" is shown. As Expected.

Why this one-way behavior? How do I get my SimpleClass to notify the
Textbox it needs to change?
--Zorpy

*** Sent via Devdex http://www.devdex.com ***
Don''t just participate in USENET...get rewarded for it!



使用复选框的完整示例


这里我们有一个Class Person。在其中我们声明了一个名为

CheckedChanged的公共事件,它由绑定管理器

侦听一次绑定事件的属性组成。当物业

从选中更改为未选中时,我们调用一个帮助函数,该事件引发此事件,并且绑定管理器将控件(rad1)同步到此

属性。


Button1.click将aPerson对象的属性更改为checked,这是

然后通过rad1状态的绑定反映出来。


On_Load是我们设置绑定的地方


HTH


Private WithEvents aPerson As New Person


公共类人员


公共事件CheckedChanged As EventHandler


私有m_checked为布尔


Public Sub New()

m_checked = False

End Sub


公共财产Checked()As Boolean

获取

返回m_checked

结束获取

设置(ByVal值为布尔值)

m_checked = value

PropertyChanged(EventArgs.Empty)

结束集

结束正确ty


Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理MyBase.Load

rad1.DataBindings.Add(" Checked",aPerson," Checked")

End Sub


Private Sub Button1_Click(ByVal sender As System。对象,ByVal e As

System.EventArgs)处理Button1.Click

aPerson.checked = True

End Sub


-


OHM(特里伯恩斯)

。 。 。单手人。 。


Full Example using check boxes

Here we have an Class Person. Within it we declare a public event called
CheckedChanged, this is made up of the property which the binding manager
listens for an event for once binding has been set. When the Property
changes from checked to unchecked we call a helper function which raises
this event and the binding manager syncs the control ( rad1 ) to this
property.

Button1.click changes the property of the aPerson object to checked, this is
then reflected through the bindings in the rad1 state.

On_Load is where we set the bindings

HTH

Private WithEvents aPerson As New Person

Public Class Person

Public Event CheckedChanged As EventHandler

Private m_checked As Boolean

Public Sub New()
m_checked = False
End Sub

Public Property Checked() As Boolean
Get
Return m_checked
End Get
Set(ByVal value As Boolean)
m_checked = value
PropertyChanged(EventArgs.Empty)
End Set
End Property

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
rad1.DataBindings.Add("Checked", aPerson, "Checked")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
aPerson.checked = True
End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .



你好Zorpy,


我无法回答你的问题,但是当你这样做的时候它是有效的。


mySimpleClass.Text =" Goodbye"

Me.BindingContext(mySimpleClass).ResumeBinding()


我希望它无论如何都有帮助吗?


Cor
Hi Zorpy,

I cannot answer your question, however when you do this it works.

mySimpleClass.Text = "Goodbye"
Me.BindingContext(mySimpleClass).ResumeBinding()

I hope it helps anyhow?

Cor


这篇关于另一个有约束力的问题......更简单......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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