Lainkes 0 Posted October 6, 2022 (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 October 6, 2022 by Lainkes Share this post Link to post
Lainkes 0 Posted October 6, 2022 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
Uwe Raabe 2057 Posted October 6, 2022 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
Lainkes 0 Posted October 6, 2022 That's indeed my problem. So I guess it's not possible to create that. Share this post Link to post
Uwe Raabe 2057 Posted October 6, 2022 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
Uwe Raabe 2057 Posted October 6, 2022 @haentschman Not all of the lower numbers still exist... Share this post Link to post
Lainkes 0 Posted October 6, 2022 (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 October 6, 2022 by Lainkes Share this post Link to post
haentschman 92 Posted October 6, 2022 (edited) Clear... I have only TForm1 always renamed to foBlubb etc. Edited October 6, 2022 by haentschman Share this post Link to post
Uwe Raabe 2057 Posted October 6, 2022 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
Stano 143 Posted October 6, 2022 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
Lainkes 0 Posted October 7, 2022 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
Uwe Raabe 2057 Posted October 7, 2022 Are you sure that the dataset is open? Share this post Link to post
Lainkes 0 Posted October 7, 2022 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
Fr0sT.Brutal 900 Posted October 10, 2022 I'd tie events to tdataset fields rather than to controls. Share this post Link to post
Lainkes 0 Posted October 10, 2022 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
PeterBelow 238 Posted October 10, 2022 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
Lainkes 0 Posted October 10, 2022 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
Stano 143 Posted October 10, 2022 Will setting all affected DB table fields to False (or null) help? Share this post Link to post
Lainkes 0 Posted October 10, 2022 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
Stano 143 Posted October 11, 2022 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