• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

Edit box which will have fixed digits and DOTS between digits

How do I implement an Edit box which will have fixed digits and dots between digits? The kind of control you find in TCP/IP protocol setup.Could I have some example code please.
0
Code_Angel
Asked:
Code_Angel
  • 5
  • 3
  • 3
  • +2
3 Solutions
 
SteHCommented:
Have a look at
http://www.flounder.com/validating_edit_control.htm
on how to modify an edit control. Instead of changing the background you only change display to show a dot after at most 3 digits.
0
 
jkrCommented:
Why reinvent the wheel? You'd use the CIPAddressCtrl that comes with MFC.
0
 
Jaime OlivaresCommented:
You can use some kind of mask edit control. Have a look to:
http://www.codeproject.com/editctrl/maskededit.asp

Some more examples of validating edit controls at:
http://www.codeproject.com/editctrl/#Masked+and+Validating+controls
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
>> You can use some kind of mask edit control.

Why would you do that if MFC already has a control for *exactly* this purpose?
0
 
Jaime OlivaresCommented:
>Why would you do that if MFC already has a control for *exactly* this purpose?
Because you need version 4.71 or later of Comctl32.dll installed in your machine to work? So, you can't be sure it will work.
0
 
jkrCommented:
>>Because you need version 4.71 or later of Comctl32.dll installed in your machine to work?

OK, so it won't work on a out-of-the box Win95 that has never seen any IE newer than IE3 and *no* additional SW installed. And, it will not work on Win3.1 either.
0
 
SteHCommented:
>> You can use some kind of mask edit control.

Does Code_Angel really need a TCP/IP control or something similar? If he needs a control to input IP addresses then CIPAddressCtrl is the best way. In the case it is something similar the other solutions are better since they allow for modifications.
0
 
Code_AngelAuthor Commented:
SteH,

CIPAddressCtrl is not supported in eVC++ 4.0 so that's why I had to look for alternatives.
0
 
SteHCommented:
I had no problems with it ;-)
0
 
Jaime OlivaresCommented:
I have used the maskedit control without problems with eVC++, here is an IP variation of the control from codeproject. I have translated it, hope there are not errors:

ipmaskedit.h
---------------
#include "maskedit.h"       /* original maskedit control, also you must add maskedit.cpp to your project  */

class CEditIPAddr : public CMaskEdit
{
public:
      typedef struct {
            unsigned dir1:8;
            unsigned dir2:8;
            unsigned dir3:8;
            unsigned dir4:8;
      } TIPAddr;

public:
      DWORD m_Address;

      CEditIPAddr () : CEditIPAddr () {
            m_Address= 0;
      }
      virtual void PreSubclassWindow() {
            Init("[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9].[0-2][0-9][0-9]");
            CMaskEdit::PreSubclassWindow();
      }
      static void AFXAPI DDX_EditIPAddr (CDataExchange* pDX, int nIDC, CEditIPAddr & rControl, DWORD& addr);
      static void AFXAPI DDV_EditIPAddr(CDataExchange* pDX, CEditIPAddr & rControl);
};


IPmaskedit.cpp
-----------------

#include "ipmaskedit.h"

void AFXAPI CEditIPAddr::DDX_CEditIPAddr(CDataExchange* pDX, int nIDC, CEditIPAddr &rControl, DWORD& addr)
{
      DDX_Control(pDX, nIDC, (CWnd&)rControl);
      CString data;
      TIPAddr IPaddr;

      if (!pDX->m_bSaveAndValidate) {
            memcpy(&IPaddr, &addr, sizeof(IPaddr));
            data.Format(TEXT("%03i.%03i.%03i.%03i"), (int)IPaddr.dir4, (int)IPaddr.dir3, (int)IPaddr.dir2, (int)IPaddr.dir1);
            rControl.SetData(data);
      } else {
            data = rControl.GetData();
            IPaddr.dir4 = LOBYTE(_ttoi(data.Mid(0,3)));
            IPaddr.dir3 = LOBYTE(_ttoi(data.Mid(4,3)));
            IPaddr.dir2 = LOBYTE(_ttoi(data.Mid(8,3)));
            IPaddr.dir1 = LOBYTE(_ttoi(data.Mid(12,3)));
            memcpy(&addr, &IPaddr, sizeof(addr));
      }
}

void AFXAPI CEditIPAddr::DDV_CEditIPAddr(CDataExchange* pDX, CEditIPAddr& rControl)
{
      if (pDX->m_bSaveAndValidate) {
            CString data = rControl.GetData();
            int dir4 = _ttoi(data.Mid( 0,3));
            int dir3 = _ttoi(data.Mid( 4,3));
            int dir2 = _ttoi(data.Mid( 8,3));
            int dir1 = _ttoi(data.Mid(12,3));
            if (dir1*dir2*dir3*dir4==0 || dir1>255 || dir2>255 || dir3>255 || dir4>255) {
                  ::MessageBox(NULL, TEXT("Values must be between 1 and  255."), TEXT("IP Address Error"), MB_OK|MB_ICONEXCLAMATION);
                  pDX->Fail();
                  rControl.SetFocus();
            }
      }
}
0
 
Code_AngelAuthor Commented:
Thanks Guys it works like magic, much better than the previous one.
0
 
jkrCommented:
Since an IP address control was requested specifically, I am not really inclined to think of a split...
0
 
AndyAinscowCommented:
He did post:

 SteH,

CIPAddressCtrl is not supported in eVC++ 4.0 so that's why I had to look for alternatives.


jkr - I did have some trouble with this question.  The 'best' suggestion was one he did not want to use!  He also seems to say he uses the code posted by jaime.   Hence the split.
0
 
jkrCommented:
OK, no prob with me then.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now