2017-12-15 35 views
0

私は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 
+0

)MessageCenterDownloadFileを(IS 1つのプロパティを設定するだけであれば、それを使う必要はありませんか? – Plutonix

+0

それを行う方法サー? –

+1

'With'ブロックのポイントは、同じオブジェクトの複数のアクセスを単純化することです。あなたの場合は、同じオブジェクトにアクセスしていないので、これは 'With'ブロックを使用する状況ではありません。あなたが望むかもしれないという事実はそれを論理的または妥当なものにしません。 – jmcilhinney

答えて

1

Withは通常使用されています。ただ、直接プロパティにアクセス:

form1.MessageDataGridView.DataSource = table 
form2.MessageDataGridView.DataSource = table 
form3.MessageDataGridView.DataSource = table 
+0

私はいくつかのプロパティと長いコードをwithステートメントと一緒に持っています。 Form1の 有するモジュール パブリックサブMessageCenterAdd() 内部 –

+0

.SubjectTextBox.Clear() .DescriptionTextBox.Clear() .ComboPriotity.ResetText() .txtDateReceived.Clear() .ComboOFR.ResetText() 。 End Subの でtxtAttach.Clear() .SubjectTextBox.Select() .DTPickerDateReceived.ResetText() .DTPickerDeadLine.ResetText() .txtDeadLine.Clear() 終了私は7は、 –

0

は、パラメータとしてフォームを受け入れる関数を作成し、その中で同じ

Public Sub GridValues() 
    GridValuesFor(form1) 
    GridValuesFor(form2) 
    GridValuesFor(form3) 
End Sub 

Public Sub GridValuesFor(ByVal thisForm As Form) 
    With thisForm 
     .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 

複製するコードを書く -

+0

こんにちはを持っています先生.lblIdMessage.Textはsystem.windows.forms.formのメンバーではないというエラーがあります。 –

+2

このコードを動作させるには、 'Option Strict Off'を設定するというlate-bindingを有効にする必要があります。あなたは1つのファイルのためだけにそれを行うことができますが、私はこれをお勧めしません。より良い選択肢は、この共通の機能をすべて基底フォームクラスに入れ、それを他の3つのフォームで継承することです。これらのフォームの1つでメソッドを呼び出すだけで、UI自体を変更するときの作業方法がわかります。 – jmcilhinney

+0

Monkey D Luffyは、この基底クラスからすべてのフォームを継承して継承し、このクラスの関数パラメータのタイプも定義します –

関連する問題