Set ComboBox CellType so no item is selected

Posted by: imgodot on 8 September 2017, 3:25 pm EST

  • Posted 8 September 2017, 3:25 pm EST

    How to I set a combo in a cell so that none of the dropdown items are selected?

    This is so simple, but I just can't find it.

    Thanks.
    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Hello Paul,

    You may set it this way:-

                FarPoint.Win.Spread.CellType.ComboBoxCellType cmbocell = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
    cmbocell.Items = (new String[] { "January", "February", "March", "April", "May", "June" });
    fpSpread1.ActiveSheet.Columns[0].CellType = cmbocell;


    Hope it helps.

    Thanks,
    Reeva
  • Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    I sincerely apologize for the delay in our response. I tried this out, and it looks like you should be able to use one of Spread's built-in events, particularly the Change event. For example, if you wanted to check if the text was null so that you can change it to something else for your database, you could use that event:

    http://sphelp.grapecity.com/WebHelp/SpreadNet10/WF/FarPoint.Win.Spread~FarPoint.Win.Spread.FpSpread~Change_EV.html

    private void fpSpread1_Change(object sender, FarPoint.Win.Spread.ChangeEventArgs e)
    {
    if(fpSpread1.ActiveSheet.Cells[e.Row, e.Column].Text == "")
    //translate null value
    }


    In addition, I apologize but I'm not sure what you mean in your bonus question. Combo boxes show the text for the options regardless of whether they can be edited. Would it be possible to please go into a little more detail about that particular issue?

    Regards,
    Kevin
  • Replied 8 September 2017, 3:25 pm EST

    Reeva, Thanks for the response.

    However, there are a couple of issues with setting the value to null.
    1) The key event is only recognized when:
    a. EditMode is off, OR,
    b. The dropdown list is displayed.
    That means when the combo is in editmode and the list is not displayed, no key event.

    2) Setting the combo value to null will trigger a validation error because our databases columns do not allow nulls.

    SO, my questions:
    1) How do I set the combo to null if in editmode and no list is displayed?
    2) How do I intercept the update of the combo cell to translate its null value to a value that is usable by the database?
    (I'm pretty sure I know the answer, but I'll let you verify it for me.)

    Bonus question:
    3) How can I prevent a combo from displaying the current text value as if it is editable, when it isn't?
    (I'm pretty sure I know the answer to this one too but, again, I'll wait for your answer.)

    Thanks.
    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Hello,

    Here is the code to delete selected combobox item when pressing delete key:-

            private void Form1_Load(object sender, EventArgs e)
    {
    string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"C:\Program Files (x86)\GrapeCity\Spread Studio 10\Common\Nwind.mdb";
    OleDbConnection conn = new OleDbConnection(connString);
    OleDbCommand comm = new OleDbCommand("Select * from Employees", conn);
    OleDbDataAdapter adap = new OleDbDataAdapter(comm);
    DataSet ds = new DataSet();
    adap.Fill(ds);
    fpSpread1.DataSource = ds.Tables[0].AsDataView();
    // Changing one column to ComboBoxCellType.
    string[] arr = new string[ds.Tables[0].Rows.Count];
    for (int r = 0; r < ds.Tables[0].Rows.Count; r++)
    arr[r] = ds.Tables[0].Rows[r]["Title"].ToString();

    FarPoint.Win.Spread.CellType.ComboBoxCellType cmbocell = new FarPoint.Win.Spread.CellType.ComboBoxCellType();
    cmbocell.Items = arr;
    fpSpread1.ActiveSheet.Columns[3].CellType = cmbocell;

    fpSpread1.KeyDown += fpSpread1_KeyDown;
    }
    void fpSpread1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Delete)
    if (fpSpread1.ActiveSheet.ActiveColumnIndex == 3)
    fpSpread1.ActiveSheet.Cells[fpSpread1.ActiveSheet.ActiveRowIndex, fpSpread1.ActiveSheet.ActiveColumnIndex].Value = null;
    }


    Hope it helps.

    Thanks,
    Reeva
  • Replied 8 September 2017, 3:25 pm EST

    Kevin,

    A) Using the Change() event: In the "translate null value" code, what needs to be done there?
    Change the cell Value?
    Set the datasource column value?

    B) The KeyDown event is not always recognized by a combo cell (see previous post).
    So, how do I reliably catch the Delete key so I can set the cell value?

    [The bonus]
    C) When I click in a combo cell, the value in the text area can be selected like it is editable text but we have set Editable = FALSE.
    How can we make the text value NOT look editable and not be selectable?
    I see no standard property to allow this but, perhaps I missed it or, perhaps it requires a custom cell-type.

    Thanks.
    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    A) The translate null value code would be whatever you wanted the behavior to be:
    If you wanted to manually change the cell value so that it shows as whatever value that you are setting for null values, you could do that and wait until some other conditions are meant before you update the database. Alternatively, you could set the datasource column value instead, it just depends on when you wanted to update your database.

    B) It sounds like the issue here is that the delete key isn't recognized when the cell is in edit mode because the editor is the currently active control rather than the Spread control, which is what the keydown event is bound to. I will need to ask the developers about this for further clarification and get back to you. I don't know if it would require a custom cell type or not.

    C) I see this behavior: when clicking in a combo box cell, the text that is in what would be considered the text editor is highlighted as if you would be able to edit it, but you have the property set to Editable = false, so you don't want that look. It should just be standard text that isn't highlighted. This appears to be the standard behavior of the ComboBoxCellType, so it looks like it might require a custom cell type. I will need to ask the developers what internal code is related to that, though it is probably related to the paint function.

    The tracking number for this issue is 236511.

    Regards,
    Kevin
  • Replied 8 September 2017, 3:25 pm EST

    Reeva,

    Thanks for the reply but now, looking back, I realize I was to anxious to leave work and phrased my question poorly.

    Let me be more clear and hopefully you can help:
    A) I have a grid that is databound.
    B) I have a combo assigned to a cell in the grid.
    C) The combo is already populated with items.
    D) The combo works perfectly displaying and updating data from the datasource.
    E) The combo shows the current value from the datasource.

    What I want is the user to be able to press the delete key and have the combo set so no item in the list is selected.
    This is analogous to how the standalone C1Combo control behaves when you set: SelectedIndex = -1

    How do I do that?

    Thanks.
    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Kevin,

    I updated my custom combobox cell-type for handling the Delete key to clear the combo value.
    I couldn't get it to work right so, while I work with that some more, perhaps you could answer another question that recapitulates a prior one.

    Is there any way to capture key strokes inside a custom combobox cell-type when the dropdown list is displayed (i.e. the subeditor is active).

    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Kevin,

    Thanks for the reply and I have more questions (of course).

    B) So let me get this straight; in order to properly handle delete key processing we need to:
    1. Create a custom cell-type with key handling, and
    2. Add code to the Spread key handling, and
    3. Add code to the Spread Change event.
    That seems like a lot of hassle just for one, pretty simple, requirement.
    QUESTION 1: There is no was to capture all of the key handling within the custom combo?
    QUESTION 2: There is no was to perform the translation of the null value into a value meaningful to the database within the custom combo?

    C) I understand, though I don't see any overriding usability benefit to highlighting the value in the text control. The user already knows the value has to be in the combo. The important part is that the selected value is highlighted in the list, as it does.
    But, be that as it may, perhaps there may be something else done to prevent the non-editable text string from looking editable.
    QUESTION 3: Can the text display value be highlighted like it is in a standalone C1 combo control when the list is displayed?
    (The entire background of the text box is filled with the highlight color.)

    Thanks.
    -- Paul

  • Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    B) That was just the way that I had implemented it, but the key points is that you somehow need to add a handler for the keydown event for the editor of the celltype, which I did through the GetEditorControl function in the custom cell type.

    1. As the developers stated, the reason why you can't do it all in one place is because the editor handles its own key events. Typically if you pressed the delete key when inside the editor, it would act as a standard delete key (and delete a character in front of the cursor). So if you wanted to customize it to have it delete all the characters in the editor, it would need its own KeyDown event handler.

    2. I don't see any reason why not. You would just override the parse method of the celltype and implement your own code for converting the string that is typed in to the value that you want to submit to the database.

    http://sphelp.grapecity.com/WebHelp/SpreadNet10/WF/FarPoint.Win.Spread~FarPoint.Win.Spread.CellType.ComboBoxCellType~Parse.html

    3. I see what you mean now, you want it to look like the editor is just a label or something where the whole background of it is using the highlighted color when selected, rather than just the text itself being highlighted. I will need to ask the developers how to do that and get back to you.

    Regards,
    Kevin
  • Replied 8 September 2017, 3:25 pm EST

    Kevin,

    1) I understand the "reason" but, it is non-intuitive and hurts code encapsulation.
    When the combo cell has focus, whether in the textbox or with the dropdown displayed, you would think a custom cell-type would be able to catch all events related to the cell-type and not just some. It would be nice to be able to catch all key events within the custom cell-type regardless of whether the list is displayed or not.

    2) I will take a stab at this.

    3) Thanks.

    -- Paul
  • Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    That functionality can be accomplished by attaching an event handler to the editor itself , as I described in a previous reply on this forum:
    class MyCombo : FarPoint.Win.Spread.CellType.ComboBoxCellType
    {
    FarPoint.Win.Spread.FpSpread fpSpread1;
    public MyCombo(FarPoint.Win.Spread.FpSpread parentSpread)
    {
    this.fpSpread1 = parentSpread;
    }

    public override Control GetEditorControl(FarPoint.Win.Spread.Appearance appearance, float zoomFactor)
    {
    var comboBox = base.GetEditorControl(appearance, zoomFactor);
    comboBox.KeyDown += ComboBox_KeyDown;
    return comboBox;
    }

    private void ComboBox_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Delete)
    if (fpSpread1.ActiveSheet.ActiveColumnIndex == 3)
    fpSpread1.ActiveSheet.Cells[fpSpread1.ActiveSheet.ActiveRowIndex, fpSpread1.ActiveSheet.ActiveColumnIndex].Value = null;
    }
    }


    Regards,
    Kevin
  • Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    The developers have looked into the issue and this is the feedback they had:

    B) The reason why you can't capture the delete key in edit mode is because when the comboboxCellType is in editMode, its editor will be handling key events. This means that Spread's KeyDown event will not be raised. Instead, you will need to bind to the editor's KeyDown event. I found this was easily done with a custom cell type that inherits the ComboBoxCellType:


    class MyCombo : FarPoint.Win.Spread.CellType.ComboBoxCellType
    {
    FarPoint.Win.Spread.FpSpread fpSpread1;
    public MyCombo(FarPoint.Win.Spread.FpSpread parentSpread)
    {
    this.fpSpread1 = parentSpread;
    }

    public override Control GetEditorControl(FarPoint.Win.Spread.Appearance appearance, float zoomFactor)
    {
    var comboBox = base.GetEditorControl(appearance, zoomFactor);
    comboBox.KeyDown += ComboBox_KeyDown;
    return comboBox;
    }

    private void ComboBox_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Delete)
    if (fpSpread1.ActiveSheet.ActiveColumnIndex == 3)
    fpSpread1.ActiveSheet.Cells[fpSpread1.ActiveSheet.ActiveRowIndex, fpSpread1.ActiveSheet.ActiveColumnIndex].Value = null;
    }
    }


    C) This behavior is as intended when ComboBoxCellType.Editable = false:

    - The text in the Editor of the ComboBoxCellType is not highlighted when double-clicking on the cell and the DropDownList of the ComboBox is not displayed.
    - The text in the Editor of the ComboBoxCellType is highlighted when the DropDownList of the ComboBox is displayed, because we want to show that the selected item corresponds to a specific item in the DropDownList.

    You can download a sample showing this from here:

    http://cdn.grapecity.com/support/spread/Kevin%20Ashley/Customer%20Tickets/236511%20SpreadWinformsCSharp.zip

    Let me know if that helps.

    Regards,
    Kevin
  • Marked as Answer

    Replied 8 September 2017, 3:25 pm EST

    Hi Paul,

    You are right, "Delete" key is absorbed by the Spread's KeyDown event and it never reaches the ComboBox. In case you want to handle the "Delete" key outside your combobox you can use the code as follows:
     private void fpSpread1_ComboDropDown(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
    {
    flag= true;

    }
    private void fpSpread1_KeyDown(object sender, KeyEventArgs e)
    {
    if (flag == true) {
    e.Handled = true;
    fpSpread1.ActiveSheet.ActiveCell.Value = null;
    }
    }
    private void fpSpread1_ComboCloseUp(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
    {
    flag = false;
    }


    Thanks,
    Deepak Sharma
  • Replied 8 September 2017, 3:25 pm EST

    Kevin,

    Yes, we have been over this.
    However, the KeyDown event within a custom combo cell-type is NOT triggered by a keypress when the dropdown list is displayed.
    When the dropdown list is displayed the KeyDown is caught by the Spread activesheet KeyDown event.

    I want the key stroke events to all be handled within the custom combo cell-type to avoid having duplicate code in the custom combo cell-type and the spread KeyDown event.

    Even though the devs says it can't, I was hoping perhaps there was a way of getting at the key events via the subeditor that the devs didn't consider.

    -- Paul
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels