Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created November 2, 2020 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/c331742f5c05798d828f4259870dcb31 to your computer and use it in GitHub Desktop.
Save dj1711572002/c331742f5c05798d828f4259870dcb31 to your computer and use it in GitHub Desktop.
VB.NET Matrix_Calculator using ImagingSolution.Mat.dll_https://imagingsolution.net/program/csharp/dotnet_matrix_class/
Imports Microsoft.VisualBasic.FileIO
Imports System.Text
Imports System.IO
Imports ImagingSolution 'ImagingSolution.Matクラス用
Public Class Form1
Public i, j, k As Integer
'=================MATRIX A=================
Public matA_rowN As Integer
Public matA_colN As Integer
Public matA(,) As Double
Public RmatA(,) As Double
'=================MATRIX B=================
Public matB_rowN As Integer
Public matB_colN As Integer
Public matB(,) As Double
Public RmatB(,) As Double
'=============matA とmatBの計算=============
Public RmatAB(,) As Double
'================Data Set============
Public ds As DataSet
Public dsFlag As Integer = 0
'======================DataTable &DGV Parameters ========================
Public dt() As DataTable
Public dtRow As DataRow '型宣言
Public dt_chname() As String
Public chHead() As String
Public selectdt As Integer = 0 'dt ComboBox selected Index
Public selectdt_1 As Integer = 1 'dt ComboBox selected Index
Public copyCol() As String
Public copyRow() As String
Public copyHead As String
Public copyCols(,) As String
Public copyRows(,) As String
Public copyHeads() As String
'=============CSV Save======================
Public headerFlg As Integer = 0
Public file As System.IO.StreamWriter
Public file1 As System.IO.StreamWriter
Public Tfile1 As System.IO.StreamWriter
Public readata(10000, 10000) As String
Public cIndex, cIndex_max As Integer
'====FILE Headers =============================
Public fname As String
Public chname(17) As String
Public wakuch(17) As String
Public wakuN(6) As String
'===============DataGridView用パラメータ==========
Public Path As String '= "D:\Temp\TestData.csv" 'CsvファイルのPathを指定
Public Path_1 As String '1個前のファイル名
Public kaishiKeta As Integer = 1 ' データ配列書き込み開始列数
'==================Data Array========================
Public rIndex As Integer
Public kajyuN As Integer
'waku1,2,3(荷重No,結果No)=correl,slope,intercept,X軸値mV、Y軸値mV
'chdata(荷重No,CHNo)=CH1-CH16
Public waku1(200, 7) As Double
Public waku2(200, 7) As Double
Public waku3(200, 7) As Double
Public chdata(200, 17) As Double
Public dgRow(17) As Double
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
End Sub
'====================Current Cell 座標表示==============================
Private Sub dgv1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv1.CellClick
''現在のセルの行インデックスを表示
TextBox1.Text = dgv1.CurrentCell.RowIndex
'現在のセルの列インデックスを表示
TextBox2.Text = dgv1.CurrentCell.ColumnIndex
End Sub
'=============DataGridView Size 表示===================================
Private Sub maxRowCol()
Label10.Text = "DataRow_" & dgv1.RowCount & "行" & dgv1.ColumnCount & "列"
End Sub
'=========================単列or単行コピー=================================================
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Clipboard.SetDataObject(dgv1.GetClipboardContent())
Dim cX As Integer = dgv1.CurrentCell.ColumnIndex
Dim cY As Integer = dgv1.RowCount
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
ReDim copyCol(cY)
ReDim copyRow(rX)
'-------------列コピー-----------------------------
If RadioButton1.Checked = True Then
copyHead = dgv1.Columns(cX).HeaderText
For i = 0 To cY - 1
copyCol(i) = CStr(dgv1.Rows(i).Cells(cX).Value)
Next i
Label6.Text = "CopiedColNo" & cX
End If
'------------行コピー-------------------------------
If RadioButton2.Checked = True Then
For i = 0 To rX - 1
copyRow(i) = CStr(dgv1.Rows(rY).Cells(i).Value)
Next i
Label6.Text = "CopiedRowNo" & rY
End If
End Sub
'=========================単列or単行 上書きペースト==============================================
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If Label6.Text <> "" Then 'COPY 在り
'-------------列ペースト-----------------------------
If RadioButton1.Checked = True Then
Dim cX As Integer = dgv1.CurrentCell.ColumnIndex
Dim cY As Integer = dgv1.RowCount
dgv1.Columns(cX).HeaderText = copyHead
For i = 0 To dgv1.Rows.Count - 1
dgv1.Rows(i).Cells(cX).Value = copyCol(i)
Next i
Label6.Text = "PastedColNo" & cX
End If
'------------行ペースト-------------------------------
If RadioButton2.Checked = True Then
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
For i = 0 To rX - 1
dgv1.Rows(rY).Cells(i).Value = copyRow(i)
Next i
Label6.Text = "PastedRowNo" & rY
End If
End If
maxRowCol()
End Sub
'=====================列or行削除Remove Columns of Current Cursor====
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
'-------------列消去-----------------------------
If RadioButton1.Checked = True Then
dgv1.Columns.RemoveAt(dgv1.CurrentCell.ColumnIndex)
Label6.Text = "Deleted:" & dgv1.CurrentCell.ColumnIndex
End If
'------------行消去-------------------------------
If RadioButton2.Checked = True Then
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
dgv1.Rows.RemoveAt(rY)
Label6.Text = "Deleted RowNo" & rY
End If
maxRowCol()
End Sub
'====================列挿入 現在列以降の前列をコピーして1列追加してペーストして現在列を空にする============
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim cmax As Integer = dgv1.ColumnCount
Dim cX As Integer = dgv1.CurrentCell.ColumnIndex
Dim cY As Integer = dgv1.RowCount
ReDim copyCols(cY, cmax)
ReDim copyHeads(cmax)
'-------------列挿入-----------------------------
If RadioButton1.Checked = True Then
copyHead = dgv1.Columns(cX).HeaderText
'-----------移動行列コピー--------------------------------
For i = 0 To cY - 1
For j = 0 To cmax - cX - 1
copyHeads(j) = dgv1.Columns(j + cX).HeaderText
copyCols(i, j) = CStr(dgv1.Rows(i).Cells(j + cX).Value)
Next j
Next i
Label6.Text = "CopiedCols=" & cX & "-" & cmax
'----------------列追加-----------------------------------
Dim colName As New DataGridViewTextBoxColumn()
colName.Name = "dummy"
colName.HeaderText = "dummy"
dgv1.Columns.Add(colName)
cmax = dgv1.ColumnCount
'-----------------列ペースト---------------
For i = 0 To cY - 1
For j = 0 To cmax - cX - 2
dgv1.Columns(j + cX + 1).HeaderText = copyHeads(j)
dgv1.Rows(i).Cells(j + cX + 1).Value = copyCols(i, j)
Next j
Next i
For i = 0 To cY - 1
dgv1.Rows(i).Cells(cX).Value = 0
Next i
End If
'------------行挿入-------------------------------
If RadioButton2.Checked = True Then
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
dgv1.Rows.Insert(rY)
End If
maxRowCol()
End Sub
'========================================================================
'==================DataTableをdgv1に表示==================================
'========================================================================
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
'------dgv1 Clear--------------
dgv1.Columns.Clear()
dgv1.DataSource = Nothing
selectdt = ComboBox1.SelectedIndex
If selectdt_1 <> selectdt Then
selectdt_1 = selectdt
dgv1.Columns.Clear()
dgv1.DataSource = DataSet1.Tables(selectdt) 'selectdt
Label3.Text = "DataTable:" & ComboBox1.SelectedItem
End If
' 最終行のインデックスを取得する
'Dim idx As Integer = dgv1.RowCount - 1
'' DataGridView の最終行を削除(インデックス指定)
'dgv1.Rows.RemoveAt(idx)
maxRowCol()
End Sub
'===============================================================================
' DataTable Make
'DGVから読み込んでDataTable化する
'===============================================================================
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'---------dgv1 サイズ表示-------
TextBox4.Text = dgv1.Columns.Count
TextBox5.Text = dgv1.Rows.Count
'----------------------------------
File_matrix()
End Sub
'==================================================================================
'DataGridView全データを CSV保存
'=================================================================================
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim f1open As Integer
Dim fname1 As String
Dim fStr1 As String
Dim fStr2 As String
f1open = 0
'========First CSV File Declaration======================
If f1open = 0 Then
fname1 = Format(Now, "yyyyMMdd_HHmmss")
fStr1 = "" 'ComboBox9.SelectedItem.ToString()
fname1 = "C:\vb_LOG\" & fStr1 & fname1 & ".csv"
fStr2 = "FileOpen:" + fname1
'------------------------------------------
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding(“Shift_JIS”)
file1 = My.Computer.FileSystem.OpenTextFileWriter(fname1, False, enc)
f1open = 1
'---------定義されたサイズ全体を保存する--------------------
For i = 0 To dgv1.RowCount - 1
For j = 0 To dgv1.ColumnCount - 1
Dim sdata = CStr(dgv1.Rows(i).Cells(j).Value)
If sdata = "" Then
Exit For
End If
If j < dgv1.Columns.Count - 1 Then
sdata = sdata & ","
End If
file1.Write(sdata)
Next j
file1.Write(vbCrLf)
Next i
file1.Close()
End If
End Sub
'=================FILE OPEN FIleReading DGVへ自動追記========================
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CSV_DGV()
End Sub
'==========================================================================
'==============--一般のCSVファイル読み込みDGV===================================
'===================ヘッダの有無は、最初変数がが数値か文字かで判定============================
Private Sub CSV_DGV()
Dim datasu As Integer
Dim counter As Integer
Dim maxRowCSV As Integer
'---------dgv1 サイズ表示-------
TextBox4.Text = dgv1.Columns.Count
TextBox5.Text = dgv1.Rows.Count
'----------------------------------
If CheckBox1.Checked = True Then
'-----dgv1に表示------------
'------dgv1初期化--------------
dgv1.Columns.Clear()
dgv1.DataSource = Nothing
'------File名Path-------------------
Path = readfile() 'Dialogから読み込むファイル名指定
Label3.Text = Path
'-------ファイルの行数----------------
maxRowCSV = GetLinesOfTextFile(Path)
Label1.Text = CStr(maxRowCSV) & "行"
'--------CSVァイル読み込み-----------------
' Label2.Text = "CSV File Reading"
datasu = CSV_crlf(Path) 'ファイルの改行数なのでHeader行も含まれる行数なのでRowではない
'Label2.Text = "CSV File Read Finished"
'--------dgv1 行数設定--------------------
If IsNumeric(readata(0, 0)) = False Then 'ヘッダーあり
Label15.Text = "ヘッダーあり"
dgv1.RowCount = datasu - 1 'ヘッダー行を差し引いてDGVの行を作る
Debug.Print("cIndex_max=" & CStr(cIndex_max))
dgv1.ColumnCount = cIndex_max
headerFlg = 1
End If
If IsNumeric(readata(0, 0)) = True Then 'ヘッダーなし
Label15.Text = "ヘッダーなし"
dgv1.RowCount = datasu 'データ数分DGVの行を作る
Debug.Print("cIndex_max=" & CStr(cIndex_max))
dgv1.ColumnCount = cIndex_max
headerFlg = 0
End If
'========================Header Col作成=================
If IsNumeric(readata(0, 0)) = False Then 'ヘッダーがある場合
For j = 0 To cIndex_max - 1
dgv1.Columns(j).HeaderText = readata(0, j) 'dgv1の列Header Property定義
Next
End If
'=========CSVから全データ配列 redata(maxRowCSV,cIndex)==========
rIndex += 1
For i = 0 To dgv1.RowCount - 1
For j = 0 To dgv1.ColumnCount - 1 '1その行の列数は、readata(行,0)に格納されている
'Debug.Print("DGV_rdata(" & CStr(i) & "," & CStr(j) & ")=" & CStr(readata(i, j)))
dgv1.Rows(i).Cells(j).Value = readata(i + headerFlg, j)
Next j
Next i
End If
maxRowCol()
End Sub
Private Function readfile() As String
Dim ofd As New OpenFileDialog()
'はじめのファイル名を指定する
'はじめに「ファイル名」で表示される文字列を指定する
ofd.FileName = "yyyyMMdd_HHmmss" '"default.html"
'はじめに表示されるフォルダを指定する
'指定しない(空の文字列)の時は、現在のディレクトリが表示される
ofd.InitialDirectory = "C:\vb_DT "
'[ファイルの種類]に表示される選択肢を指定する
'指定しないとすべてのファイルが表示される
ofd.Filter = "TXTファイル|*.txt|CSVファイル|*.csv|すべてのファイル|*.*"
'[ファイルの種類]ではじめに選択されるものを指定する
'2番目の「すべてのファイル」が選択されているようにする
ofd.FilterIndex = 2
'タイトルを設定する
ofd.Title = "開くファイルを選択してください"
'ダイアログボックスを閉じる前に現在のディレクトリを復元するようにする
ofd.RestoreDirectory = True
'存在しないファイルの名前が指定されたとき警告を表示する
'デフォルトでTrueなので指定する必要はない
ofd.CheckFileExists = True
'存在しないパスが指定されたとき警告を表示する
'デフォルトでTrueなので指定する必要はない
ofd.CheckPathExists = True
If ofd.ShowDialog() = DialogResult.OK Then
'OKボタンがクリックされたとき、選択されたファイル名を表示する
Debug.Print(ofd.FileName)
End If
Return ofd.FileName
End Function
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
'------dgv1 Clear--------------
dgv1.Columns.Clear()
dgv1.DataSource = Nothing
End Sub
Private Sub Label7_Click(sender As Object, e As EventArgs) Handles Label7.Click
End Sub
'==============================================================================================
'==============================================================================================
'==============================================================================================
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
rIndex = 0 ' DGVの初期行番号
End Sub
'==========================================================================================
'===================CSVファイル読み込み CrLf付 FILE Read Function=================================================
'======================================================================================
Private Function CSV_crlf(ByVal fnm As String) As Integer '戻り値はデータ数
Dim rdataN As Integer
Using parser As New TextFieldParser(fnm, System.Text.Encoding.GetEncoding("Shift_JIS"))
parser.TextFieldType = FieldType.Delimited
parser.SetDelimiters(",") ' 区切り文字はコンマ
' parser.HasFieldsEnclosedInQuotes = False
' parser.TrimWhiteSpace = False
cIndex = 0
cIndex_max = 0
rdataN = 0
While Not parser.EndOfData
Dim row As String() = parser.ReadFields() ' 1行読み込み
For Each field As String In row
readata(rdataN, cIndex) = field
cIndex += 1
' Debug.Print("field=" & field)
Debug.Print("readata(" & CStr(rdataN) & "," & CStr(cIndex) & ")=" & CStr(readata(rdataN, cIndex)))
Next
If cIndex > cIndex_max Then
cIndex_max = cIndex
End If
cIndex = 0
rdataN += 1
End While
End Using
Return rdataN
End Function
'============================CSVファイル 総行数カウントFunction==============================
Public Function GetLinesOfTextFile(ByVal FileName As String) As Integer
Dim StReader As New System.IO.StreamReader(FileName)
Dim LineCount As Integer
While (StReader.Peek() >= 0)
StReader.ReadLine()
LineCount += 1
End While
Return LineCount
End Function
'******************************************************************************************************************************
'******************************************************************************************************************************
' マトリックス計算 ブロック
'******************************************************************************************************************************
'******************************************************************************************************************************
'==============================================================================================
'=================csv ファイルを読んでdgv1に書いmatA配列を作る=================================================
'==============================================================================================
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
dgv1.Columns.Clear()
'-----MatA file read -----------------------------
CSV_DGV() 'Sub ファイルを読んで、dgv1に書き込む
Label9.Text = Label3.Text '読み込んだFile 名
matA_rowN = dgv1.Rows.Count - 1
matA_colN = dgv1.Columns.Count - 1
'---set dgv1------------------
dgv1.RowCount = matA_rowN + 1
dgv1.ColumnCount = matA_colN + 1
Dim matAstr As String
If dgv1.Rows(0).Cells(matA_colN).Value = "" Then
matA_colN = matA_colN - 1
End If
Label13.Text = CStr(matA_rowN + 1) & "x" & CStr(matA_colN + 1) & ":" & "matA(" & CStr(matA_rowN) & "," & CStr(matA_colN) & ")"
'-------matA(,) Making--------------------------------------------------
ReDim matA(matA_rowN, matA_colN)
For i = 0 To matA_rowN
For j = 0 To matA_colN
If dgv1.Rows(i).Cells(j).Value <> "" Then
matA(i, j) = dgv1.Rows(i).Cells(j).Value
matAstr = "matA(" & CStr(i) & "," & CStr(j) & ")=" & CStr(matA(i, j)) & vbCrLf
RichTextBox1.AppendText(matAstr)
matAstr = ""
End If
Next j
Next i
End Sub
'==============================================================================================
'=================マトリックスB ファイルを読み込み==================================================
'==============================================================================================
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
CSV_DGV()
Label12.Text = Label3.Text
matB_rowN = dgv1.Rows.Count - 1
matB_colN = dgv1.Columns.Count - 1
Dim matBstr As String
If dgv1.Rows(0).Cells(matB_colN).Value = "" Then
matB_colN = matB_colN - 1
End If
Label14.Text = CStr(matB_rowN + 1) & "x" & CStr(matB_colN + 1) & ":" & "matB(" & CStr(matB_rowN) & "," & CStr(matB_colN) & ")"
'-------matB(,) Making--------------------------------------------------
ReDim matB(matB_rowN, matB_colN)
For i = 0 To matB_rowN
For j = 0 To matB_colN
matB(i, j) = dgv1.Rows(i).Cells(j).Value
matBstr = "matB(" & CStr(i) & "," & CStr(j) & ")=" & CStr(matB(i, j)) & vbCrLf
RichTextBox2.AppendText(matBstr)
matBstr = ""
Next j
Next i
End Sub
'=========================行列計算A================================================
'========================== matA 計算===============================================
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
Dim matname As String
ReDim chHead(dgv1.ColumnCount)
If RadioButton3.Checked = True Then ' 逆行列
matname = "Invs_A_" & Label9.Text.Substring(9, 6)
End If
If RadioButton4.Checked = True Then ' 転置
matname = "Trps_A_" & Label9.Text.Substring(9, 6)
End If
'=================Tfile1 Saving Preparation======================
Dim Tfname1 = Format(Now, "MMdd_HHmmss")
Dim TfStr1 = matname 'Making Data Table Name
Tfname1 = "C:\vb_DT\" & TfStr1 & Tfname1 & ".csv"
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding(“Shift_JIS”)
Tfile1 = My.Computer.FileSystem.OpenTextFileWriter(Tfname1, False, enc)
Dim Tf1open = 1
'---------逆行列--------------------------
If RadioButton3.Checked = True Then '逆行列
ReDim RmatA(matA_rowN, matA_colN)
Dim matAstr As String
Dim xoff As Integer = matA_colN + 1
Dim addHeader(matA_colN) As String
'---結果表示はdgv1右横2行空け分列拡張----------
dgv1.ColumnCount = (matA_colN + 1) * 2 + xoff
'---------------Header追加--------------------
For j = 0 To matA_colN
addHeader(j) = "Inv_A_" & CStr(j)
Tfile1.Write(addHeader(j))
If j < matA_colN Then
Tfile1.Write(",")
End If
dgv1.Columns(j + xoff).HeaderText = addHeader(j) 'ヘッダー追加表示
Next j
Tfile1.Write(vbCrLf) ' header改行
'----Mtrix Inverse------------------
RmatA = matA.Inverse()
'=====表示===========================================
For i = 0 To matA_rowN
For j = 0 To matA_colN
dgv1.Rows(i).Cells(j + xoff).Value = RmatA(i, j)
Tfile1.Write(RmatA(i, j))
If j < matA_colN Then
Tfile1.Write(",")
End If
matAstr = "Inv_A(" & CStr(i) & "," & CStr(j) & ")=" & CStr(RmatA(i, j)) & vbCrLf
RichTextBox1.AppendText(matAstr)
matAstr = ""
Next j
Tfile1.Write(vbCrLf)
Next i
Tfile1.Close()
End If
'-------------------転置--------------------------
If RadioButton4.Checked = True Then '逆行列
ReDim RmatA(matA_colN, matA_rowN)
Dim matAstr As String
Dim xoff As Integer = matA_colN + 1
Dim addHeader(matA_rowN) As String
'---結果表示はdgv1右横2行空け分列拡張----------
dgv1.ColumnCount = (matA_rowN + 1) * 2 + xoff
'---------------Header追加--------------------
For j = 0 To matA_rowN
addHeader(j) = "Inv_A_" & CStr(j)
Tfile1.Write(addHeader(j))
If j < matA_rowN Then
Tfile1.Write(",")
End If
dgv1.Columns(j + xoff).HeaderText = addHeader(j) 'ヘッダー追加表示
Next j
Tfile1.Write(vbCrLf) ' header改行
'---------------計算転置-------------------------------
RmatA = matA.Transpose()
'------結果表示----------------------
For i = 0 To matA_colN
For j = 0 To matA_rowN
dgv1.Rows(i).Cells(j + xoff).Value = RmatA(i, j)
Tfile1.Write(RmatA(i, j))
If j < matA_rowN Then
Tfile1.Write(",")
End If
matAstr = "Trps_A(" & CStr(i) & "," & CStr(j) & ")=" & CStr(RmatA(i, j)) & vbCrLf
RichTextBox1.AppendText(matAstr)
matAstr = ""
Next j
Tfile1.Write(vbCrLf)
Next i
Tfile1.Close()
End If
End Sub
Private Sub File_matrix()
Dim matname As String = TextBox3.Text
ReDim chHead(dgv1.ColumnCount)
If TextBox3.Text = "" Then
matname = Label3.Text.Substring(9, 6) & "d"
End If
'=================Tfile1 Saving Preparation======================
Dim Tfname1 = Format(Now, "MMdd_HHmmss")
Dim TfStr1 = matname 'Making Data Table Name
Tfname1 = "C:\vb_DT\" & TfStr1 & Tfname1 & ".csv"
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding(“Shift_JIS”)
Tfile1 = My.Computer.FileSystem.OpenTextFileWriter(Tfname1, False, enc)
Dim Tf1open = 1
'==================ComboBoxa に登録===================
ComboBox1.Items.Add(TfStr1 & Tfname1)
Label16.Text = "Saved " & TfStr1 & Tfname1
'--------Rows.Count Check-----------------------------
Debug.Print("File:dgv1.Rows.Count=" & CStr(dgv1.Rows.Count))
'-------------------------------------------------------
'=================================================
'========Col Header name =======================
'--------Rows.Count Check-----------------------------
Debug.Print("header:dgv1.Rows.Count=" & CStr(dgv1.Rows.Count))
'-------------------------------------------------------
'================Header行保存=================
'------dgv1のヘッダー 0列目が数値でなかったらヘッダー文字ありと判断---------------
If IsNumeric(dgv1.Columns(1).HeaderText) = False Then
For j = 0 To dgv1.ColumnCount - 1 'chHead(1)から開始
chHead(j) = dgv1.Columns(j).HeaderText ' 最上部Header行の列名
Tfile1.Write(chHead(j))
If j < dgv1.ColumnCount - 1 Then
Tfile1.Write(",")
End If
Debug.Print("ColumnNo=" & CStr(j) & "chHead=" & chHead(j))
'------Header Writing to Tfile1 csv File------
Next j
End If
Tfile1.Write(vbCrLf)
'=================data 行保存===================================
'--------Rows.Count Check-----------------------------
Debug.Print("data:dgv1.Rows.Count=" & CStr(dgv1.Rows.Count))
'-------------------------------------------------------
For i = 0 To dgv1.Rows.Count - 1
For j = 0 To Me.dgv1.ColumnCount - 1 '追加した行へ値を書き込み
Dim matdata As String = dgv1.Rows(i).Cells(j).Value
Debug.Print("dgv1.Cells=(" & CStr(i) & "," & CStr(j) & ")=" & matdata)
'------Data Writing to Tfile1 csv File------
Tfile1.Write(matdata)
If j < dgv1.ColumnCount - 1 Then
Tfile1.Write(",")
End If
Next j
Tfile1.Write(vbCrLf)
Next i
'--------Rows.Count Check-----------------------------
Debug.Print("dataset:dgv1.Rows.Count=" & CStr(dgv1.Rows.Count))
' '-------------------------------------------------------
'DataSet1.Tables(0).Rows.Count
TextBox3.Text = ""
Tfile1.Close()
maxRowCol()
End Sub
Private Sub dgv1_MouseClick(sender As Object, e As MouseEventArgs) Handles dgv1.MouseClick
dgv1.Columns(1).HeaderCell.SortGlyphDirection = SortOrder.None
End Sub
'=========================行列計算B================================================
'========================== matB 計算===============================================
Private Sub Button13_Click_1(sender As Object, e As EventArgs) Handles Button13.Click
Dim matname As String = ""
ReDim chHead(dgv1.ColumnCount)
If RadioButton8.Checked = True Then ' 逆行列
matname = "Invs_B_" & Label12.Text.Substring(9, 6)
End If
If RadioButton9.Checked = True Then ' 転置
matname = "Trps_B_" & Label12.Text.Substring(9, 6)
End If
'=================Tfile1 Saving Preparation======================
Dim Tfname1 = Format(Now, "MMdd_HHmmss")
Dim TfStr1 = matname 'Making Data Table Name
Tfname1 = "C:\vb_DT\" & TfStr1 & Tfname1 & ".csv"
Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding(“Shift_JIS”)
Tfile1 = My.Computer.FileSystem.OpenTextFileWriter(Tfname1, False, enc)
Dim Tf1open = 1
'---------逆行列--------------------------
If RadioButton8.Checked = True Then '逆行列
ReDim RmatB(matB_rowN, matB_colN)
Dim matBstr As String
Dim xoff As Integer = matB_colN + 1
Dim addHeader(matB_colN) As String
'---結果表示はdgv1右横2行空け分列拡張----------
dgv1.ColumnCount = (matB_colN + 1) * 2 + xoff
'---------------Header追加--------------------
For j = 0 To matB_colN
addHeader(j) = "Inv_B_" & CStr(j)
Tfile1.Write(addHeader(j))
If j < matB_colN Then
Tfile1.Write(",")
End If
dgv1.Columns(j + xoff).HeaderText = addHeader(j) 'ヘッダー追加表示
Next j
Tfile1.Write(vbCrLf) ' header改行
'----Mtrix Inverse------------------
RmatB = matB.Inverse()
For i = 0 To matB_rowN
For j = 0 To matB_colN
dgv1.Rows(i).Cells(j + xoff).Value = RmatB(i, j)
Tfile1.Write(RmatB(i, j))
If j < matB_colN Then
Tfile1.Write(",")
End If
matBstr = "Inv_B(" & CStr(i) & "," & CStr(j) & ")=" & CStr(RmatB(i, j)) & vbCrLf
RichTextBox1.AppendText(matBstr)
matBstr = ""
Next j
Tfile1.Write(vbCrLf)
Next i
Tfile1.Close()
End If
'-------------------転置--------------------------
If RadioButton9.Checked = True Then '転置
ReDim RmatB(matB_colN, matB_rowN)
Dim matBstr As String
Dim xoff As Integer = matB_colN + 1
Dim addHeader(matB_rowN) As String
'---結果表示はdgv1右横2行空け分列拡張----------
dgv1.ColumnCount = (matB_rowN + 1) * 2 + xoff
'---------------Header追加--------------------
For j = 0 To matB_rowN
addHeader(j) = "Inv_B_" & CStr(j)
Tfile1.Write(addHeader(j))
If j < matB_rowN Then
Tfile1.Write(",")
End If
dgv1.Columns(j + xoff).HeaderText = addHeader(j) 'ヘッダー追加表示
Next j
Tfile1.Write(vbCrLf) ' header改行
'---------------計算転置-------------------------------
RmatB = matB.Transpose()
'----結果表示--------------------------------------
For i = 0 To matB_colN
For j = 0 To matB_rowN
dgv1.Rows(i).Cells(j + xoff).Value = RmatB(i, j)
Tfile1.Write(RmatB(i, j))
If j < matB_rowN Then
Tfile1.Write(",")
End If
matBstr = "Trps_B(" & CStr(i) & "," & CStr(j) & ")=" & CStr(RmatB(i, j)) & vbCrLf
RichTextBox1.AppendText(matBstr)
matBstr = ""
Next j
Tfile1.Write(vbCrLf)
Next i
Tfile1.Close()
End If
End Sub
'====================================================================================================
'============================matA matB計算============================================================
'====================================================================================================
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
'-------------------RmatA(,)とRmatB(,)が用意されていること-----------------------
'---matA_rowN,matA_colNとmatB_rown,matB_colNが整合していること
If RadioButton5.Checked = True Then '積
'積は、matAの列数とmatBの行数が一致
If matA_colN = matB_rowN Then
ReDim RmatAB(matA_rowN, matA_colN)
End If
Else
Label18.Text = "計算に対して行列数が整合してませんMatrixを修正してください"
End If
'-----積計算--------------
RmatAB = matA.Mult(matB)
'------結果表示----------------------
For i = 0 To matA_colN
For j = 0 To matA_rowN
dgv1.Rows(i).Cells(j).Value = RmatAB(i, j)
'Tfile1.Write(RmatAB(i, j))
'If j < matA_rowN Then
' Tfile1.Write(",")
'End If
'matAstr = "Trps_A(" & CStr(i) & "," & CStr(j) & ")=" & CStr(RmatA(i, j)) & vbCrLf
'RichTextBox1.AppendText(matAstr)
'matAstr = ""
Next j
'Tfile1.Write(vbCrLf)
Next i
'Tfile1.Close()
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment