|
-
January 29th, 2008, 06:22 PM
#1
problems with properties.
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?
-
January 29th, 2008, 07:13 PM
#2
Re: problems with properties.
 Originally Posted by new2csharp
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
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
January 29th, 2008, 07:23 PM
#3
Re: problems with properties.
but if i choose selection mode one, it still allows multiple selections. i only want them to beable to choose one
-
January 29th, 2008, 08:03 PM
#4
Re: problems with properties.
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.
Code:
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 Sub
2. 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.
Code:
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 Class
If 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.
-
January 29th, 2008, 08:05 PM
#5
Re: problems with properties.
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.
-
January 29th, 2008, 08:07 PM
#6
Re: problems with properties.
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.
-
January 29th, 2008, 08:16 PM
#7
Re: problems with properties.
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.
-
January 29th, 2008, 11:23 PM
#8
Re: problems with properties.
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.
-
January 29th, 2008, 11:33 PM
#9
Re: problems with properties.
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
-
January 29th, 2008, 11:43 PM
#10
Re: problems with properties.
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.
-
January 30th, 2008, 07:35 AM
#11
Re: problems with properties.
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.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
January 30th, 2008, 08:41 AM
#12
Re: problems with properties.
It really sounds (based on the limited information posted) that what is wanted is a radio button group.......
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 30th, 2008, 12:29 PM
#13
Re: problems with properties.
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.
-
January 30th, 2008, 01:15 PM
#14
Re: problems with properties.
So stay with the design you have and just code the event handler as previously suggested...
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 30th, 2008, 01:18 PM
#15
Re: problems with properties.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|