teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

追記

2018/06/09 16:13

投稿

YAmaGNZ
YAmaGNZ

スコア10665

answer CHANGED
@@ -1,6 +1,90 @@
1
1
  こちらの方法は、設定の画面の「種類」を増やす方法ではありません。
2
- ```Properties.Settings.Default.Lines```とList<string>の項目を
2
+ Properties.Settings.Default.LinesとList<string>の項目を
3
3
  追加するコードになります。
4
4
  要は、デザイナを使用せずに、コードで設定項目を増やしている行為となります。
5
5
 
6
- デザイナ上の「種類」を追加する方法に関しては、私もわかりません。
6
+ デザイナ上の「種類」を追加する方法に関しては、私もわかりません。
7
+
8
+ ### 追記
9
+ DLLを作成しての方法ですが、以下のようにしたところ、設定のデザイナ上でも設定でき、プログラム上からも
10
+ 値の保存ができました。
11
+
12
+ DLL側
13
+ ```C#
14
+ using System;
15
+ using System.ComponentModel;
16
+ using System.Globalization;
17
+
18
+ namespace TestDLL
19
+ {
20
+ [TypeConverter(typeof(CustomClassConverter))]
21
+ public class TestClass1
22
+ {
23
+ public int Test { get; set; }
24
+ public string Test2 { get; set; }
25
+ }
26
+
27
+ public class CustomClassConverter : ExpandableObjectConverter
28
+ {
29
+ //コンバータがオブジェクトを指定した型に変換できるか
30
+ //(変換できる時はTrueを返す)
31
+ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
32
+ {
33
+ if (destinationType == typeof(TestClass1))
34
+ return true;
35
+ return base.CanConvertTo(context, destinationType);
36
+ }
37
+
38
+ //指定した値オブジェクトを、指定した型に変換する
39
+ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
40
+ {
41
+ if (destinationType == typeof(string) && value is TestClass1)
42
+ {
43
+ TestClass1 cc = (TestClass1)value;
44
+ return cc.Test.ToString() + "," + cc.Test2;
45
+ }
46
+ return base.ConvertTo(context, culture, value, destinationType);
47
+ }
48
+
49
+ //コンバータが特定の型のオブジェクトをコンバータの型に変換できるか
50
+ //(変換できる時はTrueを返す)
51
+ //ここでは、String型のオブジェクトなら変換可能とする
52
+ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
53
+ {
54
+ if (sourceType == typeof(string))
55
+ return true;
56
+ return base.CanConvertFrom(context, sourceType);
57
+ }
58
+
59
+ //指定した値をコンバータの型に変換する
60
+ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
61
+ {
62
+ if (value is string)
63
+ {
64
+ string[] c = value.ToString().Split(new char[]{','},2);
65
+ TestClass1 cc = new TestClass1();
66
+ cc.Test = int.Parse(c[0]);
67
+ cc.Test2 = c[1];
68
+
69
+ return cc;
70
+ }
71
+ return base.ConvertFrom(context, culture, value);
72
+ }
73
+ }
74
+ }
75
+
76
+ ```
77
+
78
+ 下図のようにデザイナ上で設定でき
79
+ ![イメージ説明](c88fc62d6015720ec4d6fa70889a720a.png)
80
+
81
+ 保存結果はこうなりました。
82
+ ```XML
83
+ <userSettings>
84
+ <WindowsFormsApp1.Properties.Settings>
85
+ <setting name="Test" serializeAs="String">
86
+ <value>123,ABC</value>
87
+ </setting>
88
+ </WindowsFormsApp1.Properties.Settings>
89
+ </userSettings>
90
+ ```