Amass

Forcing Sign display on Integers

January 31st, 2008 Luke

For a recent project, I needed to output integers to show the change in two values.

When an integer is negative, the negative sign(’-') is automatically added to it - but when you output an integer that is positive, there is no positive sign(’+') added to the beginning. I wanted to display the positive sign as well, so that users could easily tell when the values were positive vs. negative.

If you pass this format string into String.Format(), you will receive numbers with the proper signs applied:

"{0:+0;-0;0}"

Here are some examples:

String.Format("{0:+0;-0;0}",22); // returns "+22" String.Format("{0:+0;-0;0}",-22); // returns "-22" String.Format("{0:+0;-0;0}",0); // returns "0"

If you’re looking for a little more information on things you can do with String.Format, check out Kathy Kam’s .Net Format String 101 post.

SQL: Selecting default values using the CASE statement

January 25th, 2008 Luke

Have you ever encountered a situation where you’re pulling data out of tables that is supposed to be numeric, but for some reason occasionally has NULLs inside of it?

Handling those NULLs inside of your application is a pain - wouldn’t it be nicer if there was just a way to force those NULLs to become 0s?

As it turns out, there is. And it’s a pretty simple SQL query, making use of the CASE statement:

SELECT case WHEN column_value is null then 0 else column_value AS column_value FROM table WHERE condition

And that’s all there is to it. When there’s a null value retrieved you’ll get a 0, and when there isn’t you’ll get the value of the column.

Client App C#: How to retrieve the SelectedText value from a ComboBox

January 22nd, 2008 Luke

If you’ve done any work with ComboBoxen lately, you might have noticed that, even if you used my method for storing text/values into a combobox, you still can’t seem to use myComboBox.SelectedText to actually retrieve the SelectedText of the Combobox.

Why is this? I don’t know.

But I do know that if you change your call slightly, you can do it - you just refer to ComboBox.Text, instead of ComboBox.SelectedText:

string myText = ComboBox.Text;

It doesn’t quite make sense to me, but that’s how you work around the issue.

Client App C#: Storing value/text pairs into ComboBoxen

January 21st, 2008 Luke

Right now I’m working on a project that involves generating reports, based on a variety of user-selected criteria.

In order to make it easier for the user to choose what they want reports on(and also make it harder to screw up), we’ve decided to use the ComboBox component for all of our data - so that they can only select that values that are actually viable to report on.

The ComboBox is a little weird, however, because unlike the website-based DropDownList element, it doesn’t contain any ListItems. You can access ComboBox.SelectedValue, or ComboBox.SelectedText, but there didn’t seem to be any way to actually store values and text into a combo box.

As it turns out, it’s easier than you’d think. You can store your data into a datatable, and then give the ComboBox the names of the columns you want to use for values and for text:

DataTable dt = new DataTable(); dt.Load(myQuery.GetReader()); cbPictures.DisplayMember = "column_1"; cbPictures.ValueMember = "column_2"; cbPictures.DataSource = dt;

And with that finished, you will now have all the values of column_1 for your text, and all the values of column_2 for your data.

How to change the startup form in a client app

January 18th, 2008 Luke

Have you ever been plugging away at a client app, and suddenly found yourself in need of a way to change the startup form for the project?

I have. And the only answers I could find while Googling for the issue were “Go to project properties->application->’Startup Object’, and select your form from there”. Except that, whenever I tried to do that, the only thing that shows up under ‘Startup Object’ was my client app - myApp.program.

As it turns out, there is a way to change the startup form - you need to edit Program.cs(I would assume that under VB you need to change Program.vb). This is what the code looks like:

namespace MyProgram { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new myForm()); } } }

As it turns out, all you need to change is the line that says Application.Run(new myForm()); - just change the word after the new constructor to the name of the class/form you want to set as your startup form. Handy!