Solved

C++ Builder: How to use TDrawGrid ?

Posted on 2002-07-24
5
2,241 Views
Last Modified: 2008-02-26
Hi! I'm pretty new to BC++ and I need a working example on how to use the TDrawGrid.
I want to program an configuration editor:

Name  | Value
------------
Color |  17
Size  |  12
Weight| 199

Therefore I need to know how to control the rows and cols, how to do the captions and how to check if the user input is valid.

I didn't find any resources about TDrawGrid on the net (only for Delphi users), so I have to ask this beginners question here...

thanx a lot
0
Comment
Question by:benutzername
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
Boomers earned 50 total points
ID: 7175235
Hi benutzername,

  I wrote the code using Delphi (I will try the C++ builder version tonight but it sould be really closed to this):

1. Add a TStringGrid (StringGrid1) to your form.
2. Add 2 text box (Edit1,Edit2) to your form.
3. Add a Button (Button1) and change the caption to "save".
4. Add a Button (Button2) and change the caption to "Add as new".

Create a global variable to your program called iCurrentRow
 int iCurrentRow;

In the CreateForm procedure paste this code:
{
  StringGrid1->RowCount = 4;
  StringGrid1->FixedRows = 1;
  StringGrid1->FixedCols = 0;

  StringGrid1->Cells[0,0] = 'Name';
  StringGrid1->Cells[1,0] = 'Value';
  StringGrid1->Cells[0,1] = 'Color';
  StringGrid1->Cells[1,1] = '17';
  StringGrid1->Cells[0,2] = 'Size';
  StringGrid1->Cells[1,2] = '12';
  StringGrid1->Cells[0,3] = 'Weight';
  StringGrid1->Cells[1,3] = '199';
  StringGrid1->col = 0;
  StringGrid1->row =1;
  Edit1->text = StringGrid1->Cells[0,1];
  Edit2->text = StringGrid1->Cells[1,1];
}

In the Button1Click procedure paste this code
{
  StringGrid1->Cells[0,iCurrentRow] = Edit1->Text;
  StringGrid1->Cells[1,iCurrentRow] = Edit2->Text;
}

In the StringGrid1Click procedure paste this code
{
  iCurrentRow = StringGrid1.Row;
  Edit1->text = StringGrid1->Cells[0,StringGrid1->Row];
  Edit2->text = StringGrid1->Cells[1,StringGrid1->Row];
}

In the Button2Click procedure paste this code
{
  StringGrid1->RowCount = StringGrid1->RowCount + 1;
  StringGrid1->Cells[0,StringGrid1->RowCount - 1] = Edit1->Text;
  StringGrid1->Cells[1,StringGrid1->RowCount - 1] = Edit2->Text;
}


This should be pretty close to what you wish to accomplish. If you have any question about the code add a comment!!!

Hope this will help you.
0
 
LVL 1

Expert Comment

by:wolfpackinc
ID: 7177918
This example specifies an edit mask in a string grid to limit the cells that contain telephone numbers, as indicated by a label cell in the first row:

void __fastcall TForm1::StringGrid1GetEditMask(TObject *Sender, long ACol, long ARow, AnsiString &Value)

{
  if (StringGrid1->Cells[ACol, 0] == "Phone Number")
    Value = "!\(999\)000-0000;1";
}

Heres the Masks you can use:

Character      Meaning in mask

 !      If a ! character appears in the mask, optional characters are represented in the EditText as leading blanks. If a ! character is not present, optional characters are represented in the EditText as trailing blanks.
 >      If a > character appears in the mask, all characters that follow are in uppercase until the end of the mask or until a < character is encountered.
 <      If a < character appears in the mask, all characters that follow are in lowercase until the end of the mask or until a > character is encountered.

 <>      If these two characters appear together in a mask, no case checking is done and the data is formatted with the case the user uses to enter the data.
\      The character that follows a \ character is a literal character. Use this character to use any of the mask special characters as a literal in the data.
 L      The L character requires an alphabetic character only in this position. For the US, this is A-Z, a-z.
 l      The l character permits only an alphabetic character in this position, but doesn't require it.

A      The A character requires an alphanumeric character only in this position. For the US, this is A-Z, a-z, 0-9.
 a      The a character permits an alphanumeric character in this position, but doesn't require it.
C      The C character requires an arbitrary character in this position.
 c      The c character permits an arbitrary character in this position, but doesn't require it.
 0      The 0 character requires a numeric character only in this position.
 9      The 9 character permits a numeric character in this position, but doesn't require it.

#      The # character permits a numeric character or a plus or minus sign in this position, but doesn't require it.
:      The : character is used to separate hours, minutes, and seconds in times. If the character that separates hours, minutes, and seconds is different in the regional settings of the Control Panel utility on your computer system, that character is used instead.
 /      The / character is used to separate months, days, and years in dates. If the character that separates months, days, and years is different in the regional settings of the Control Panel utility on your computer system, that character is used instead.

 ;      The ; character is used to separate the three fields of the mask.
 _      The _ character automatically inserts spaces into the text. When the user enters characters in the field, the cursor skips the _ character.

Any character that does not appear in the preceding table can appear in the first part of the mask as a literal character. Literal characters must be matched exactly in the edit control. They are inserted automatically, and the cursor skips over them during editing. The special mask characters can also appear as literal characters if preceded by a backslash character (\).

The second field of the mask is a single character that indicates whether literal characters from the mask should be included as part of the text for the edit control. For example, the mask for a telephone number with area code could be the following string:

(000)_000-0000;0;*

The 0 in the second field indicates that the Text property for the edit control would consist of the 10 digits that were entered, rather than the 14 characters that make up the telephone number as it appears in the edit control.

A 0 in the second field indicates that literals should not be included, any other character indicates that they should be included. The character that indicates whether literals should be included can be changed in the EditMask property editor, or programmatically by changing the MaskNoSave typed constant.

The third field of the mask is the character that appears in the edit control for blanks (characters that have not been entered). By default, this is the same as the character that stands for literal spaces. The two characters appear the same in an edit window. However, when a user edits the text in a masked edit control, the cursor selects each blank character in turn, and skips over the space character.

Example Mask : 999 will except up to 3 numbers
0
 

Author Comment

by:benutzername
ID: 7180076
hey, thanx for the fast and profound help, guys - but allow me one more question:

wolfpackinc told me about masking - works fine and is very usefull... but: is it possible to mask an input field, so it only accepts the characters 0-9 and A-F ? It seems that you can only choose between numerical, alphabetical and alphanumerical...

with best regards

ben utzer
0
 
LVL 1

Expert Comment

by:Boomers
ID: 7180121
Hi ben utzer,

   You can use a MaskEdit that would only allow alphanumeric value ("AAAAAAA") and maybe validate that you get an A to F under the event "keydown". But "out of the box" I don't think you can set the mask to A-F.
0
 

Author Comment

by:benutzername
ID: 7180131
thanks a lot guys!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now