从form2输入数据后刷新form1 [英] Refresh form1 after entering the data from form2

查看:82
本文介绍了从form2输入数据后刷新form1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好,再次,
我有一个小问题。从 Form2 输入数据后,我不知道如何刷新Form1 :(左
我在使用函数 Form2 时尝试刷新> showData()但是程序崩溃了...我尝试了不同的方法,我也没有任何想法来解决这个问题。


感谢您的帮助或建议:)


表格1 : 

 Imports System.Data.OleDb 
Public Class Form1
Public dbconn As New OleDbConnection
Dim adt As New OleDbDataAdapter
Dim ds As New DataSet

Dim datatable As New DataTable
Dim cmd As New OleDbCommand

Private Sub Form1_Load(sender As Object,e As EventArgs)处理MyBase.Load
dbconn.ConnectionString =" provider = microsoft.jet.oledb.4.0; data source = key.mdb" ;
showData()'在datagridview中显示数据库值
Button1.Text =" Give"
Button2.Text ="返回"
End Sub
Public Sub showData()
Dim dbcommand As String
dbcommand =" SELECT * FROM keys"
adt =新OleDbDataAdapter(dbcommand,dbconn)
datatable =新DataTable
adt.Fill(datatable)
DataGridView1.DataSource = datatable
End Sub

Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click
Dim form As New Form2
form.Button3.Text =" Add"
dbconn.Dispose()
dbconn.Close()
form.ShowDialog()
End Sub

Private Sub Button2_Click(sender As Object,e作为EventArgs)处理Button2.Click
Dim form As New Form2(CInt(CStr(DataGridView1.CurrentRow.Cells(0).Value)))
form.Button3.Text =" Edit"
form.ComboBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
form.TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
form.TextBox2.Enabled = False
form.ComboBox1.Enabled = False
dbconn.Dispose()
dbconn.Close()

form.ShowDialog( )
End Sub

Private Sub Button3_Click(sender As Object,e As EventArgs)Handles Button3.Click
showData()
End Sub
End Class

和 表格2:

 Imports System.Data.OleDb 
Public Class Form2
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim sql As String
Dim myconn As OleDbConnection = New OleDbConnection

Private currentRowIdentifier As Integer

Public Sub New()
'this c一切都是设计师的要求。
InitializeComponent()

'在InitializeComponent()调用后添加任何初始化。
结束子

公共ReadOnly属性CurrentId作为整数
获取
返回currentRowIdentifier
结束获取
结束物业
Public Sub New (ByVal pIdentifier As Integer)

'设计师需要此调用。
InitializeComponent()

'在InitializeComponent()调用后添加任何初始化。
currentRowIdentifier = pIdentifier
End Sub


Private Sub Button2_Click(sender As Object,e As EventArgs)处理Button2.Click
myconn.Close()
Me.Close()
End Sub

Private Sub Form2_Load(sender As Object,e As EventArgs)Handles MyBase.Load
ComboBox1.Items.Add(" ; Office1")
ComboBox1.Items.Add(" Office2")
ComboBox1.Items.Add(" Key1")
Label1.Text =" Key"
Label2.Text =" Give"
Label3.Text =" Name"
Label4.Text ="返回"
TextBox0.Visible = False
End Sub
Sub Add()
Dim main As New Form1
provider =" provider = microsoft.jet.oledb.4.0; data source = key.mdb"
connString = provider& dataFile
myconn.ConnectionString = connString
myconn.Open()
Dim str As String
str ="插入键([1],[2],[3] ,[4])值(?,?,?,?)"
Dim cmd As OleDbCommand = New OleDbCommand(str,myconn)
cmd.Parameters.Add(New OleDbParameter(" key",CType(ComboBox1.Text,String)))
cmd。 Parameters.Add(New OleDbParameter("",CType(TextBox2.Text,String)))
cmd.Parameters.Add(New OleDbParameter(" name",CType(TextBox3.Text,String)))
cmd.Parameters.Add(New OleDbParameter(" return",CType(TextBox4.Text,String)))
尝试
cmd.ExecuteNonQuery()
cmd.Dispose( )
main.showData()
myconn.Close()
Catch ex As Exception
MessageBox.Show(" Error")
End Try
Me.Close()
End Sub
Sub Edit()
Dim main As New Form1

If String.IsNullOrWhiteSpace(myconn.ConnectionString)Then
我的conn.ConnectionString =" provider = microsoft.jet.oledb.4.0; data source = key.mdb"
结束如果
如果myconn.State = ConnectionState.Closed那么
myconn.Open()
结束如果

如果ComboBox1.Text<> ""和TextBox2.Text<> ""和TextBox3.Text<> ""和TextBox4.Text<> ""然后

sql =" UPDATE KEYS SET [1] = @ key,2 = @give,3 = @ name,4 = @ret WHERE id = @ id"

Dim cmd As OleDbCommand = New OleDbCommand(sql,myconn)
cmd.Parameters.AddWithValue(" @ key",ComboBox1.Text)
cmd.Parameters.AddWithValue( " @ give",TextBox2.Text)
cmd.Parameters.AddWithValue(" @ name",TextBox3.Text)
cmd.Parameters.AddWithValue(" @ ret",TextBox4.Text)
cmd.Parameters.AddWithValue(" @ id",currentRowIdentifier)

Dim Affected As Integer = 0
尝试
Affected = cmd.ExecuteNonQuery()
如果受影响= 1则
MessageBox.Show(" Updated")
Else
MessageBox.Show(" Updated failed")
End if
cmd .Dispose()
myconn.Close()
Me.Close( )
Catch ex As Exception
MessageBox.Show($" Error:{ex.Message}")
End Try
Else
MessageBox.Show(" ;空字段!")
结束如果
结束子

私有子按钮3_Click(发件人作为对象,e作为EventArgs)处理Button3.Click
如果Button3。文字="添加"然后
添加()
结束如果
如果Button3.Text ="编辑"然后
编辑()
结束如果
结束子
结束等级





解决方案

在Form2中,您创建了一个Form1的新实例 - 它与您的Form1实例不同开始于。 我假设你想将Form1的引用传递给Form2,以便它可以操作它。


只需给Form2一个属性:


公共类Form2

  公共财产RemoteForm为Form1

   ... ...
结束类


然后在创建新的Form2实例时设置此属性:

 Private Sub Button2_Click(sender As Object,e As EventArgs)处理Button2.Click 
Dim form As New Form2(CInt(CStr(DataGridView1.CurrentRow.Cells(0).Value)))
form.RemoteForm = Me
form.Button3.Text ="编辑"
form.ComboBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
form.TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
form.TextBox2.Enabled = False
form.ComboBox1.Enabled = False
dbconn.Dispose()
dbconn.Close()

form.ShowDialog( )
End Sub


然后在Form2中,只需参考RemoteForm,而不是尝试创建Form1的新实例。


Hello again,
i've a small problem. After entering the data from Form2 I don't know how to refresh Form1 :(
I tried to refresh when closing Form2 using function showData() but the program crash... I have tried different methods and I don't have any idea to solve this problem.
Thanks for any help or advises :)

Form 1 : 

Imports System.Data.OleDb
Public Class Form1
    Public dbconn As New OleDbConnection
    Dim adt As New OleDbDataAdapter
    Dim ds As New DataSet

    Dim datatable As New DataTable
    Dim cmd As New OleDbCommand

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dbconn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
        showData() 'show database values in datagridview
        Button1.Text = "Give"
        Button2.Text = "Return"
    End Sub
    Public Sub showData()
        Dim dbcommand As String
        dbcommand = "SELECT * FROM keys"
        adt = New OleDbDataAdapter(dbcommand, dbconn)
        datatable = New DataTable
        adt.Fill(datatable)
        DataGridView1.DataSource = datatable
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim form As New Form2
        form.Button3.Text = "Add"
        dbconn.Dispose()
        dbconn.Close()
        form.ShowDialog()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim form As New Form2(CInt(CStr(DataGridView1.CurrentRow.Cells(0).Value)))
        form.Button3.Text = "Edit"
        form.ComboBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
        form.TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
        form.TextBox2.Enabled = False
        form.ComboBox1.Enabled = False
        dbconn.Dispose()
        dbconn.Close()

        form.ShowDialog()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        showData()
    End Sub
End Class

and Form 2 :

Imports System.Data.OleDb
    Public Class Form2
        Dim provider As String
        Dim dataFile As String
        Dim connString As String
        Dim sql As String
        Dim myconn As OleDbConnection = New OleDbConnection

        Private currentRowIdentifier As Integer

        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
        End Sub

        Public ReadOnly Property CurrentId As Integer
            Get
                Return currentRowIdentifier
            End Get
        End Property
        Public Sub New(ByVal pIdentifier As Integer)

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            currentRowIdentifier = pIdentifier
        End Sub


        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            myconn.Close()
            Me.Close()
        End Sub

        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add("Office1")
            ComboBox1.Items.Add("Office2")
            ComboBox1.Items.Add("Key1")
            Label1.Text = "Key"
            Label2.Text = "Give"
            Label3.Text = "Name"
            Label4.Text = "Return"
            TextBox0.Visible = False
        End Sub
        Sub Add()
            Dim main As New Form1
            provider = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
            connString = provider & dataFile
            myconn.ConnectionString = connString
            myconn.Open()
            Dim str As String
            str = "Insert into keys([1], [2], [3], [4]) Values (?,?,?,?)"
            Dim cmd As OleDbCommand = New OleDbCommand(str, myconn)
            cmd.Parameters.Add(New OleDbParameter("key", CType(ComboBox1.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("give", CType(TextBox2.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("name", CType(TextBox3.Text, String)))
            cmd.Parameters.Add(New OleDbParameter("return", CType(TextBox4.Text, String)))
            Try
                cmd.ExecuteNonQuery()
                cmd.Dispose()
                main.showData()
                myconn.Close()
            Catch ex As Exception
                MessageBox.Show("Error")
            End Try
            Me.Close()
        End Sub
        Sub Edit()
            Dim main As New Form1

            If String.IsNullOrWhiteSpace(myconn.ConnectionString) Then
                myconn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=key.mdb"
            End If
            If myconn.State = ConnectionState.Closed Then
                myconn.Open()
            End If

            If ComboBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" Then

                sql = "UPDATE KEYS SET [1] = @key, 2 = @give, 3 = @name, 4 = @ret WHERE id = @id"

                Dim cmd As OleDbCommand = New OleDbCommand(sql, myconn)
                cmd.Parameters.AddWithValue("@key", ComboBox1.Text)
                cmd.Parameters.AddWithValue("@give", TextBox2.Text)
                cmd.Parameters.AddWithValue("@name", TextBox3.Text)
                cmd.Parameters.AddWithValue("@ret", TextBox4.Text)
                cmd.Parameters.AddWithValue("@id", currentRowIdentifier)

                Dim Affected As Integer = 0
                Try
                    Affected = cmd.ExecuteNonQuery()
                    If Affected = 1 Then
                        MessageBox.Show("Updated")
                    Else
                        MessageBox.Show("Updated failed")
                    End If
                    cmd.Dispose()
                    myconn.Close()
                    Me.Close()
                Catch ex As Exception
                    MessageBox.Show($"Error: {ex.Message}")
                End Try
            Else
                MessageBox.Show("Empty fields!")
            End If
        End Sub

        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If Button3.Text = "Add" Then
                Add()
            End If
            If Button3.Text = "Edit" Then
                Edit()
            End If
        End Sub
    End Class



解决方案

In Form2 you create a new instance of Form1 - that won't be the same Form1 instance you started with.  I assume you want to pass a reference to Form1 into Form2 so that it can manipulate it.

Just give Form2 a property:

Public Class Form2
   Public Property RemoteForm As Form1
   ...
End Class

Then set this property when creating the new Form2 instances:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim form As New Form2(CInt(CStr(DataGridView1.CurrentRow.Cells(0).Value)))
        form.RemoteForm = Me
        form.Button3.Text = "Edit"
        form.ComboBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString()
        form.TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value.ToString()
        form.TextBox2.Enabled = False
        form.ComboBox1.Enabled = False
        dbconn.Dispose()
        dbconn.Close()

        form.ShowDialog()
    End Sub

Then in Form2 just refer to RemoteForm instead of trying to create a new instance of Form1.


这篇关于从form2输入数据后刷新form1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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