Skip to content

Commit d417e63

Browse files
committed
Basic: add delete + count label for ListView
1 parent 738d2db commit d417e63

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

examples/basic/main.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
"strconv"
@@ -194,15 +195,18 @@ func bindWidgets(dlg *wingui.Dialog) {
194195
lv1.SetExtendedStyle(win.LVS_EX_FULLROWSELECT | win.LVS_EX_GRIDLINES | win.LVS_EX_DOUBLEBUFFER)
195196
lv1.InsertColumn(0, "Name", 120, win.LVCFMT_LEFT)
196197
lv1.InsertColumn(1, "Value", 80, win.LVCFMT_LEFT)
198+
lv1Status, _ := wingui.BindNewStatic(IDC_TAB_LABEL_COUNT, tab1)
199+
updateLv1Status := func() {
200+
lv1Status.SetText(fmt.Sprintf("Count: %d Selected: %d", lv1.ItemCount(), lv1.SelectedIndex()))
201+
}
197202
for i := 0; i < 5; i++ {
198203
idx := lv1.InsertItem(-1, "Item "+strconv.Itoa(i))
199204
lv1.SetItemText(idx, 1, "V"+strconv.Itoa(i))
200205
}
206+
updateLv1Status()
201207
lv1.OnItemChanged = func(index int) {
202-
sel := lv1.SelectedIndex()
203-
if sel >= 0 {
204-
tab1.SetText("Page1 - selected: " + lv1.GetItemText(sel, 0))
205-
}
208+
_ = index
209+
updateLv1Status()
206210
}
207211
lv1.OnItemActivate = func(index int) {
208212
if index < 0 {
@@ -212,7 +216,6 @@ func bindWidgets(dlg *wingui.Dialog) {
212216
}
213217

214218
tabBtn, _ := wingui.BindNewButton(IDC_TAB_BUTTON1, tab1)
215-
tabBtn.SetText("Add Item")
216219
var addSeq int32 = 5
217220
tabBtn.OnClicked = func() {
218221
n := int(atomic.AddInt32(&addSeq, 1) - 1)
@@ -222,7 +225,20 @@ func bindWidgets(dlg *wingui.Dialog) {
222225
return
223226
}
224227
lv1.SetItemText(idx, 1, "V"+strconv.Itoa(n))
225-
tab1.SetText("Page1 - count: " + strconv.Itoa(lv1.ItemCount()))
228+
updateLv1Status()
229+
}
230+
delBtn1, _ := wingui.BindNewButton(IDC_TAB_BUTTON_DEL, tab1)
231+
delBtn1.OnClicked = func() {
232+
sel := lv1.SelectedIndex()
233+
if sel < 0 {
234+
delBtn1.MessageBox("No selection", "ListView", win.MB_OK|win.MB_ICONWARNING)
235+
return
236+
}
237+
if !lv1.DeleteItem(sel) {
238+
delBtn1.MessageBox("DeleteItem failed", "ListView", win.MB_OK|win.MB_ICONERROR)
239+
return
240+
}
241+
updateLv1Status()
226242
}
227243
tabcontrol.AddDialogPage("Page1", tab1)
228244

@@ -235,13 +251,21 @@ func bindWidgets(dlg *wingui.Dialog) {
235251
lv2.SetExtendedStyle(win.LVS_EX_FULLROWSELECT | win.LVS_EX_GRIDLINES | win.LVS_EX_DOUBLEBUFFER)
236252
lv2.InsertColumn(0, "Name", 120, win.LVCFMT_LEFT)
237253
lv2.InsertColumn(1, "Value", 80, win.LVCFMT_LEFT)
254+
lv2Status, _ := wingui.BindNewStatic(IDC_TAB_LABEL_COUNT, tab2)
255+
updateLv2Status := func() {
256+
lv2Status.SetText(fmt.Sprintf("Count: %d Selected: %d", lv2.ItemCount(), lv2.SelectedIndex()))
257+
}
238258
for i := 0; i < 3; i++ {
239259
idx := lv2.InsertItem(-1, "Row "+strconv.Itoa(i))
240260
lv2.SetItemText(idx, 1, time.Now().Format("15:04:05"))
241261
}
262+
updateLv2Status()
263+
lv2.OnItemChanged = func(index int) {
264+
_ = index
265+
updateLv2Status()
266+
}
242267

243268
tabBtn2, _ := wingui.BindNewButton(IDC_TAB_BUTTON1, tab2)
244-
tabBtn2.SetText("Show Selected")
245269
tabBtn2.OnClicked = func() {
246270
sel := lv2.SelectedIndex()
247271
if sel < 0 {
@@ -250,6 +274,19 @@ func bindWidgets(dlg *wingui.Dialog) {
250274
}
251275
tabBtn2.MessageBox("Selected: "+lv2.GetItemText(sel, 0)+" / "+lv2.GetItemText(sel, 1), "ListView", win.MB_OK|win.MB_ICONINFORMATION)
252276
}
277+
delBtn2, _ := wingui.BindNewButton(IDC_TAB_BUTTON_DEL, tab2)
278+
delBtn2.OnClicked = func() {
279+
sel := lv2.SelectedIndex()
280+
if sel < 0 {
281+
delBtn2.MessageBox("No selection", "ListView", win.MB_OK|win.MB_ICONWARNING)
282+
return
283+
}
284+
if !lv2.DeleteItem(sel) {
285+
delBtn2.MessageBox("DeleteItem failed", "ListView", win.MB_OK|win.MB_ICONERROR)
286+
return
287+
}
288+
updateLv2Status()
289+
}
253290
tabcontrol.AddDialogPage("Page2", tab2)
254291
tabcontrol.Select(0)
255292
// other

examples/basic/resource.h.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic/ui.syso

96 Bytes
Binary file not shown.

examples/basic/ui/resource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
#define IDS_SLIDER 40013
2727
#define IDP_PROGRESSBAR 40014
2828
#define IDC_TAB1 40015
29+
#define IDC_TAB_BUTTON_DEL 40016
30+
#define IDC_TAB_LABEL_COUNT 40017

examples/basic/ui/ui.rc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ STYLE DS_3DLOOK | DS_CENTER | DS_SHELLFONT | WS_VISIBLE | WS_CHILDWINDOW | WS_SY
7171
EXSTYLE WS_EX_CLIENTEDGE
7272
FONT 8, "Ms Shell Dlg"
7373
{
74-
CONTROL "", IDC_TAB_LIST1, WC_LISTVIEW, WS_TABSTOP | WS_BORDER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS, 7, 7, 141, 55, WS_EX_LEFT
75-
PUSHBUTTON "Button1", IDC_TAB_BUTTON1, 7, 66, 70, 14, 0, WS_EX_LEFT
74+
CONTROL "", IDC_TAB_LIST1, WC_LISTVIEW, WS_TABSTOP | WS_BORDER | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS, 7, 7, 141, 50, WS_EX_LEFT
75+
PUSHBUTTON "Add Item", IDC_TAB_BUTTON1, 7, 60, 55, 14, 0, WS_EX_LEFT
76+
PUSHBUTTON "Delete Sel", IDC_TAB_BUTTON_DEL, 66, 60, 55, 14, 0, WS_EX_LEFT
77+
LTEXT "Count: 0", IDC_TAB_LABEL_COUNT, 7, 76, 141, 9, SS_LEFT, WS_EX_LEFT
7678
}
7779

7880

0 commit comments

Comments
 (0)