Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Computer GraphicsRSS Feeds

Program of text box

Posted By: Easy Tutor     Category: C++ Programming     Views: 7583

Write a program of text box in c++

Code for Program of text box in C++ Programming

/*    The Main class is TextField    The constructors are        TextField(int x,int y,int len,char * str) &        TextField(int x,int y,int len)            Here:                x   - X position where textbox will be drawn                y   - Y position where textbox will be drawn                len - length of the array (string)                str - Specifies the text the default text                                        of text)    The methods are        void setText(char *str)            Stores str to the text field. Note that this wont display                 the text. You will have to explicithly call draw method.        void draw()            Draws the text field with the string in the variable str.        void hide()            Hides the text field        char *getText()            get the text from the text field. Press enter to exit from             this.    The Keys of the text field includes:        Escape                   : Clears the text field.        Left & Right Cursor keys : To move left and right respectively.        Home                     : To move to home.        End                         : To move to end.        Delete                     : Deletes the character at current                                    cursor position.        Back Space                 : Deletes the character before the current                                    cursor position.        Insert                     : To insert character into the field.*/

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>

#define ESC 27
#define ENTER 13
#define RIGHT 77
#define LEFT 75
#define HOME 71
#define END 79
#define INSERT 82
#define DEL 83
#define BKSP 8
#define TXW 8
#define TXH 8

/*   To Check the status of Insert       1 if ON       0 if OFF*/int ins_state()
{
    unsigned char far *stat=(char far*)0x417;
    char status;
    status=*stat;
    if((status&128)==128)
        return 1;
    elsereturn 0;
}

class TextField
{
    private:
        char str[21];
        int startx,starty,size;
        void draw(char *strin);
    public:
        TextField(int x1,int y1,int s,char *str1)
        {
            startx=x1+2;
            starty=y1+2;
            strcpy(str,str1);
            size=s;
        }
        TextField(int x1,int y1,int s)
        {
            strcpy(str,"");
            startx=x1+2;
            starty=y1+2;
            size=s;
        }
        void setText(char *str1);
        void draw();
        char *getText();
        void hide();
        void cursor(int x,int y,int ins,char ch);
};

void TextField::hide()
{
    setcolor(getbkcolor());
    setfillstyle(SOLID_FILL,getbkcolor());
    bar(startx-3,starty-3,startx+size*TXW+9,starty+TXH+3);
}

void TextField::setText(char *str1)
{
    strcpy(str,str1);
}

void TextField::draw()
{
    setcolor(RED);
    setfillstyle(SOLID_FILL,WHITE);
    bar(startx-2,starty-2,startx+size*TXW+8,starty+TXH+2);
    outtextxy(startx,starty,str);
}

void TextField::cursor(int x,int y,int ins,char ch)
{
    y+=2;
    flushall();
    char str[2];
    str[0]=ch;
    str[1]='\0';
    while(!kbhit())
    {
        setcolor(RED);
        if (ins)
            outtextxy(x,y,"Ü");
        else
            outtextxy(x,y,"_");
        delay(500);
        setcolor(WHITE);
        if (ins)
            outtextxy(x,y,"Ü");
        else
            outtextxy(x,y,"_");
        setcolor(RED);
        if (ch!=0)
            outtextxy(x,y-2,str);
        delay(500);
    }
    setcolor(WHITE);
    if (ins)
        outtextxy(x,y,"Ü");
    else
        outtextxy(x,y,"_");
    setcolor(RED);
    if (ch!=0)
        outtextxy(x,y-2,str);
}
char* TextField::getText()
{
    draw();
    int cur_x,xend,ins,len;
    char cur_ch,ch;
    int stx;
    cur_x=xend=textwidth(str);
    ins=ins_state();
    cur_ch=str[cur_x/TXW];
    cursor(cur_x+startx,starty,ins,cur_ch);
    ch=getch();
    setcolor(RED);
    while (ch!=ENTER)
    {
        if ((ch!=0)&&((isalnum(ch))||(ch=='.')||(ch==' ')))
        {
            ins=ins_state();
            if (!ins)
            {
                if (cur_x==xend)
                {
                    if (xend/TXW<size)
                    {
                        strncat(str,&ch,1);
                        xend+=TXW;
                        cur_x+=TXW;
                    }
                }
                else
                {
                    str[cur_x/TXW]=ch;
                    cur_x+=TXW;
                }
            }
            elseif (xend/TXW<size)
            {
                len=strlen(str);
                if (cur_x==xend)
                    strncat(str,&ch,1);
                else
                {
                    str[len+1]='\0';
                    for (int i=len;i>=cur_x/TXW;i--)
                        str[i]=str[i-1];
                    str[cur_x/TXW]=ch;
                }
                xend+=TXW;
            }
        }
        elseif (ch==ESC)
        {
            setText("");
            cur_x=xend=0;
        }
        elseif ((ch==BKSP)&&(cur_x>0))
        {
            len=strlen(str);
            for (int i=(cur_x-1)/TXW;i<len;i++)
                str[i]=str[i+1];
            str[len]='\0';
            xend-=TXW;
            cur_x-=TXW;
        }
        elseif (ch==0)
        {
            ch=getch();
            if ((ch==LEFT)&&(cur_x>0))
                cur_x-=TXW;
            elseif ((ch==RIGHT)&&(cur_x<xend))
                cur_x+=TXW;
            elseif (ch==HOME)
                cur_x=0;
            elseif (ch==END)
                cur_x=xend;
            elseif ((ch==DEL)&&(cur_x<xend))
            {
                len=strlen(str);
                for (int i=(cur_x+1)/TXW;i<len;i++)
                    str[i]=str[i+1];
                str[len]='\0';
                xend-=TXW;
            }
            ins=ins_state();
        }
        if (cur_x==xend)
            cur_ch=0;
        else
            cur_ch=str[cur_x/TXW];
        draw();
        stx=startx;
        cursor(cur_x+stx,starty,ins,cur_ch);
        ch=getch();
    }
    return str;
}

//Examplevoid main()
{
    int gd=DETECT,gm=0;
    initgraph(&gd,&gm,"");
    char *st="Hello";
    TextField t1(100,100,10,st);
    t1.draw();
    char *str=t1.getText();
    closegraph();
    cout<<str;
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program of text box Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program of text box is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Waresha Kumel from United States Comment on: Aug 24
I am getting 31 errors please help me in running this.Which setup did you use for this code?

View All Comments