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.
LVL 2
Code_AngelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Why reinvent the wheel? You'd use the CIPAddressCtrl that comes with MFC.
0
Jaime OlivaresSoftware ArchitectCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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 OlivaresSoftware ArchitectCommented:
>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 OlivaresSoftware ArchitectCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.