〜 オブジェクト 〜
コンボボックスにアイテムを追加・挿入・削除

コンボボックスに新たな項目を追加・既存項目を削除する場合、
アイテムを変数で管理しているなら変数値を更新してobjprm命令を実行すれば実現できますが、
定数で管理している場合には、一筋縄には行きませんね。
このような場合は、オブジェクトにメッセージを送る方法でリストアイテムの追加削除を行いましょう。
HSP2はobjsend命令、HSP3からはsendmsg命令でオブジェクトにメッセージを送信できますので、
コンボボックスの最後に新たに項目を追加したい場合は「CB_ADDSTRING(=0x0143)」を、

HSP2「objsend オブジェクトID, 0x0143, 不使用(=0), 追加文字列(変数), 0」 HSP3「sendmsg オブジェクトハンドル, 0x0143, 不使用(=0), 追加文字列(変数のポインタ)

コンボボックスの途中に新たに項目を挿入したい場合は「CB_INSERTSTRING(=0x014A)」を、

HSP2「objsend オブジェクトID, 0x014A, 挿入位置, 挿入文字列(変数), 0」 HSP3「sendmsg オブジェクトハンドル, 0x014A, 挿入位置, 挿入文字列(変数のポインタ)

コンボボックスの既存項目を削除したい場合は「CB_DELETESTRING(=0x0144)」を、

HSP2「objsend オブジェクトID, 0x0144, 削除位置, 不使用変数(=0), 0」 HSP3「sendmsg オブジェクトハンドル, 0x0144, 削除位置, 不使用(=0)

で簡単に実現できます。 挿入や削除は項目位置を指定する必要がありますが、 先頭項目の指定ではない場合、大抵は選択項目を対象項目とすることと思います。 HSPでは選択アイテムを変数で管理しているので対象変数からの取得で確認できますが、 モジュール内で確認する場合等で変数へ直接アクセスしたくない場合は「CB_GETCURSEL(=0x0147)」を、

HSP2「objsend オブジェクトID, 0x0147, 不使用(=0), 不使用変数(=0), 0」 HSP3「sendmsg オブジェクトハンドル, 0x0147, 不使用(=0), 不使用(=0)」とすることで確認できます。

combox_index オブジェクトID
オブジェクトID選択行を取得するコンボボックスオブジェクトIDを指定する。

combox_add オブジェクトID, 文字列
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
文字列追加する文字列を指定する。

combox_ins オブジェクトID, 挿入行, 文字列
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
挿入行0を基準とした挿入行位置を指定する。
文字列追加する文字列を指定する。

combox_del オブジェクトID, 削除行
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
文字列追加する文字列を指定する。
削除行0を基準とした削除行位置を指定する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#module
#deffunc combox_index int
	mref id, 0
	mref stt, 64
	objsend id, 0x0147, 0, zero // コンボ選択アイテムを取得
	return stat

#deffunc combox_add int, str
	mref id, 0 : mref string, 33
	s = string
	objsend id, 0x0143, 0, s // コンボアイテムを追加
	return

#deffunc combox_ins int, str, int
	mref id, 0 : mref string, 33 : mref no, 2
	s = string
	objsend id, 0x014A, no, s // コンボアイテムを挿入
	return

#deffunc combox_del int, int
	mref id, 0 : mref no, 1
	objsend id, 0x0144, no, nonuse // コンボアイテムを削除
	return
#global

	objsize 150, 20
	pos 10, 10
	button "追加", *combo
	button "挿入", *combo
	button "削除", *combo
	pos 10, 100 : combox index.0, 200, "コンボ既存1\nコンボ既存2\nコンボ既存3" // オブジェクトID:3
	stop

*combo
	// 押されたボタンの処理を行う
	btnid = stat
	combox_index 3 : no = stat
	switch btnid
		case 0: cmb.0++ : combox_add 3, "コンボ追加" + cmb.0 : swbreak
		case 1: cmb.1++ : combox_ins 3, "コンボ挿入" + cmb.1, no : swbreak
		case 2: combox_del 3, no : swbreak
	swend
	stop

受取先 = combox_index(オブジェクトID)
受取先取得した選択行の受取先を指定する。
オブジェクトID選択行を取得するコンボボックスオブジェクトIDを指定する。

combox_add オブジェクトID, 文字列
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
文字列追加する文字列を指定する。

combox_ins オブジェクトID, 文字列, 挿入行
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
文字列追加する文字列を指定する。
挿入行0を基準とした挿入行位置を指定する。

combox_del オブジェクトID, 削除行
オブジェクトID追加するリストボックスオブジェクトIDを指定する。
文字列追加する文字列を指定する。
削除行0を基準とした削除行位置を指定する。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#module
#defcfunc combox_index int id
	sendmsg objinfo(id, 2), 0x147, 0, 0 // コンボ選択アイテムを取得
	return stat

#deffunc combox_add int id, str string
	s = string
	sendmsg objinfo(id, 2), 0x143, 2, s // コンボアイテムを追加
	return

#deffunc combox_ins int id, int no, str string
	s = string
	v objinfo(id, 2), 0x14A, no, s // コンボアイテムを挿入
	return

#deffunc combox_del int id, int no
	sendmsg objinfo(id, 2), 0x144, no, 0 // コンボアイテムを削除
	return
#global

	objsize 150, 20
	pos 10, 10
	button gosub "追加", *combo
	button gosub "挿入", *combo
	button gosub "削除", *combo
	pos 10, 100 : combox index.0, 200, "コンボ既存1\nコンボ既存2\nコンボ既存3" // オブジェクトID:3
	stop

*combo
	// 押されたボタンの処理を行う
	switch stat
		case 0: cmb.0++ : combox_add 3, "コンボ追加" + cmb.0 : swbreak
		case 1: cmb.1++ : combox_ins 3, combox_index(3), "コンボ挿入" + cmb.1 : swbreak
		case 2: combox_del 3, combox_index(3) : swbreak
	swend
	return