Posts

Showing posts from April, 2014

How To - Default Text on WPF Combo Boxes

Image
When you want to add a default text (i.e. Please Select/ Select Item etc.) to a ComboBox in WPF, there is a easier way to do it. Take a look at the code below.

<ComboBox Name="comboBox1" Text="--Select Item--"IsEditable="true" IsReadOnly="true"/> This works. You get a nice "--Select Item--" on the combo box but the problem is, the ComboBox is editable. I don't want this behavior. I want the default text to appear in an uneditable combo box. Like this,


StackOverFlow is our friend. There is a much better answer using a Combo Box Behavior. But in this code, there is a small problem.

publicstaticreadonlyDependencyPropertyDefaultTextProperty=DependencyProperty.RegisterAttached("DefaultText",typeof(String),

Non repeating random numbers in Java (Android)

Recently on a app I was working on, I had to select an element from either of two lists randomly. I tried using the normal new Random().nextInt( arrayLength - 1 ); but id didn't work on short lists.

Say a given length of 4, I'm more interested in getting an output like 4, 1, 3, 2, 1, 4, 2, 3, 4 but instead what I got was 1, 1, 1, 1, 2, 3, 2, 2, 4, 3, 2, 1, 1.

So as usual, SO to the rescue. How can I generate a random number within a range but exclude some?

This code, can do magic.

publicint getRandomWithExclusion(Random rnd,int start,int end,int... exclude){int random = start + rnd.nextInt(end - start +1- exclude.length);for(int ex : exclude){if(random < ex)