Dynamically put checkboxes on form - database

I have a table in DB with information about some goods. Goods may be fillable, so we can add text to it. So I want to dynamically generate a list of checkboxes related to info in table and even some checkboxes must be with TEdit component to make a possibility to add text to this item. So how can I do it? What component should I use? I figure out that TTreeView is almost enough, but it doesn't allow to "draw" TEdit near checkboxes. I'm using Delphi 2010. Thanks in advance! Hope for your help!

If I read your question correctly, you would like to create some controls on a form based on the contents of table. In the following example I have assumed you want to do this based on the contents of the current record in a TDBGrid, so you'll have to adapt as needed.
The example assumes a form with a TDBGrid and a TPanel (Panel1) that will hold the controls created at run time.
The TDBGrid will be connected to a TDataSource component and that will be connected to some TDataSet descendant for the table/query with the information. The TDataSource has a OnDataChanged event. This event gets triggered when the data in a field changes or when the current record in the dataset changes. So you can use that to change the controls as the current record changes.
procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
var
i: Integer;
Chk: TCheckBox;
Edit: TEdit;
begin
// When the Field is assigned, the call is the result of a change in the field.
// When the Field is unassigned, the call is the result of changing the current record.
if Assigned(Field) then
Exit;
// Remove controls on panel
for i := Panel1.ControlCount - 1 downto 0 do
Panel1.Controls[i].Free;
// Add controls on panel for current record
if True then // Replace this with condition based on contents of current record (if any!)
begin
Chk := TCheckBox.Create(Self); // Set Owner, so it is freed when form is closed.
Chk.Parent := Panel1; // Set Parent, so the control is shown.
Chk.Left := FLeftIndent; // Create FLeftIndent as a member field of the form, set value in OnCreate.
Chk.Top := FNextTop; // Create FNextTop as a member field of the form.
Inc(FNextTop, FSpacing); // Create FSpacing as a member field of the form, set value in OnCreate.
if True then // Replace this with condition that dictates creation of Edit
begin
Edit := TEdit.Create(Self);
Edit.Parent := Panel1;
Edit.Left := Chk.Left + Chk.Width + FSpacing;
Edit.Top := Chk.Top; // Add offset as needed for proper alignment of CheckBox and Edit.
end;
end;
end;
Please note that if you do not have any other checkboxes or edits on the form, you will have to include the proper vcl units yourself. The easiest way to do that is to drop them on the form, save the form and then delete the controls again.

try this example about creating checkbox on runtimehpe it helps you will have to modify the position dynamically. you can either create a new component wich includes checkbox with Edit on it or create the TEdit dynamically where you need it.
Consider creating an array of TCheckbox and one of TEdit and set the visible property of each Edit using something like IsEditNeeded boolean function in which you code the conditions if an Edit field is needed.

I would be tempted to use a gridview like ExGridView, and let it draw my checkboxes, and do the "edit" controls for each row, for me. However, if you really want an edit box, instead of a grid, you could also try a control-grid approach (1 checkbox + 1 edit control, in a control-grid).

Related

Oracle Apex checkbox

I have a page within my app with a Checkbox option.
The idea is basically to allow the user to select/unselect it if the change needs to be applied for every Sales Rep.
Something like this:
If checkbox is clicked = Change would be applied to all territories owned by the Rep
If checkbox is not clicked = Change would only be applied to the selected territory
I can't seem to get the checkbox clicked option to work.
I'm using an instr function to get a value out of it but it doesn't work:
select instr(':' ||:P11_CHECK_FOR_ALL|| ':', 'Request') into v_number_terr from dual;
if v_number_terr >0
(P11_CHECK_FOR_ALL is my checkbox Item / 'Request' is a word that's part of its label)
my checkbox pic
I'm trying to capture (in a process) whether the option is checked or not.
Could someone give me a hand please?
Thanks!
In apex a checkbox behaves just like a select list (with multiple selects possible). There is a display value and a return value. If nothing is selected, the page item value will be null. If one or more value are selected, the page item will contain a colon separated list of selected return values.
To handle the select list in a page process, the easiest is to split up the colon separated list in individual values using APEX_STRING.SPLIT
Example (untested):
DECLARE
l_check_for_all apex_t_varchar2;
BEGIN
l_check_for_all := apex_string.split(:P11_CHECK_FOR_ALL,':');
-- loop through the values. If nothing is selected then the COUNT will be 0 and nothing will be executed.
FOR i IN 1 .. l_check_for_all.COUNT LOOP
// do your pl/sql magic. You can reference the current item with l_check_for_all(i)
END LOOP;
END;

How to get the user input from the frame and save into txt file?

My programs run like this:
I have a form with an add button, save button, and tabcontrol.
When the add button in the form is clicked, the tabcontrol will add more tabitem. And the tabitem is basically added from myframe which includes users key-in value in a TEdit.
What I want to achieve is to print all the user input from the frame to a txt file when I click on the save button(tick) in the form. This is my code in form. Can anyone please give me some hint?
My frame Image:
My form Image:
My code:
The following should teach you all the ingredients you need.
Create a new application. I chose to make a VCL application, but I double-checked that all the steps are the same in Firemonkey (FMX).
Add a few edit boxes and combo boxes, like this:
Name the controls eFirstName, eLastName, cbSex, and btnSave, respectively.
Then write the following OnClick handler for the button:
procedure TForm1.btnSaveClick(Sender: TObject);
var
DataFile: TMemIniFile;
begin
DataFile := TMemIniFile.Create(TPath.Combine(TPath.GetDocumentsPath, 'PersonalData.txt'));
try
DataFile.WriteString('General', 'FirstName', eFirstName.Text);
DataFile.WriteString('General', 'LastName', eLastName.Text);
if cbSex.ItemIndex <> -1 then
DataFile.WriteString('General', 'Sex', cbSex.Items[cbSex.ItemIndex]);
DataFile.UpdateFile;
finally
DataFile.Free;
end;
end;
You need to add both IniFiles and IOUtils to your uses list (at least the implementation one).
Now, if you fill in the form,
and click the Save button, the following file is created:
[General]
FirstName=Andreas
LastName=Rejbrand
Sex=Male
It isn't evident from the screenshots alone, but if you are at the top edit box and press the Tab key repeatedly, you go to the second edit box, to the combo box, and then, finally, to the button. This is because I made sure that the tab order is correct. You should do the same.
Also notice the underlined characters. These are called keyboard accelerators. If I press Alt+F, for instance, focus will move to the First name field. This is achieved by giving the label the caption (text) &First name: and assigning the corresponding edit control to the label's FocusControl property.
In this case, the button is Default, meaning that it is the button that responds to the Enter key. If it hadn't been default (and in your GUI, that might not make sense), I would have given it the caption &Save and changed &Sex: to S&ex:. Can you guess why?
Other approaches
You can also use a TStringList:
procedure TForm1.btnSaveClick(Sender: TObject);
var
DataFile: TStringList;
begin
DataFile := TStringList.Create;
try
DataFile.AddPair('FirstName', eFirstName.Text);
DataFile.AddPair('LastName', eLastName.Text);
if cbSex.ItemIndex <> -1 then
DataFile.AddPair('Sex', cbSex.Items[cbSex.ItemIndex]);
DataFile.SaveToFile(TPath.Combine(TPath.GetDocumentsPath, 'PersonalData.txt'),
TEncoding.UTF8);
finally
DataFile.Free;
end;
end;

How to get the content of a cell on a DBGrid in Delphi by using the OnCellClick event

How can I, by clicking on a cell in the dbgrid on the form, get the selected cell's content?
Please note that Delphi's DBGrid is a data-aware grid and is slightly unusual compared with other grids (e.g. Delphi's TStringGrid) in that the cells of the
grid are not readily accessible using Row and Column values.
The easiest way to do this is simply
procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
S : String;
begin
S := DBGrid1.SelectedField.AsString;
Caption := S;
end;
It works because the way TDBGrid is coded, the associated dataset is
synchronized to the currently selected/clicked grid row. Generally speaking,
it's easiest to get values from the current record of the dataset, but you asked,
so. Try to avoid changing the current record's values by manipulating the cell's
text because the DBGrid will fight you every inch of the way.
Fwiw, I've seen more "round the houses" ways of getting the cell text, but
I prefer this on the KISS principle.
Note that a more robust way of getting the cell text, which includes
Remy Lebeau's suggestion to use Column.Field instead of SelectedField,
is as follows:
procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
S : String;
AField : TField;
begin
AField := DBGrid1.SelectedField;
// OR AField := Column.Field;
// Note: If the DBGrid happens to have an unbound column (one with
// no TField assigned to it) the AField obtained mat be Nil if
// it is the unbound column which is clicked. So we should check for
// AField being Nil
if AField <> Nil then begin
S := AField.AsString;
Caption := S;
end;
end;

how can I add an item in specific position

I have two forms which are extended from another form. I want to add some fields to one of them but not at the bottom of the form. I use add method and my items are added at the bottom of the form.
Does anyone have an idea?
You need to use insert method on container instead of container.add to update the layout.
The insert takes two args,
first : index to insert at
second: the element to be inserted (could by a couple of other things)
In your case the form serves as the container so you need to do something like (assuming you are on Ext 5.x or lower):
var newField = Ext.create('Ext.form.field.Field', {});
var insertAt = 3;
yourForm.insert(insertAt, newField);
Take a look at the fiddle as well: https://fiddle.sencha.com/#view/editor&fiddle/2e0i
Edit: Removed the hacky way

SmartGWT Dynamic Form edit record with boolean values

I've got following problem using SmartGWT 2.4:
we are having a DynamicForm showing several static text fields (so the form is in readonly mode). The form uses a datasource in the background and our own FormItemFactory to create proper form items based on our meta data. Some of the form items contain boolean values displayed as strings: like 'isHidden': false or 'canShow': true.
by user action (button click) we need to switch the form to edit mode.
We do it in following way:
we first gather the form values as rec = form.getValuesAsRecord() getting a Record object
then we create a new dynamic form and set into it the same datasource as original has
then we call the newForm.editRecord(rec) method of newly created dynamic form
This way the form static values are shown as editable input fields. However the problem is with those boolean values. They are correctly transformed into check boxes but all of them are checked by default.
I think that the string values 'false' or 'true' are not parsed into boolean values and set as value for respective check box item.
Can I somehow influence this process? I tried to provide an anonymous implementation of FormItemValueParser to CheckboxItem but it turns out to be use only by free text form items.
I'll be really thankful for any given hint.
Try setting the value explicitly to the formItem with record.getAttributeAsBoolean("formItemName")
BooleanItem boolItem = new BooleanItem("boolname");
DynamicForm form = new DynamicForm();
form.setItems(boolItem);
//Get record
Record rec = form.getValuesAsRecord();
boolItem.setValue("boolname",rec.getAttributeAsBoolean("boolname"));

Resources