How to enable a button in a delphi form only if all combo boxes have the selection made - combobox

Need to check if multiple Combo boxes are not empty.
Tried for one combo box, how to apply the same to multiple.
if (combobox1.ItemIndex <> -1) then
begin
btnOK.Enabled := true;
end
else
btnOK.Enabled := false;
PS: I'm new to delphi please bear with mistakes, if any.

The easiest would be a construct like this
if (ComboBox1.ItemIndex = -1) or (ComboBox2.ItemIndex = -1) or (ComboBox3.ItemIndex = -1) then
Exit;
Button1.Enabled := true;
This exits the procedure if any of the comboboxes has -1 selected. If all boxes are selected, the button gets enabled.
Or you could just bind them all together and write directly to the Enabled property
Button1.Enabled := (ComboBox1.ItemIndex <> -1) and (ComboBox2.ItemIndex <> -1) and (ComboBox3.ItemIndex <> -1);
The most beautiful way, if you really have a lot of comboboxes is using arrays
procedure TForm1.SetButtonActive;
var
boxes: array of TComboBox;
box: TComboBox;
begin
boxes := [ComboBox1, ComboBox2, ComboBox3];
for box in boxes do
if box.ItemIndex = -1 then
Exit;
Button1.Enabled := true;
end;

Related

Custom Action Delete/Add On ComboBox in wix Installer Toolset

I have a dialog (D1) that has a ComboBox where the items of the ComboBox is filled by a custom action (C++ dll).
UINT __stdcall FillComboBox(MSIHANDLE hInstall)
{
HRESULT hResult = WcaInitialize(hInstall, "FillComboBox");
if (FAILED(hResult)) return ERROR_INSTALL_FAILURE;
MSIHANDLE hTable = NULL;
MSIHANDLE hColumns = NULL;
hResult = WcaAddTempRecord(&hTable, &hColumns, L"ComboBox", NULL, 0, 3, L"COMBOBOXVALUES", 1, L"ABC");
hResult = WcaAddTempRecord(&hTable, &hColumns, L"ComboBox", NULL, 0, 3, L"COMBOBOXVALUES", 2, L"DEF");
hResult = WcaAddTempRecord(&hTable, &hColumns, L"ComboBox", NULL, 0, 3, L"COMBOBOXVALUES", 3, L"GHI");
}
When the user has selected an item the next button is enabled and another dialog (D2) is shown (if the user clicks the next button) with another combobox. The D2 combobox is also filled by a custom action in the same way as the first one but the selection of items is based on the item that was selected in the first dialog (D1).
My problem is if the user clicks the back button in dialog D2 and in the D1 dialog changes the selection of the Combobox and then clicks the next button (To move back to D2) I want to "reload" the items in the D2-ComboBox using the new value that was selected in D1.
Is there a way to clear an msi-table from the c++ code?
Can I delete the combobox table using function
MsiDatabaseOpenView(hDatabase, L"DELETE FROM 'ComboBox' WHERE 'Property' = 'COMBOBOXVALUES'", &hView)
Have you considered using a property to retain and share the values across the combo boxes?
Alternate way to add and delete the combobox item is to use DB concept.
You can do the direct operations on the Table writing SQL kind of query.
HRESULT hResult = WcaInitialize(hInstall, "FillComboBox");
if (FAILED(hResult)) return ERROR_INSTALL_FAILURE;
MSIHANDLE hView = 0;
MSIHANDLE hDatabase = 0;
LPWSTR query;
query = L"INSERT INTO ComboBox (Property, `Order`, `Value`, `Text`) VALUES ('COMBOBOXVALUES', 1, 'Test', 'Test') TEMPORARY";
hDatabase = MsiGetActiveDatabase(hInstall);
hResult = MsiDatabaseOpenView(hDatabase, query, &hView);
if (hResult == ERROR_SUCCESS)
{
hResult = MsiViewExecute(hView, 0);
}
To Delete the table you can use the below query.
query = L"DELETE FROM ComboBox";
In order to delete specific row you need to execute the below query.
query = L"DELETE FROM ComboBox WHERE `Order` = 1";

Immediately update DataGridViewCheckBoxCell

I am programmatically updating my WinForm DataGridView
Problem, DataGridViewCheckBoxCell doesn't get updated !!!
I was google, it seams like knowing case but whatever I've tried did not help yet.
private void InitializeFunctionsDataGrid()
{
System.Data.DataSet ds = func.GetFunctions();
this.FunctionsDataGrid.DataSource = ds.Tables[0];
this.FunctionsDataGrid.Columns["FunctionId"].Visible = false;
this.FunctionsDataGrid.Columns["DESCRIPTION"].Width = 370;
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Name = "enable";
column.HeaderText = "enable";
column.FalseValue = 0;
column.TrueValue = 1;
FunctionsDataGrid.Columns.Add(column);
foreach(DataGridViewRow row in FunctionsDataGrid.Rows)
{
(( DataGridViewCheckBoxCell)row.Cells["enable"]).Value = 1;
}
FunctionsDataGrid.CurrentCell = null;
}
enable is an unbound column. This means that you need to provide cell value yourself.
You can set the VirtualMode property to true and handle the CellValueNeeded event.
If you want to enable the user to check a cell then you need to handle the CellValuePushed event.
DataGridView samples that are part of the DataGridView FAQ has a specific example of an unbound checkbox column along with databound columns.
OK basically easiest way for me was to work with datasource.
I've add the column to the DataTable and fill it with data.
And then last thing this.FunctionsDataGrid.DataSource = ds.Tables[0];

Delphi. Set button position when form is created

I have some code, it will be create form with button, but when form show button has wrong position.
var
Form: TForm;
Button: TButton;
begin
Form := TForm.Create(Self);
try
Button := TButton.Create(Form);
Button.Parent := Form;
Button.Left := 8;
Button.Top := 8;
Button.Height := 185;
Button.Width := 292;
Button.Anchors := [];
Form.ShowModal;
finally
FreeAndNil(Form);
end;
Any idea how to solve this?
Don't clear the Anchors. That causes the button to move around when the Form is resized. Leave the default Anchors set to [akLeft,akTop] so the button will honor your Left/Top values.
When I use HandleNeeded before create controls
Form.HandleNeeded
When form show - button has correct position

XAMDATAGRID: Enable first row first cell in XAML or by Code

I am working with XamDataGrid where in i have to disable the Row:0 Cell:1 enabled all the times.
to achieve this i Set AllowEdit for the field to false and then dynamically onClick of the cell for selection: i am trying to get rowindex and cell index and see if they are for first row i set enable. But that enables entire column all rows which i dont want.
DataRecord selectedRecord = (DataRecord)frameSpacingDataGrid.ActiveRecord;
Cell selectedCell = frameSpacingDataGrid.ActiveCell;
CellCollection cellCollection = selectedRecord.Cells;
int indxSelectedCell = cellCollection.IndexOf(selectedCell);
var rowIndex = selectedRecord.Index;
if (rowIndex == 0 && indxSelectedCell == 0)
{
selectedCell.DataPresenter.IsEnabled= true;
}
Above code is written in SelectedItemsChanged event.
Also, when user deletes rows: new row:0 and cell:0 should be editable.
How do i achieve this Via Triggers/XAML/ Codebehind.
I have found a work around as stated here:
What i did is to set cancel for the edit event.
But surely this is not a proper solution.

DataGridViewComboBoxCell Value Not Updating When Set Programmatically C# Winforms

I've read all the similiar posts about this darn control (DataGridViewComboBoxCell) and setting it's value programmatically but implementing all the suggestions hasn't worked. I could probably change the UI to get around this problem but I don't like to be beat!
private void PopulateAreaForRoleAssociation()
{
// If businessRoleList is null then no data has been bound to the dgv so return
if (businessRoleList == null)
return;
// Ensure businessArea repository is instantiated
if (businessAreaRepository == null)
businessAreaRepository = new BusinessAreaRespository();
// Get a reference to the combobox column of the dgv
DataGridViewComboBoxColumn comboBoxBusinessAreaColumn = (DataGridViewComboBoxColumn)dgvBusinessRole.Columns["BusinessArea"];
// Set Datasource properties to fill combobox
comboBoxBusinessAreaColumn.DisplayMember = "Name";
comboBoxBusinessAreaColumn.ValueMember = "Id";
comboBoxBusinessAreaColumn.ValueType = typeof(Guid);
// Fill combobox with businessarea objects from list out of repository
comboBoxBusinessAreaColumn.DataSource = businessAreaRepository.GetAll();
// loop through the businessRoles list which the dgv is bound to and get out each dgv row based upon the current id in the loop
businessRoleList.Cast<BusinessRole>().ToList().ForEach(delegate(BusinessRole currentRole)
{
DataGridViewRow currentRowForRole = dgvBusinessRole.Rows.Cast<DataGridViewRow>().ToList().Find(row => ((BusinessRole)row.DataBoundItem).Id == currentRole.Id);
// Get a reference to the comboBox cell in the current row
DataGridViewComboBoxCell comboBoxCell = (DataGridViewComboBoxCell)currentRowForRole.Cells[2];
// Not sure if this is necessary since these properties should be inherited from the combobox column properties
comboBoxCell.DisplayMember = "Name";
comboBoxCell.ValueMember = "Id";
comboBoxCell.ValueType = typeof(Guid);
// Get the business area for the current business role
BusinessArea currentAreaForRole = businessAreaRepository.FetchByRoleId(currentRole.Id);
// if the role has an associated area then set the value of the cell to be the appropriate item in the combobox
// and update the cell value
if (currentAreaForRole != null)
{
foreach (BusinessArea area in comboBoxCell.Items)
{
if (currentAreaForRole.Id == area.Id)
{
comboBoxCell.Value = area.Id;
dgvBusinessRole.UpdateCellValue(2, comboBoxCell.RowIndex);
}
}
}
});
}
The dgv is first bound to a binding list holding BusinessRole objects, then the combobox column is bound to a basic list of BusinessArea objects that come out of a repository class. I then loop through the bindinglist and pull out the row of the dgv that is bound to the current item in the bindinglist loop.
With that row I make a database call to see if the BusinessRole entity is associated with a BusinessArea entity. If it is then I want to select the item in the combobox column that holds the BusinessAreas.
The problem is that when the grid is loaded, all the data is there and the comboboxes are populated with a list of available areas, however any values that are set are not displayed. The code that sets the value is definately getting hit and the value I am setting definately exists in the list.
There are no data errors, nothing. It's just refusing to update the UI with the value I programmatically set.
Any help would be greatly appreciated as always.
Thanks
This code works for my first combo box I have in my row with the following code, but I try it with the next one in the row and it doesn't work. I could use the help with the rest I have 3 more to do. I do set the combo boxes on a second form from this form with the same code as is on the last line of the try block but using the cell information instead of the dataset
try
{
string strQuery = "select fundCode from vwDefaultItems where IncomeType = '" + stIncome + "'";
SqlDataAdapter daDefault = new SqlDataAdapter(strQuery, conn);
DataSet dsDefault = new DataSet();
daDefault.Fill(dsDefault);
strDefFund = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
dgvCheckEntry.Rows[curRow].Cells[7].Value = dsDefault.Tables[0].Rows[0].ItemArray[0].ToString();
}
catch (Exception eq)
{
}

Resources