Edit box not updating

I have a window that includes x (depending on height) edit controls. If there are more than x variables to be entered, the user can scroll up and down using the vertical scroll bar. This causes the edit boxes to be associated with different variables (and for the name next to the edit box to reflect this).

The underlying variables are updated when an edit box looses focus, or, if an edit box is selected (shown by Current!=-1) when the scroll bar is changed (this is to prevent problems when the edit box looses focus to the scroll bar).

In use, odd events occur: Occasionally, when the edit box contents is changed and the edit box looses focus, the varible is not updated and the edit box resets to the old value.

I have no idea why. The problem does not occur when I place break points in the code to check what is going on! I have tried SRW locks (where indicated) but to no avail.

NB the function Command_Call is being called by the wndproc is called when an edit control message is sent through the WM_COMMAND message to this window, and is constrcuted to permit a variable number of edit controls. The functions On_(windows message) are overrides of virtual functions from the base window class and are likewise called by the wndproc on the given window recieving the given message.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//	Constructor, Initialisation and Destructor
Evidence_Window::Evidence_Window(HWND owner,RECT& rect,Inference_Info* i):Info(i),Max_Values(2),Current(-1)
{
	InitializeSRWLock(&Evilock);
	Domain::iterator l=Info->Dom->end();
	for (Domain::iterator i=Info->Dom->begin();i!=l;++i)
	{
		if ((*i)->Get_Values_Num()>Max_Values){Max_Values=(*i)->Get_Values_Num();}	
	}
	Initialise_Window(Get_Winfo(owner,rect),SW_SHOW);
}
Evidence_Window::~Evidence_Window()
{	
	int l=Edit_Boxes.size();
	for (int n=0;n<l;++n)
	{
		delete Edit_Boxes[n];
	}
}

//	Class and Window information
WINFO Evidence_Window::Get_Winfo(HWND owner,RECT& rect)
{
	WINFO info(WS_CHILD|WS_VISIBLE|WS_VSCROLL,WS_EX_DLGMODALFRAME,_T("Evidence Window"),owner,rect,this);
	return info;
}		
//	Title

tstring Evidence_Window::GetTitleText()
{
	return _T("INPUT EVIDENCE");
}

//	Set Node

void Evidence_Window::Show_Var()
{
	InvalidateRect(hwnd,0,TRUE);
}

//-------------------------------------------------------------------------------------------------------------------------
//	Set Controls
//	Set max values (with new domain).  To be called when Domain in Info changes.
//-------------------------------------------------------------------------------------------------------------------------
void Evidence_Window::Reset_Given_New_Domain()
{
	//----------------------
	//	Work out max values
	Max_Values=2;
	Domain::iterator l=Info->Dom->end();
	for (Domain::iterator i=Info->Dom->begin();i!=l;++i)
	{
		if ((*i)->Get_Values_Num()>Max_Values){Max_Values=(*i)->Get_Values_Num();}	
	}
	//----------------------
	//	Set Vertical Scroll Bar
	SCROLLINFO si;
	si.cbSize = sizeof (si);
	
	si.fMask  = SIF_RANGE|SIF_POS|SIF_PAGE;
	si.nMin=0;
	si.nMax=max(Max_Values-Depth,0);
	si.nPage=Depth-1;
	si.nPos=0;
	SetScrollInfo (hwnd, SB_VERT, &si, TRUE);

	InvalidateRect(hwnd,0,TRUE);
}

//-------------------------------------------------------------------------------------------------------------------------
//	WndProc Message Processing
//-------------------------------------------------------------------------------------------------------------------------
bool Evidence_Window::On_WM_CREATE(HWND passed_hwnd)
{
	RECT rect;
	GetClientRect(passed_hwnd,&rect);

	SetTitleRect(rect.left+H_GAP,rect.top+V_GAP,rect.right-H_GAP,title_box_depth+V_GAP);	//tbd=title box depth in Titlebase

	Depth=(rect.bottom-(title_box_depth+(2*V_GAP)))/(BUT_DEPTH+V_GAP);

	//	First set of controls
	int x1=H_GAP+80;									//x position of first set of controls
	int w1=30;											//width of buttons in first row of controls
	int y1=title_box_depth+(2*V_GAP);					//base y position of first control

	for (int n=0;n<Depth;++n)
	{
		Edit_Boxes.push_back(new Edit_Box(passed_hwnd,10000+n,x1,y1,w1,BUT_DEPTH,ES_NUMBER|WS_CHILD|WS_VISIBLE));
		y1+=V_GAP+BUT_DEPTH;
	}

	//----------------------
	//	Set Vertical Scroll Bar
	SCROLLINFO si;
	si.cbSize = sizeof (si);
	
	si.fMask  = SIF_RANGE|SIF_POS|SIF_PAGE;
	si.nMin=0;
	si.nMax=max(Max_Values-Depth,0);
	si.nPage=Depth-1;
	si.nPos=0;
	SetScrollInfo (passed_hwnd, SB_VERT, &si, TRUE);

	return true;
}

bool Evidence_Window::On_WM_PAINT(HDC hdc,RECT& rect,int xPos,int yPos)
{
	//--------------------------------
	//	Title
	DrawTitle(hdc);

	//--------------------------------
	//	Edit Boxes
	rect.left=H_GAP;				
	rect.right=rect.left+80;
	rect.top=title_box_depth+(2*V_GAP);
	rect.bottom=rect.top+BUT_DEPTH;					

	//AcquireSRWLockExclusive(&Evilock);
	if (Info->Clicked_Var!=NULL)
	{
		int l=min(Depth,Info->Clicked_Var->Get_Values_Num()-yPos);
		int n=0;
		for (;n<l;++n)
		{
			DrawText(hdc,Info->Clicked_Var->Get_Value_Name(n+yPos).c_str(),-1,&rect,DT_END_ELLIPSIS);
			Edit_Boxes[n]->Enable(TRUE);
			Edit_Boxes[n]->SetText(num2T(Info->Evi->Get_Raw_Evidence(Info->Clicked_Var,n+yPos)));
			rect.top=rect.bottom+V_GAP;
			rect.bottom=rect.top+BUT_DEPTH;
		}
		for (;n<Depth;++n)
		{
			DrawText(hdc,_T("Unused"),-1,&rect,DT_END_ELLIPSIS);
			Edit_Boxes[n]->Clear();
			Edit_Boxes[n]->Enable(FALSE);
			rect.top=rect.bottom+V_GAP;
			rect.bottom=rect.top+BUT_DEPTH;
		}
	}
	else
	{
		for (int n=0;n<Depth;++n){Edit_Boxes[n]->Enable(FALSE);}
	}
	//ReleaseSRWLockExclusive(&Evilock);

	return true;
}

bool Evidence_Window::Command_Call(WPARAM wparam,LPARAM lparam) 
{
	// Get vertical scroll bar position.
	SCROLLINFO si;
	si.cbSize = sizeof (si);
	si.fMask  = SIF_POS;
	GetScrollInfo (hwnd, SB_VERT, &si);
	if (HIWORD(wparam)==EN_SETFOCUS){Current=LOWORD(wparam)-10000+si.nPos;return true;}//Needed to avoid bug on loosing focus to a scroll button!
	if (HIWORD(wparam)!=EN_KILLFOCUS){return false;}

	//AcquireSRWLockExclusive(&Evilock);
	if (Current!=-1)	
	{
		int Num;
		Edit_Boxes[Current-si.nPos]->GetInt(Num);
		Info->Evi->Input_Evidence(Info->Clicked_Var,Current,Num);
		Current=-1;
	}
	//ReleaseSRWLockExclusive(&Evilock);

	return true;
}

bool Evidence_Window::On_WM_VSCROLL(SCROLLINFO& si,WPARAM wparam)
{
	//AcquireSRWLockExclusive(&Evilock);
	if (Current!=-1)
	{
		int Num;
		Edit_Boxes[Current-si.nPos]->GetInt(Num);//Needed to avoid bug on loosing focus to a scroll button!
		Info->Evi->Input_Evidence(Info->Clicked_Var,Current,Num);
		Current=-1;
	}
	//ReleaseSRWLockExclusive(&Evilock);

	switch (LOWORD (wparam))
	{
		// User clicked the left arrow.
		case SB_LINELEFT: 
			si.nPos -= 1;
			break;

		// User clicked the right arrow.
		case SB_LINERIGHT: 
			si.nPos += 1;
			break;

		// User clicked the scroll bar shaft left of the scroll box.
		case SB_PAGELEFT:
			si.nPos -= (Depth-1);
			break;

		// User clicked the scroll bar shaft right of the scroll box.
			case SB_PAGERIGHT:
			si.nPos += (Depth-1);
			break;

		// User dragged the scroll box.
			case SB_THUMBTRACK: 
			si.nPos = si.nTrackPos;
			break;

			default :
			break;
	}

	// Set the position.  Due to adjustments
	// by Windows it may not be the same as the value set.
	si.fMask = SIF_POS;
	SetScrollInfo (hwnd, SB_VERT, &si, TRUE);

	// DO NOT scroll the window (only relevant for what text is displayed!).
	InvalidateRect(hwnd,0,TRUE);
	return true;
}
Please update your value from edit box to your variable.
UpdateData(TRUE);

Topic archived. No new replies allowed.