Click here to Skip to main content
15,914,070 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Modifying the limits of an integer edit control Pin
jhwurmbach16-Oct-02 1:59
jhwurmbach16-Oct-02 1:59 
GeneralRe: Modifying the limits of an integer edit control Pin
Tomasz Sowinski16-Oct-02 3:16
Tomasz Sowinski16-Oct-02 3:16 
GeneralRe: Modifying the limits of an integer edit control Pin
Christian Graus16-Oct-02 11:33
protectorChristian Graus16-Oct-02 11:33 
GeneralRe: Modifying the limits of an integer edit control Pin
Joan M16-Oct-02 1:14
professionalJoan M16-Oct-02 1:14 
GeneralRe: Modifying the limits of an integer edit control Pin
jhwurmbach16-Oct-02 2:01
jhwurmbach16-Oct-02 2:01 
GeneralRe: Modifying the limits of an integer edit control Pin
adamUK16-Oct-02 2:02
adamUK16-Oct-02 2:02 
GeneralRe: Modifying the limits of an integer edit control Pin
Joan M16-Oct-02 2:11
professionalJoan M16-Oct-02 2:11 
GeneralRe: Modifying the limits of an integer edit control Pin
adamUK16-Oct-02 4:25
adamUK16-Oct-02 4:25 
Hi Joan,

Pop the following .h and .cpp files in your code. It should work (does for me anyway). You might have to play around with the checking of string lengths and conversions to make it more robust but the idea is there.

In class wizard, set the member variable of your edit box as a Control of type CIntEdit (call it m_wndEditBox or something) and then call m_wndEditBox.SetMaxInt(nMax) on the fly whenver you want (pref. initialise it in OnInitDialog in your dialog box, for example as the default is zero).

Note, the minimum acceptable integer value is zero but I am sure you could change the code as you see fit.

Any probs, givvus a shout

Best regards and good luck Smile | :) ,

Adam.

IntEdit.h
class CIntEdit : public CEdit<br />
{<br />
// Construction<br />
public:<br />
	CIntEdit();<br />
<br />
// Attributes<br />
public:<br />
<br />
// Operations<br />
public:<br />
<br />
// Overrides<br />
	// ClassWizard generated virtual function overrides<br />
	//{{AFX_VIRTUAL(CIntEdit)<br />
	//}}AFX_VIRTUAL<br />
<br />
// Implementation<br />
public:<br />
	void SetMaxInt(int nMax);<br />
	int GetMaxInt();<br />
	virtual ~CIntEdit();<br />
<br />
	// Generated message map functions<br />
protected:<br />
	int m_nMaxInt;<br />
	//{{AFX_MSG(CIntEdit)<br />
	afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);<br />
	//}}AFX_MSG<br />
<br />
	DECLARE_MESSAGE_MAP()<br />
};


IntEdit.cpp
// IntEdit.cpp : implementation file<br />
//<br />
<br />
#include "stdafx.h"<br />
#include "IntEdit.h"<br />
<br />
#ifdef _DEBUG<br />
#define new DEBUG_NEW<br />
#undef THIS_FILE<br />
static char THIS_FILE[] = __FILE__;<br />
#endif<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CIntEdit<br />
<br />
CIntEdit::CIntEdit()<br />
{<br />
	//Set m_nMaxInt=0;<br />
	m_nMaxInt=0;<br />
}<br />
<br />
CIntEdit::~CIntEdit()<br />
{<br />
}<br />
<br />
<br />
BEGIN_MESSAGE_MAP(CIntEdit, CEdit)<br />
	//{{AFX_MSG_MAP(CIntEdit)<br />
	ON_WM_CHAR()<br />
	//}}AFX_MSG_MAP<br />
END_MESSAGE_MAP()<br />
<br />
/////////////////////////////////////////////////////////////////////////////<br />
// CIntEdit message handlers<br />
<br />
void CIntEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) <br />
{<br />
	const int DEL=8;<br />
	if((nChar<'0' || nChar>'9') && nChar!=DEL) return;<br />
	CString strWndTxt="", strTemp="",strChar="";<br />
	strChar=(TCHAR)nChar;<br />
<br />
	int nStartChar=0, nEndChar=0, nLen=0, nVal=0;<br />
	GetWindowText(strWndTxt);<br />
	nLen=strWndTxt.GetLength()/sizeof(TCHAR);<br />
	GetSel(nStartChar,nEndChar);<br />
	strTemp=strWndTxt.Left(nStartChar)+strChar+strWndTxt.Right(nLen-nEndChar);<br />
	TRACE(strTemp);<br />
	<br />
	nVal=_ttoi(strTemp);<br />
	if(nVal<=m_nMaxInt)<br />
	{<br />
		//if modified string is okay then call base class<br />
		CEdit::OnChar(nChar, nRepCnt, nFlags);<br />
	}<br />
}<br />
<br />
int CIntEdit::GetMaxInt()<br />
{<br />
	return m_nMaxInt;<br />
}<br />
<br />
void CIntEdit::SetMaxInt(int nMax)<br />
{<br />
	m_nMaxInt=nMax;<br />
}


www.beachwizard.com/travelogue[^]

"I spent a lot of my money on booze, birds and fast cars. The rest I just squandered"
George Best.
GeneralRe: Modifying the limits of an integer edit control Pin
PJ Arends16-Oct-02 6:23
professionalPJ Arends16-Oct-02 6:23 
GeneralRe: Modifying the limits of an integer edit control Pin
adamUK16-Oct-02 9:29
adamUK16-Oct-02 9:29 
GeneralRe: Modifying the limits of an integer edit control Pin
jhwurmbach16-Oct-02 20:44
jhwurmbach16-Oct-02 20:44 
GeneralRe: Modifying the limits of an integer edit control [corrected] Pin
Joaquín M López Muñoz16-Oct-02 3:34
Joaquín M López Muñoz16-Oct-02 3:34 
GeneralRe: Modifying the limits of an integer edit control Pin
Tomasz Sowinski16-Oct-02 3:44
Tomasz Sowinski16-Oct-02 3:44 
GeneralRe: Modifying the limits of an integer edit control Pin
jhwurmbach16-Oct-02 4:52
jhwurmbach16-Oct-02 4:52 
GeneralRe: Modifying the limits of an integer edit control Pin
Tomasz Sowinski16-Oct-02 5:03
Tomasz Sowinski16-Oct-02 5:03 
GeneralRe: Modifying the limits of an integer edit control Pin
PJ Arends16-Oct-02 6:16
professionalPJ Arends16-Oct-02 6:16 
GeneralFile Owner Question ...... SOS - SOS - SOS Pin
Qiang.Fu15-Oct-02 23:14
Qiang.Fu15-Oct-02 23:14 
GeneralRe: File Owner Question ...... SOS - SOS - SOS Pin
Andreas Saurwein16-Oct-02 4:10
Andreas Saurwein16-Oct-02 4:10 
GeneralRe: File Owner Question ...... SOS - SOS - SOS Pin
Qiang.Fu18-Oct-02 2:48
Qiang.Fu18-Oct-02 2:48 
Generalcalculation of runing time of program Pin
Anonymous15-Oct-02 22:35
Anonymous15-Oct-02 22:35 
GeneralRe: calculation of runing time of program Pin
l a u r e n16-Oct-02 4:46
l a u r e n16-Oct-02 4:46 
Generallicense Pin
helping hand15-Oct-02 21:11
helping hand15-Oct-02 21:11 
GeneralRe: license Pin
Christian Graus15-Oct-02 21:22
protectorChristian Graus15-Oct-02 21:22 
GeneralRe: license Pin
Jon Hulatt15-Oct-02 21:26
Jon Hulatt15-Oct-02 21:26 
GeneralRe: license Pin
Christian Graus15-Oct-02 21:38
protectorChristian Graus15-Oct-02 21:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.