私は1つのモジュールと7のフォームを持っていますvb.net - 次の "with"ステートメントをモジュールでシンプルにするには?
Form1、form2、form3以外のすべてのコードを複製しているのがわかりますが、これをwith文で簡単にする方法はありますか?
Module ModuleMessageCenter
Public Sub MessageCenterDatagrid()
Try
Call ConnectDatabase()
Dim adapter As New MySqlDataAdapter("SELECT IdMessage, Subject, Description, Priority, DateReceived, Deadline, OFR, Attachment FROM message ORDER BY IdMessage DESC", sqlcon)
Dim table As New DataTable()
adapter.Fill(table)
'>>>> how to make the following "with" statements simple? <<<<
With form1
.MessageDataGridView.DataSource = table
End With
With form2
.MessageDataGridView.DataSource = table
End With
With form3
.MessageDataGridView.DataSource = table
End With
Catch 'er As Exception
'MsgBox(er.Message)
End Try
End Sub
あなたはイムをForm1、Form2を、たForm3を除くすべてのコードを複製見ることができるように、with文のため、これは簡単にする方法がありますか?あなたはイム・モジュールでのForm1、Form2を、たForm3を除くすべてのコードを複製見ることができるように< < < <
Public Sub GridValues()
With form1
.lblIdMessage.Text = .MessageDataGridView.Item("IdMessage", .MessageDataGridView.CurrentRow.Index).Value.ToString
.SubjectTextBox.Text = .MessageDataGridView.Item("Subject", .MessageDataGridView.CurrentRow.Index).Value.ToString 'in the place of column name u can also place the column index .
.DescriptionTextBox.Text = .MessageDataGridView.Item("Description", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboPriotity.Text = .MessageDataGridView.Item("Priority", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDateReceived.Text = .MessageDataGridView.Item("DateReceived", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDeadLine.Text = .MessageDataGridView.Item("Deadline", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboOFR.Text = .MessageDataGridView.Item("OFR", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtAttach.Text = .MessageDataGridView.Item("Attachment", .MessageDataGridView.CurrentRow.Index).Value.ToString
End With
With form2
.lblIdMessage.Text = .MessageDataGridView.Item("IdMessage", .MessageDataGridView.CurrentRow.Index).Value.ToString
.SubjectTextBox.Text = .MessageDataGridView.Item("Subject", .MessageDataGridView.CurrentRow.Index).Value.ToString 'in the place of column name u can also place the column index .
.DescriptionTextBox.Text = .MessageDataGridView.Item("Description", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboPriotity.Text = .MessageDataGridView.Item("Priority", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDateReceived.Text = .MessageDataGridView.Item("DateReceived", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDeadLine.Text = .MessageDataGridView.Item("Deadline", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboOFR.Text = .MessageDataGridView.Item("OFR", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtAttach.Text = .MessageDataGridView.Item("Attachment", .MessageDataGridView.CurrentRow.Index).Value.ToString
End With
With form3
.lblIdMessage.Text = .MessageDataGridView.Item("IdMessage", .MessageDataGridView.CurrentRow.Index).Value.ToString
.SubjectTextBox.Text = .MessageDataGridView.Item("Subject", .MessageDataGridView.CurrentRow.Index).Value.ToString 'in the place of column name u can also place the column index .
.DescriptionTextBox.Text = .MessageDataGridView.Item("Description", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboPriotity.Text = .MessageDataGridView.Item("Priority", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDateReceived.Text = .MessageDataGridView.Item("DateReceived", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtDeadLine.Text = .MessageDataGridView.Item("Deadline", .MessageDataGridView.CurrentRow.Index).Value.ToString
.ComboOFR.Text = .MessageDataGridView.Item("OFR", .MessageDataGridView.CurrentRow.Index).Value.ToString
.txtAttach.Text = .MessageDataGridView.Item("Attachment", .MessageDataGridView.CurrentRow.Index).Value.ToString
End With
End Sub
、声明を持つため、これは簡単にする方法がありますか?あなたは、オブジェクトのいくつかのプロパティを設定しているとき
Public Sub MessageCenterDownloadFile()
With form1
Try
Dim SaveFileToFolder As New SaveFileDialog
SaveFileToFolder.FileName = .SubjectTextBox.Text
If SaveFileToFolder.ShowDialog().Equals(DialogResult.OK) Then
Dim Cmd As New MySqlCommand("SELECT AttachmentFile FROM message WHERE IdMessage =" & .lblIdMessage.Text, sqlcon)
Dim fileBytes() As Byte = DirectCast(Cmd.ExecuteScalar, Byte())
'IO.File.WriteAllBytes(.SubjectTextBox.Text + .txtAttach.Text, fileBytes) 'IO.File.WriteAllBytes("path", fileBytes)
My.Computer.FileSystem.WriteAllBytes(SaveFileToFolder.FileName + .txtAttach.Text, fileBytes, True)
MessageBox.Show("Download Successful!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Call MessageCenterDatagrid() 'REFRESH DATA
End If
Catch
End Try
End With
With form2
Try
Dim SaveFileToFolder As New SaveFileDialog
SaveFileToFolder.FileName = .SubjectTextBox.Text
If SaveFileToFolder.ShowDialog().Equals(DialogResult.OK) Then
Dim Cmd As New MySqlCommand("SELECT AttachmentFile FROM message WHERE IdMessage =" & .lblIdMessage.Text, sqlcon)
Dim fileBytes() As Byte = DirectCast(Cmd.ExecuteScalar, Byte())
'IO.File.WriteAllBytes(.SubjectTextBox.Text + .txtAttach.Text, fileBytes) 'IO.File.WriteAllBytes("path", fileBytes)
My.Computer.FileSystem.WriteAllBytes(SaveFileToFolder.FileName + .txtAttach.Text, fileBytes, True)
MessageBox.Show("Download Successful!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Call MessageCenterDatagrid() 'REFRESH DATA
End If
Catch
End Try
End With
With form3
Try
Dim SaveFileToFolder As New SaveFileDialog
SaveFileToFolder.FileName = .SubjectTextBox.Text
If SaveFileToFolder.ShowDialog().Equals(DialogResult.OK) Then
Dim Cmd As New MySqlCommand("SELECT AttachmentFile FROM message WHERE IdMessage =" & .lblIdMessage.Text, sqlcon)
Dim fileBytes() As Byte = DirectCast(Cmd.ExecuteScalar, Byte())
'IO.File.WriteAllBytes(.SubjectTextBox.Text + .txtAttach.Text, fileBytes) 'IO.File.WriteAllBytes("path", fileBytes)
My.Computer.FileSystem.WriteAllBytes(SaveFileToFolder.FileName + .txtAttach.Text, fileBytes, True)
MessageBox.Show("Download Successful!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Call MessageCenterDatagrid() 'REFRESH DATA
End If
Catch
End Try
End With
End Subの
End module
)MessageCenterDownloadFileを(IS 1つのプロパティを設定するだけであれば、それを使う必要はありませんか? – Plutonix
それを行う方法サー? –
'With'ブロックのポイントは、同じオブジェクトの複数のアクセスを単純化することです。あなたの場合は、同じオブジェクトにアクセスしていないので、これは 'With'ブロックを使用する状況ではありません。あなたが望むかもしれないという事実はそれを論理的または妥当なものにしません。 – jmcilhinney