Logo 
Search:

C Programming Articles

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

Program to display textbox works like the normal textbox found in Windows

Posted By: Easy Tutor     Category: C Programming     Views: 3809

This textbox works like the normal textbox found in Windows. You can use this textbox function in your applications or projects without any modifications at all. The keys available are arrow keys, DEL, Insert, Backspace, Home, End.

Code for Program to display textbox works like the normal textbox found in Windows in C Programming

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <dos.h>

/* ..ASCII and Scan Codes of Keys.. */

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

/*-------------------------------------   ..Checks the status of Insert Key..   ..Returns..   ..1 if ON..   ..0 if OFF..--------------------------------------*/int ins_state ( void )
{
  unsigned char far *stat =(char far*)0x417 ;

  char status ;

  status = *stat ;

  if ( ( status & 128 ) == 128 )
     return 1;
  elsereturn 0;
}

/*----------------------------------    ..Changes the shape of cursor..------------------------------------*/void cursor_shape ( int y , int x )
{
    union REGS i, o ;
    i.h.ah = 1 ;
    i.h.ch = y ;
    i.h.cl = x ;
    int86 ( 16, &i, &o ) ;
}

/*-----------------------------------------------    ..Restores the screen to the orginal state..-------------------------------------------------*/void restore_ui(void)
{
  int i;
  char far *vidmem=(char far*)0xB8000000;

  window(1,1,80,25);
  clrscr();

  for (i=1;i<4000;i+=2)
      *(vidmem+i)=7;

  _setcursortype(_NORMALCURSOR);
}

/*-------------------------------------------    ..Function for drawing horizontal line..---------------------------------------------*/void horiz_draw(int start_x,int end_x,int y,char symbol)
{
 int i,len;
 len=end_x-start_x;

 for (i=1;i<=len+1;++i,++start_x)
 {
   gotoxy(start_x,y);
   cprintf("%c",symbol);
 }
}

/*-----------------------------------  ..The important fuction..  ..Just include in your programs..-----------------------------------+------------------------- Available Keys -----------------------------------+|    ~ Right Arrow --> For moving the cursor to the right                 ||    ~ Left  Arrow --> For moving the cursor to the left                  ||    ~ DEL         --> For deleting characters                            ||    ~ Backspace   --> For deleting characters                            ||    ~ Insert      --> For inserting charcters                            ||    ~ Home           --> For going to the beginning of the text             ||    ~ End         --> For going to the end of the text                   ||    ~ ESC           --> For clearing the textbox                 |+----------------------------------------------------------------------------+*/void txtbox ( int length )
{
   char str[80];  /*              ..Array into which characters are being stored,               the length of the string is limited to 80..             ..Just change the length to meet your needs..             ..You can even use dynamic memory allocation, if you               are poor in memory..          */char key;

   int startx,    /* ..For storing the starting position.. */
       x,
       y,
       i,
       j,
       currentpos,
       lastpos = 0,
       len;      /* ..Length of the string.. */

   startx = wherex();
   y = wherey();

/*--------------------------------------------------------------------      ..I have used the colors which I like, you are free to use your      own colors..----------------------------------------------------------------------*/

   textcolor ( WHITE );
   textbackground ( BLUE );

   /* ..Just for drawing the cute blue box.. */
   horiz_draw ( startx , startx + length , y , ' ');

   gotoxy ( startx , y );
   str[0] = '\0';

   while ( 1 )
   {
      if (  ins_state() == 1 ) /* ..If the Insert is ON change the cursor.. */
      cursor_shape ( 4 , 8 );
      else
      _setcursortype( _NORMALCURSOR );

      key = getch();
      x = wherex();
      len = strlen ( str );

      if ( key == ENTER )
     break;

      if ( key == ESC ) /* ..For clearing the characters in the textbox.. */
      {
     textbackground ( BLUE );
     horiz_draw ( startx , startx + length , y , ' ');
     gotoxy ( startx , y );
     str[0] = '\0';
     lastpos = 0;
     continue;
      }

      /* ..For storing the typed characters into the array.. */if ( key > 31 && key < 127 && x < ( startx + length ) )
      {
     if ( len == length && ins_state() == 1 )
     {
         printf ( "\a" );
         continue;
     }

     if (  ins_state() == 1 )  /* ..Insert.. */
     {
        currentpos = x - startx;
        str[len + 1] = '\0';

        for ( i = len ; i >= currentpos ; i-- )
        str[i] = str[i - 1];

        str[currentpos] = key;
        len = strlen ( str );
        gotoxy ( startx , y );
        horiz_draw ( startx , startx + len , y , ' ');
        gotoxy ( startx , y );

        for ( i = 0 ; i < len ; i++ )
           putch ( str[i] );

        gotoxy ( x , y );
     }
     else
     {
       i = x - startx;
       putch ( key );

       str[i] = key;

       if ( i >= lastpos )
       {
          lastpos = i;
          str[++i] = '\0';
       }
     }
      }

      if ( key == 8 && x <= startx + len && x != startx ) /* ..Backspace.. */
      {
     x = wherex();
     currentpos = x - startx;

     for ( i = currentpos - 1 ; str[i] != '\0' ; i++ )
         str[i] = str[i + 1];

     gotoxy ( startx , y );
     horiz_draw ( startx , startx + len , y , ' ');
     gotoxy ( startx , y );

     for ( i = 0 ; i < len ; i++ )
         putch ( str[i] );

     gotoxy ( --x , y );
     continue;
      }

      if ( key == 0 )   /* ..If the keys are Scan Keys.. */
      {
     key = getch();

     if ( key == END )
     {
         gotoxy ( startx + len , y );
         continue;
     }

     if ( key == HOME )
     {
         gotoxy ( startx , y );
         continue;
     }

     if ( key == LEFT && x > startx )
     {
         gotoxy ( --x , y );
         continue;
     }

     if ( key == RIGHT && x < startx + len )
     {
         gotoxy ( ++x , y );
         continue;
     }

     x = wherex();

     if ( key == DEL &&  x != startx + len )
     {
        x = wherex();
        currentpos = x - startx;

        for ( i = currentpos ; str[i] != '\0' ; i++ )
           str[i] = str[i + 1];

        gotoxy ( startx , y );
        horiz_draw ( startx , startx + len , y , ' ');
        gotoxy ( startx , y );

        for ( i = 0 ; i < len ; i++ )
           putch ( str[i] );

        gotoxy ( x , y );
        continue;
     }
      }
   }  /* ..End of while loop.. */

   i = strlen ( str );

   /* ..Truncates the useless spaces found at the end of string.. */for ( j = i - 1 ; j >= 0 ; --j )
   {
      if ( str[j] != ' ' )
      {
     str[++j] = '\0';
     break;
      }
   }

   printf ("\n\nThe string you entered : %s",str);
   getch();
}

/*--------------------------------------------------   ..Written just to demonstrate textbox function..   ..Really shitting..----------------------------------------------------*/void main()
{
  clrscr();
  gotoxy ( 32 , 1 );
  printf ( "Text Box Demo" );
  printf ( "\n\nEnter any text :  " );
  txtbox ( 60 );   /* ..Invoke the function.. */
  fflush ( stdin );
  restore_ui();
}

  
Share: 



Easy Tutor
Easy Tutor author of Program to display textbox works like the normal textbox found in Windows 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

 
Please enter your Comment

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

 
No Comment Found, Be the First to post comment!