ListBox Control Winforms Selected Item Color - Multiple List Box Controls












1















I have 5 list box controls on a single windows form. I'm setting the enabled property to false initially to control read/edit access. This makes it hard to read the selected value without enabling the control. I'm using the enabled property as the ListBox control doesn't have a read-only property like TextBox controls.



I've seen approaches online where I can set



ListBox1.DrawMode = DrawMode.OwnerDrawFixed


And use Draw_Item to control the select item colour.



Private Sub ListBox1_DrawItem(ByVal sender As Object, _  ByVal e As System.Windows.Forms.DrawItemEventArgs) _


However, I've not seen an approach for applying this to multiple ListBox controls on a form. I don't really want to have to tap into Draw_Item for each ListBox.



Also, the ListBox controls are data bound to a DataSet. Anyone had this issue before? If so, how did you handle it?



Or does one know a way of controlling whether a ListBox control is read-only or selectable based on view/edit state?



Thanks in advance for any tips.










share|improve this question























  • It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

    – Jimi
    Nov 21 '18 at 13:30











  • Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

    – Andrew Greatorex
    Nov 21 '18 at 13:35











  • ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

    – Jimi
    Nov 21 '18 at 13:57








  • 2





    Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

    – Jimi
    Nov 21 '18 at 14:19






  • 1





    I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

    – preciousbetine
    Nov 22 '18 at 1:39
















1















I have 5 list box controls on a single windows form. I'm setting the enabled property to false initially to control read/edit access. This makes it hard to read the selected value without enabling the control. I'm using the enabled property as the ListBox control doesn't have a read-only property like TextBox controls.



I've seen approaches online where I can set



ListBox1.DrawMode = DrawMode.OwnerDrawFixed


And use Draw_Item to control the select item colour.



Private Sub ListBox1_DrawItem(ByVal sender As Object, _  ByVal e As System.Windows.Forms.DrawItemEventArgs) _


However, I've not seen an approach for applying this to multiple ListBox controls on a form. I don't really want to have to tap into Draw_Item for each ListBox.



Also, the ListBox controls are data bound to a DataSet. Anyone had this issue before? If so, how did you handle it?



Or does one know a way of controlling whether a ListBox control is read-only or selectable based on view/edit state?



Thanks in advance for any tips.










share|improve this question























  • It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

    – Jimi
    Nov 21 '18 at 13:30











  • Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

    – Andrew Greatorex
    Nov 21 '18 at 13:35











  • ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

    – Jimi
    Nov 21 '18 at 13:57








  • 2





    Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

    – Jimi
    Nov 21 '18 at 14:19






  • 1





    I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

    – preciousbetine
    Nov 22 '18 at 1:39














1












1








1








I have 5 list box controls on a single windows form. I'm setting the enabled property to false initially to control read/edit access. This makes it hard to read the selected value without enabling the control. I'm using the enabled property as the ListBox control doesn't have a read-only property like TextBox controls.



I've seen approaches online where I can set



ListBox1.DrawMode = DrawMode.OwnerDrawFixed


And use Draw_Item to control the select item colour.



Private Sub ListBox1_DrawItem(ByVal sender As Object, _  ByVal e As System.Windows.Forms.DrawItemEventArgs) _


However, I've not seen an approach for applying this to multiple ListBox controls on a form. I don't really want to have to tap into Draw_Item for each ListBox.



Also, the ListBox controls are data bound to a DataSet. Anyone had this issue before? If so, how did you handle it?



Or does one know a way of controlling whether a ListBox control is read-only or selectable based on view/edit state?



Thanks in advance for any tips.










share|improve this question














I have 5 list box controls on a single windows form. I'm setting the enabled property to false initially to control read/edit access. This makes it hard to read the selected value without enabling the control. I'm using the enabled property as the ListBox control doesn't have a read-only property like TextBox controls.



I've seen approaches online where I can set



ListBox1.DrawMode = DrawMode.OwnerDrawFixed


And use Draw_Item to control the select item colour.



Private Sub ListBox1_DrawItem(ByVal sender As Object, _  ByVal e As System.Windows.Forms.DrawItemEventArgs) _


However, I've not seen an approach for applying this to multiple ListBox controls on a form. I don't really want to have to tap into Draw_Item for each ListBox.



Also, the ListBox controls are data bound to a DataSet. Anyone had this issue before? If so, how did you handle it?



Or does one know a way of controlling whether a ListBox control is read-only or selectable based on view/edit state?



Thanks in advance for any tips.







vb.net winforms listbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 13:21









Andrew GreatorexAndrew Greatorex

527




527













  • It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

    – Jimi
    Nov 21 '18 at 13:30











  • Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

    – Andrew Greatorex
    Nov 21 '18 at 13:35











  • ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

    – Jimi
    Nov 21 '18 at 13:57








  • 2





    Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

    – Jimi
    Nov 21 '18 at 14:19






  • 1





    I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

    – preciousbetine
    Nov 22 '18 at 1:39



















  • It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

    – Jimi
    Nov 21 '18 at 13:30











  • Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

    – Andrew Greatorex
    Nov 21 '18 at 13:35











  • ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

    – Jimi
    Nov 21 '18 at 13:57








  • 2





    Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

    – Jimi
    Nov 21 '18 at 14:19






  • 1





    I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

    – preciousbetine
    Nov 22 '18 at 1:39

















It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

– Jimi
Nov 21 '18 at 13:30





It appears that you're not looking for a solution for the "problem", but a solution for the solution you came up with to solve the "problem". There's a two-letters-name for this situation. Maybe, you could explain why you need to disable those controls in the first place.

– Jimi
Nov 21 '18 at 13:30













Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

– Andrew Greatorex
Nov 21 '18 at 13:35





Hi, sure, the reason to disable them is so users cannot change them until they explicitly put the controls into edit mode. With TextBox controls I can use .ReadOnly, but the only thing I have on a ListBox is Enabled.

– Andrew Greatorex
Nov 21 '18 at 13:35













ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

– Jimi
Nov 21 '18 at 13:57







ListBoxes are not editable, so this doesn't explain much. If, with edit mode, you mean you have some procedure that allows to insert/remove a ListBox's item(s), you should focus on that procedure. You can change the ForeColor of the Items in a disable ListBox(s), owner-drawing it, but is it really necessary? Doesn't it cause some confusion at some point? Can you manage it without causing collateral issues (e.g., Screen DPI changes, Font sizes, UI resizing)?

– Jimi
Nov 21 '18 at 13:57






2




2





Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

– Jimi
Nov 21 '18 at 14:19





Well, you could (just a suggestion) have a Boolean field (e.g., IsEditMode) that is set to false before editing is allowed. In the SelectedIndexChanged event handler you could write something like: If Not IsEditMode Then ListBox1.SelectedIndex = -1 Return End If. This can give a visual clue that an Item can't be selected and any other code in the handler won't be executed.

– Jimi
Nov 21 '18 at 14:19




1




1





I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

– preciousbetine
Nov 22 '18 at 1:39





I think i know what you are trying to pass across, when the listbox is disabled, the items on it cannot be seen properly, You can change the listbox's forecolor to a brighter color before disabling it.

– preciousbetine
Nov 22 '18 at 1:39












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412994%2flistbox-control-winforms-selected-item-color-multiple-list-box-controls%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412994%2flistbox-control-winforms-selected-item-color-multiple-list-box-controls%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy