new2csharp
January 29th, 2008, 05:22 PM
i have a checkedListBox with 35 items in it and i want to make it so that you can only select one item at a time. I can't figure out how to do that. can you help?
|
Click to See Complete Forum and Search --> : problems with properties. new2csharp January 29th, 2008, 05:22 PM i have a checkedListBox with 35 items in it and i want to make it so that you can only select one item at a time. I can't figure out how to do that. can you help? JonnyPoet January 29th, 2008, 06:13 PM i have a checkedListBox with 35 items in it and i want to make it so that you can only select one item at a time. I can't figure out how to do that. can you help?If you are using a CheckedListBox this is the Standard way it does. It has no multirow selection you can only use SelectionMode One or none in the properties new2csharp January 29th, 2008, 06:23 PM but if i choose selection mode one, it still allows multiple selections. i only want them to beable to choose one jmcilhinney January 29th, 2008, 07:03 PM You are confusing checking an item with selecting it. When you click and item it is selected and highlighted in blue. That has no relationship whatsoever to whether or not the box next to the item is checked. You're saying that you only want the user to be able to check[U] one item at a time. You have two choices: 1. Handle the appropriate event of your CheckedListBox and uncheck every other item when an item is checked. This is fine if you won't ever need this functionality again.Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, _ ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck If e.NewValue = CheckState.Checked Then 'An item is being checked so as long as there are items currently checked... While Me.CheckedListBox1.CheckedItems.Count > 0 '...uncheck the first checked item. Me.CheckedListBox1.SetItemChecked(Me.CheckedListBox1.CheckedIndices(0), False) End While End If End Sub2. Define your own class derived from the CheckedListBox class and add the same functionality as mentioned above. This is a better solution because you have encapsulated the behaviour in a reusable class that you can simply use instead of a CheckedListBox.Imports System.ComponentModel Public Class CheckedListBoxEx Inherits System.Windows.Forms.CheckedListBox Private _allowMultiCheck As Boolean = True <Category("Behavior"), _ DefaultValue(True), _ Description("Indicates whether multiple items can be checked.")> _ Public Property AllowMultiCheck() As Boolean Get Return Me._allowMultiCheck End Get Set(ByVal value As Boolean) If Me._allowMultiCheck <> value Then Me._allowMultiCheck = value Me.OnAllowMultiCheckChanged(EventArgs.Empty) If Not Me._allowMultiCheck Then 'Multiple checked items are no longer allowed so make sure there is only one checked item. While Me.CheckedItems.Count > 1 Me.SetItemChecked(Me.CheckedIndices(1), False) End While End If End If End Set End Property <Category("Behavior"), _ Description("Occurs when the value of the AllowMultiCheck property changes.")> _ Public Event AllowMultiCheckChanged As EventHandler Protected Overridable Sub OnAllowMultiCheckChanged(ByVal e As EventArgs) RaiseEvent AllowMultiCheckChanged(Me, e) End Sub Protected Overrides Sub OnItemCheck(ByVal e As ItemCheckEventArgs) MyBase.OnItemCheck(e) 'If multiple items are not allowed to be checked and an item is being checked... If Not Me.AllowMultiCheck AndAlso e.NewValue = CheckState.Checked Then '...as long as there are items checked... While Me.CheckedItems.Count > 0 '...uncheck the first checked item. Me.SetItemChecked(Me.CheckedIndices(0), False) End While End If End Sub End ClassIf you choose the second option then you can simply add a CheckedListBoxEx to your form instead of a regular CheckedListBox. It will behave the same by default but if you set the AllowMultiCheck property to False then the items will act like radio buttons. This is also a good example of how and why you might inherit a class. Finally, if you do go for the second option then you don't have to delete your existing CheckedListBox and add a new CheckedListBoxEx. You just open the designer code file and do a find and replace to swap your class name for System.Windows.Foms.CheckedListBox. jmcilhinney January 29th, 2008, 07:05 PM Ah b*gger! I just realised that I'm in the C# forum and not VB.NET. That said, the principles are all exactly the same. You'd just have to write the code yourself in C#. There are various code converters available online and there's the trial version of Instant C# too. new2csharp January 29th, 2008, 07:07 PM thanks for the reply, I see now what you mean by checking and selecting. However, I don't know enough about c sharp to write my own class or write an event handler to uncheck every other box when one box is checked. new2csharp January 29th, 2008, 07:16 PM well i took a hint from your code and tried if(checkedListBox1.CheckedItems.Count > 1) MessageBox.Show("error"); and that works, but it prints the error message for every checkbox that is checked. jmcilhinney January 29th, 2008, 10:23 PM When I said:There are various code converters available online and there's the trial version of Instant C# too.what did you do? Did you go to Google and search for a code converter? If you want to go the first option then you create an event handler from the Properties window, clicking the Events button first. Once you've got the method created for you it's just a matter of adding the code. As you can see from the VB, you need an 'if' statement and a 'while' statement. C# has both so the code will be almost the same. If you don't know how to write an 'if' statement or 'while' statement then the MSDN Library awaits. new2csharp January 29th, 2008, 10:33 PM i do know how to write an if statement and a while statement. i just looked at the code you wrote and thought that some of it would work for c sharp and i was right. I will keep trying. it's not working perfectly yet jmcilhinney January 29th, 2008, 10:43 PM VB and C# really are very similar in a lot of respects. In many cases the only difference is a semicolon. Even when there are greater differences, a C# developer can still see the principles implemented in VB code and vice versa. Like I said, you can see that there's an If and a While so you can just implement those patterns in C#. The C# equivalent of that first code snippet will be almost exactly the same. Add a few parentheses, an equals sign, a few braces, a semicolon, then swap 'this' for 'Me' and a couple of brackets for parentheses and it's done. JonnyPoet January 30th, 2008, 06:35 AM Hi ! I have a basic question to your design. As it now is cleared that you have meant checked and uncheckednstead of selected unselected my question is: Whats the design pattern behind that idea. Brcause this checkbuttons are standard in the way they work. So why you need to have only one of them ckecked. This would mean that only the selected one stays checked. Whats the intention behind ? Becaue which one is selected you also can see on the blue line which shows the selected item. This wouldn't need checkbuttons. ? On the general coding issue as you mentioned you are totally new to C#. I would suggest you to read a basic book like MS Visual C# 2006 step by step of MS press or the great cheap online book of apress named 'Illlustrated C# 2005' by Daniel Solis. It has lots of explanations and drawings, easy to follow and lots of examples. This way you will larn a lot about this language, how to use it and how to design solutions. TheCPUWizard January 30th, 2008, 07:41 AM It really sounds (based on the limited information posted) that what is wanted is a radio button group....... new2csharp January 30th, 2008, 11:29 AM i tried the list box, checkedlistbox, the table layout panel, everything except the radio buttons. Here is what my project is. There are 35 names. each name has a set of values that are 5 to 10 lines long. when you select a name, you press the generate values button and it pops those values into a rtb right next to the checkedlistbox. then you can copy those values and paste them into another document. I went with the checkedlistbox because it scrolls real nicely and because as i was getting help, this is the direction it went. So if the user acidently selects more than one box, it could print more than one value. The chances of that arent very good, but i want to program it so that it's not possible just to be safe. So if you were to click the firs box wich has the name "Aftermath" you would get these values "contrastGain" "0.15" "diffusefraction" "0.45" "_color" "0.90 0.90 1" "sunlight" "1" "sundirection" "-35 150 0" "sundiffusecolor" "1 1 1" "suncolor" "1 1 1" "ambient" ".15" They are sunlight values for a video game called call of duty 4 - modern warfare. TheCPUWizard January 30th, 2008, 12:15 PM So stay with the design you have and just code the event handler as previously suggested... new2csharp January 30th, 2008, 12:18 PM i am staying with the design i have now. i got the right click menu working from the help above, now i am trying to figure out how to actually get it to cut copy and paste. i feel like i am missing some code. I found it days earlier but can't find it again. Something that tells the menu items what to do. codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |