Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NETMVC理解与绑定_Vb.net - Fatal编程技术网

VB.NETMVC理解与绑定

VB.NETMVC理解与绑定,vb.net,Vb.net,我正试图将我的代码逻辑从gui中分离出来,就像在MVC原则中一样,我试图实现的是非常简单的 我有我的Form1,它包含一个文本框和按钮,一旦单击按钮,它将在我的控制器类中加载一个函数,该函数使用实体向数据库添加一个字符串,然后应该用这个名称更新文本框 我想我需要做的是将原始表单传递给表单上的textbox对象,然后将其数据绑定到表单上的textbox对象,这就是我的逻辑失败的地方 Public Class Form1 Private mf As New MainForm(Me)

我正试图将我的代码逻辑从gui中分离出来,就像在MVC原则中一样,我试图实现的是非常简单的

我有我的Form1,它包含一个文本框和按钮,一旦单击按钮,它将在我的控制器类中加载一个函数,该函数使用实体向数据库添加一个字符串,然后应该用这个名称更新文本框

我想我需要做的是将原始表单传递给表单上的textbox对象,然后将其数据绑定到表单上的textbox对象,这就是我的逻辑失败的地方

Public Class Form1
    Private mf As New MainForm(Me)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        mf.buttonClick()
    End Sub
End Class

Public Class MainForm
    Private Property a As Form
    Public Sub New(ByVal s As Form)
        a = s
    End Sub



    Function buttonClick() As Boolean

        Dim context As TestDBEntities2 = New TestDBEntities2
        Dim newCategory As tTable = New tTable
        newCategory.Name = "Test1 " & Today.DayOfWeek
        context.tTables.Add(newCategory)
        context.SaveChanges()
        Dim current As String = newCategory.Name
        a.DataBindings.Add("text", "TextBox1", current)

        Return True

    End Function

End Class
我的错误是: 无法绑定到数据源上的属性或列Test1 6

我看得对吗?或者我离这太远了,有一个明显的原因,这不起作用


任何意见都将不胜感激!将数据传回源而不作为函数返回的最佳方法是什么?

您似乎误解了Add方法

第一个参数是要绑定到的控件属性的名称。这应该是“文本”,而不是“文本”

第二个参数是包含要绑定的数据的对象。您传递的是目标控件的名称,而不是数据源。您还绑定到表单而不是文本框。因此,您所说的是希望将表单的text属性绑定到可以从字符串“TextBox1”提取的数据

第三个论点是去哪里寻找数据。例如,如果为第二个参数传递了FileInfo对象,并且希望绑定文件的路径,则将传递字符串“FullName”,因为这是包含所需数据的属性的名称。因此,您告诉绑定在名为“test16”的string类上查找属性,这就是为什么您收到错误消息说找不到它

我想你想要的是

a.TextBox1.DataBindings.Add("Text", newCategory, "Name");

<>你应该考虑一下改变你的代码,以便更多地反映出:

  • 用于交换数据并指示操作触发器,而不是使用表单对象
  • 通常,控制器知道表单,而不是相反,所以在项目中交换这个。这也反映了第一点
因此,Windows窗体应用程序的可能解决方案如下所示:

该表单有一个按钮和一个文本字段,一个事件表示按钮单击,一个WriteOnly属性从表单外部填充文本框:

Public Class MainForm
    Public Event GenerateAndShowEvent()
    ' allow text box filling
    Public WriteOnly Property SetTextBoxContent()
        Set(ByVal value)
            generatedInputTextBox.Text = value
        End Set
    End Property

    Private Sub generateAndShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateAndShowButton.Click
        ' forward message: inform the subscriber that something happened
        RaiseEvent GenerateAndShowEvent()
    End Sub
End Class
Public Class MainFormController
    ' the form that the controller manages
    Private dialog As MainForm = Nothing

    Public Sub New()
        dialog = New MainForm()
        ' bind to the form button click event in order to generate the text and response
        AddHandler dialog.GenerateAndShowEvent, AddressOf Me.GenerateAndShowText
    End Sub

    ' allow the world to access readonly the form - used to start the application
    Public ReadOnly Property GetMainForm()
        Get
            Return dialog
        End Get
    End Property

    Private Sub GenerateAndShowText()
        ' create the text
        Dim text As String = "Test test test"
        ' access the Database ...
        ' give the generated text to the UI = MainForm dialog!
        dialog.SetTextBoxContent = text
    End Sub

End Class
Public Class DataContainer
    Private t As String
    Private i As Integer

    Public Property Text() As String
        Get
            Return t
        End Get
        Set(ByVal value As String)
            t = value
        End Set
    End Property
    Public Property Id() As Integer
        Get
            Return i
        End Get
        Set(ByVal value As Integer)
            i = value
        End Set
    End Property
End Class
Public Property TextBoxDataSource()
    Get
        Return TextBoxBindingSource.DataSource
    End Get
    Set(ByVal value)
        TextBoxBindingSource.DataSource = value
    End Set
End Property
Public Sub New()
    InitializeComponent()
    ' bind the TextBox control manually to the binding source
    ' first Text is the TextBox.Text property
    ' last Text is the DataContainer.Text property
    generatedInputTextBox.DataBindings.Add(New Binding("Text", TextBoxBindingSource, "Text"))
End Sub
控制器类了解表单,创建表单,绑定(侦听)表单的按钮单击事件,并使表单为全世界只读(公开表单):

现在剩下的是首先创建控制器,它创建表单并使用表单显示它。可以这样做:

使用
Main
方法创建AppStarter模块:

Module AppStarter
    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)
        ' create the controller object
        Dim controller As MainFormController = New MainFormController()
        ' use the public property to get the dialog
        Application.Run(controller.GetMainForm)
    End Sub
End Module
在项目设置中,取消选中启用应用程序框架设置,并将
AppStarter
模块设置为项目的起点:

现在您有了一个使用MVC模式的Windows窗体项目

如果仍要对TextBox控件使用数据绑定,请创建表示将从控制器传输到窗体的字段的:

Public Class MainForm
    Public Event GenerateAndShowEvent()
    ' allow text box filling
    Public WriteOnly Property SetTextBoxContent()
        Set(ByVal value)
            generatedInputTextBox.Text = value
        End Set
    End Property

    Private Sub generateAndShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateAndShowButton.Click
        ' forward message: inform the subscriber that something happened
        RaiseEvent GenerateAndShowEvent()
    End Sub
End Class
Public Class MainFormController
    ' the form that the controller manages
    Private dialog As MainForm = Nothing

    Public Sub New()
        dialog = New MainForm()
        ' bind to the form button click event in order to generate the text and response
        AddHandler dialog.GenerateAndShowEvent, AddressOf Me.GenerateAndShowText
    End Sub

    ' allow the world to access readonly the form - used to start the application
    Public ReadOnly Property GetMainForm()
        Get
            Return dialog
        End Get
    End Property

    Private Sub GenerateAndShowText()
        ' create the text
        Dim text As String = "Test test test"
        ' access the Database ...
        ' give the generated text to the UI = MainForm dialog!
        dialog.SetTextBoxContent = text
    End Sub

End Class
Public Class DataContainer
    Private t As String
    Private i As Integer

    Public Property Text() As String
        Get
            Return t
        End Get
        Set(ByVal value As String)
            t = value
        End Set
    End Property
    Public Property Id() As Integer
        Get
            Return i
        End Get
        Set(ByVal value As Integer)
            i = value
        End Set
    End Property
End Class
Public Property TextBoxDataSource()
    Get
        Return TextBoxBindingSource.DataSource
    End Get
    Set(ByVal value)
        TextBoxBindingSource.DataSource = value
    End Set
End Property
Public Sub New()
    InitializeComponent()
    ' bind the TextBox control manually to the binding source
    ' first Text is the TextBox.Text property
    ' last Text is the DataContainer.Text property
    generatedInputTextBox.DataBindings.Add(New Binding("Text", TextBoxBindingSource, "Text"))
End Sub
然后为文本框添加一个,并将其配置为使用DTObject:

现在将TextBox控件绑定到数据绑定控件:

剩下的是在表单中为TextBox数据绑定控件添加一个公共setter:

Public Class MainForm
    Public Event GenerateAndShowEvent()
    ' allow text box filling
    Public WriteOnly Property SetTextBoxContent()
        Set(ByVal value)
            generatedInputTextBox.Text = value
        End Set
    End Property

    Private Sub generateAndShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateAndShowButton.Click
        ' forward message: inform the subscriber that something happened
        RaiseEvent GenerateAndShowEvent()
    End Sub
End Class
Public Class MainFormController
    ' the form that the controller manages
    Private dialog As MainForm = Nothing

    Public Sub New()
        dialog = New MainForm()
        ' bind to the form button click event in order to generate the text and response
        AddHandler dialog.GenerateAndShowEvent, AddressOf Me.GenerateAndShowText
    End Sub

    ' allow the world to access readonly the form - used to start the application
    Public ReadOnly Property GetMainForm()
        Get
            Return dialog
        End Get
    End Property

    Private Sub GenerateAndShowText()
        ' create the text
        Dim text As String = "Test test test"
        ' access the Database ...
        ' give the generated text to the UI = MainForm dialog!
        dialog.SetTextBoxContent = text
    End Sub

End Class
Public Class DataContainer
    Private t As String
    Private i As Integer

    Public Property Text() As String
        Get
            Return t
        End Get
        Set(ByVal value As String)
            t = value
        End Set
    End Property
    Public Property Id() As Integer
        Get
            Return i
        End Get
        Set(ByVal value As Integer)
            i = value
        End Set
    End Property
End Class
Public Property TextBoxDataSource()
    Get
        Return TextBoxBindingSource.DataSource
    End Get
    Set(ByVal value)
        TextBoxBindingSource.DataSource = value
    End Set
End Property
Public Sub New()
    InitializeComponent()
    ' bind the TextBox control manually to the binding source
    ' first Text is the TextBox.Text property
    ' last Text is the DataContainer.Text property
    generatedInputTextBox.DataBindings.Add(New Binding("Text", TextBoxBindingSource, "Text"))
End Sub
并从控制器传输数据:

Private Sub GenerateAndShowText()
    ' create the text
    Dim text As String = "Test test test"
    ' access the Database ...
    ' give the generated text to the UI = MainForm dialog!
    'dialog.SetTextBoxContent = text
    Dim data As DataContainer = New DataContainer
    data.Text = text
    data.Id = 1 ' not used currently
    dialog.TextBoxDataSource = data
End Sub
也可以通过编程方式设置绑定-不要通过控件属性窗口进行设置,而是在窗体的构造函数中添加以下代码:

Public Class MainForm
    Public Event GenerateAndShowEvent()
    ' allow text box filling
    Public WriteOnly Property SetTextBoxContent()
        Set(ByVal value)
            generatedInputTextBox.Text = value
        End Set
    End Property

    Private Sub generateAndShowButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles generateAndShowButton.Click
        ' forward message: inform the subscriber that something happened
        RaiseEvent GenerateAndShowEvent()
    End Sub
End Class
Public Class MainFormController
    ' the form that the controller manages
    Private dialog As MainForm = Nothing

    Public Sub New()
        dialog = New MainForm()
        ' bind to the form button click event in order to generate the text and response
        AddHandler dialog.GenerateAndShowEvent, AddressOf Me.GenerateAndShowText
    End Sub

    ' allow the world to access readonly the form - used to start the application
    Public ReadOnly Property GetMainForm()
        Get
            Return dialog
        End Get
    End Property

    Private Sub GenerateAndShowText()
        ' create the text
        Dim text As String = "Test test test"
        ' access the Database ...
        ' give the generated text to the UI = MainForm dialog!
        dialog.SetTextBoxContent = text
    End Sub

End Class
Public Class DataContainer
    Private t As String
    Private i As Integer

    Public Property Text() As String
        Get
            Return t
        End Get
        Set(ByVal value As String)
            t = value
        End Set
    End Property
    Public Property Id() As Integer
        Get
            Return i
        End Get
        Set(ByVal value As Integer)
            i = value
        End Set
    End Property
End Class
Public Property TextBoxDataSource()
    Get
        Return TextBoxBindingSource.DataSource
    End Get
    Set(ByVal value)
        TextBoxBindingSource.DataSource = value
    End Set
End Property
Public Sub New()
    InitializeComponent()
    ' bind the TextBox control manually to the binding source
    ' first Text is the TextBox.Text property
    ' last Text is the DataContainer.Text property
    generatedInputTextBox.DataBindings.Add(New Binding("Text", TextBoxBindingSource, "Text"))
End Sub