Jump to content
Lainkes

DBRadiobutton

Recommended Posts

Posted (edited)

Hello, 

 

I want to make a GroupBox component, and there I need DBCheckboxes.  

But I want that the user can select only checkbox. In general I want to copy the checkbox behaviour, since there is no DBRadiobutton component.

I use this for reporting purpose. At the end, I just count the number of True values in a field to have a statistic.

With a DBRadiogroup, I get the value of the selected radiobutton. But that's not what I need.

 

Is that possible with the standard components? Or are there some components on the net that I can use?


Thanks for your answer.

Edited by Lainkes

Share this post


Link to post

Let's say I have 5 fields in my table.

I want to link each of them to a DBCheckbox component (in a TGroupbox). But only one can be checked. So the behavour is Radiobuttons.

So for every record in my table 4 of the 5 fields have the value False, and 1 value is True.

At the end, I can do a count of the number of True values of each field. 

Using if statements can be a solution, but that's not clean in my opinion. I have some groups where I can chose between 20 choices. 

Share this post


Link to post
1 minute ago, Lainkes said:

I want to link each of them to a DBCheckbox component (in a TGroupbox). But only one can be checked.

I cannot reproduce that. If I have several TDBCheckBox inside a TGroupBox each can be switched independently.

Share this post


Link to post

That's indeed my problem.

So I guess it's not possible to create that. 

 

Share this post


Link to post

Oh, now I understand what you want to achieve.

 

One solution would be to wire this event handler to each TDBCheckBox inside the TGroupBox:

var
  DisableEvent: Boolean = False;

procedure TForm747.DBCheckBoxClick(Sender: TObject);
begin
  if DisableEvent then Exit;

  DisableEvent := True;
  try
    var grp := (Sender as TControl).Parent as TGroupBox;
    for var I := 0 to grp.ControlCount - 1 do
      if grp.Controls[I] is TDBCheckBox then begin
        var chk := TDBCheckBox(grp.Controls[I]);
        chk.Checked := chk = Sender;
      end;
  finally
    DisableEvent := False;
  end;
end;

 

Share this post


Link to post
Posted (edited)

Thanks for the code.

This seems to work like I wanted.

The only problem is that all the fields are getting the value True when saving the record.

I linked all the components to the same event. Maybe that's not the correct way to handle.

Edited by Lainkes

Share this post


Link to post
Posted (edited)

Clear...:classic_cool: I have only TForm1 always renamed to foBlubb etc. :classic_cheerleader:

Edited by haentschman

Share this post


Link to post
1 hour ago, Lainkes said:

I linked all the components to the same event. Maybe that's not the correct way to handle.

Actually that is exactly the way to do it. It just seems that setting the Checked property of TDBCheckBox is not propagated to the underlying field. I need to investigate that and file a bug report as soon as time allows.

 

As a workaround don't set the Checked property but the field value directly:

        chk.Field.AsBoolean := chk = Sender;

 

Share this post


Link to post

In similar cases, I basically set the value of the field. My experience: changing the value of the component (DB TMS) does not change the value of the DB field. I assume that the appropriate event is not triggered, because it happens programmatically. I don't consider it a mistake. If I change the value of the DB field, the components always respond correctly.
For me, it doesn't really matter what I set. I have significantly longer text when writing to the DB. For that write-up, I use the given component directly and NOT the DB table.

Share this post


Link to post

Good morning.

When changing the code I get an access violation error. 

I guess it has to do with the change of code.

Any idea what is going wrong?

 

Many thanks

Share this post


Link to post

I restarted the Delphi program, and now your first code seems to work.

The selected checkbox has True, all the other fields are NULL.

So that's a very good way to work.


Many thanks for this solution.

Share this post


Link to post

I have a question.
When I start my program, there is always one of the DBCheckboxes that is active.

How can I disable that. So that by default no value is selected.

 

Thanks

Share this post


Link to post
9 minutes ago, Lainkes said:

I have a question.
When I start my program, there is always one of the DBCheckboxes that is active.

How can I disable that. So that by default no value is selected.

 

Thanks

Set the form's ActiveControl property to some other control that can take the focus, e.g. a button.

Share this post


Link to post

It does not work.

To be complete, the checkboxes are on a groupbox component, and these are on a pagecontrol component.

I set the ActiveControl property of the form to the Save button of the form.

 

Share this post


Link to post

Will setting all affected DB table fields to False (or null) help?

Share this post


Link to post

If I call the form, all DBCheckbox components should be unchecked.

I tried to loop through all the components, but it does not help. 
Maybe I did that not in a correct way.

Share this post


Link to post

If they are connected to the DB table and it is open, then their state reflects the state of the field for the current record! It must be so.
Try it: Set them to show 3 statuses. DataSource.Enabled = False.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×