昨日の
これを使って
これでPixtack紫陽花にもうまく取り込めたので
内容をメモ
昨日の書き方よりもかなり短くできた
これであっているのかわからないけど動いている
作ったクラスは二つなのは変わらないけど
設定を記録するクラス
<Serializable> Class FontSetting
Public Property ForeColor1 As Color
Public Property ForeColor2 As Color
Public Property ItemName As String
Public Property Yokogaki As Boolean
Public Property Gradation As Boolean
Public Property GammaC As Boolean
Public Property Size As Integer
Public Property FontName As String
End Class
昨日はそれぞれのプロパティにGetとSetを書いていたけど今回は書いていない
ただ単に宣言をしただけになっているけど普通に動いている
これでいいの?
もう一つは設定をまとめてリストに追加するクラス
<Serializable> Class FontSettingList
Inherits List(Of FontSetting)
Public Function GetNameList() As String()
Dim lc As Integer = Me.Count - 1
Dim nl(lc) As String
For i As Integer = 0 To lc
nl(i) = Me.Item(i).ItemName
Next
Return nl
End Function
End Class
これも昨日のmyFontListクラスに比べると大幅に短くなった
最初はmyFontListと同じように書いていたんだけど
リストに追加するとか追加された設定を読みだして返すとかの動作は
ジェネリックリスト?クラス?オブジェクト?にある動作と同じだから
これを継承(Inherits)したクラスで作れば追加するとかの動作を書かなくてもいいんじゃないか?って思いついて試したらこれでも期待通りに動いたのでこんなに短くなった
もしかしたら
Form1の中でList(Of FontSetting)の変数を作ってそれをシリアライズすれば
このクラス自体必要ないのかもしれないけどその方法がわかっていないので
このままでいいや
詳細設定ボタンで表示されるウィンドウのForm6
設定をバイナリファイルにして保存するメソッド
Private Sub SettingSave設定ファイル保存()
Using fst As New FileStream(filename, FileMode.Create)
Dim bin As New BinaryFormatter
bin.Serialize(fst, FontSLフォントの設定リスト)
End Using
End Sub
Using
これは初めて使った
途中でエラーとかがでもUsingで宣言した変数なら確実に終了処理をしてくれる
そういうのもあるのかっ
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form6
Private FontSLフォントの設定リスト As New FontSettingList
Private myForm3 As Form3 = New Form3
Private fileName As String
Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fD As String = My.Application.Info.DirectoryPath
Dim fN As String = "FontSetting.bin"
fileName = fD & "\" & fN
myForm3 = Me.Owner
If File.Exists(fileName) = False Then Exit Sub
Using fst As New FileStream(fileName, FileMode.Open)
Dim bin As New BinaryFormatter()
FontSLフォントの設定リスト = bin.Deserialize(fst)
End Using
Dim nl() As String = FontSLフォントの設定リスト.GetNameList()
Me.ComboBoxFontSetting.Items.AddRange(nl)
End Sub
Private Sub ComboBoxFontSetting_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxFontSetting.SelectedIndexChanged
Dim int As Integer = Me.ComboBoxFontSetting.SelectedIndex
Dim fs As FontSetting = FontSLフォントの設定リスト.Item(int)
Form1.IsFontSettingNow = True
With myForm3
.RadioButtonStringH.Checked = fs.Yokogaki
.ButtonFontColor.ForeColor = fs.ForeColor1
.ButtonFontColor2.ForeColor = fs.ForeColor2
.CheckBoxTextGradation.Checked = fs.Gradation
.CheckBoxStringGamma.Checked = fs.GammaC
.ComboBoxAllFonts.Text = fs.FontName
.NumericUpDownFontSize.Value = fs.Size
End With
Form1.IsFontSettingNow = False
Call Form1.TextSample()
End Sub
Private Sub ButtonAddFontSetting登録_Click(sender As Object, e As EventArgs) Handles ButtonAddFontSetting登録.Click
Dim fs As New FontSetting
With fs
.ForeColor1 = myForm3.ButtonFontColor.ForeColor
.ForeColor2 = myForm3.ButtonFontColor2.ForeColor
.ItemName = myForm3.NumericUpDownFontSize.Value & "_" & myForm3.ComboBoxAllFonts.Text
.FontName = myForm3.ComboBoxAllFonts.Text
.Yokogaki = myForm3.RadioButtonStringH.Checked
.Gradation = myForm3.CheckBoxTextGradation.Checked
.GammaC = myForm3.CheckBoxStringGamma.Checked
.Size = myForm3.NumericUpDownFontSize.Value
End With
FontSLフォントの設定リスト.Add(fs)
Call SettingSave設定ファイル保存()
Call Comboboxコンボボックスの項目更新(True)
End Sub
このあたりの設定の指定が無視される
よくわかっていない
Form3からForm6の呼び出しかた
Private Sub ButtonFontSetting詳細設定_Click(sender As Object, e As EventArgs) Handles ButtonFontSetting詳細設定.Click
For Each f As Form In Me.OwnedForms
If f.Name = "Form6" Then
Exit Sub
End If
Next
Me.Cursor = Cursors.WaitCursor
Dim f6 As New Form6
f6.Show(Me)
f6.ShowInTaskbar = False
Me.Cursor = Cursors.Default
End Sub
こう書いている
参照したところ
関連記事
今回のを使っていろいろ設定保存できるようにした