SQL – string inside a string

Handy little nugget to check if a string exists within another string. Particularly useful if you’re working with a delimited string as I was, as it makes dealing with the delimiters irrelevant.


SELECT * FROM table
WHERE CHARINDEX('string1', 'string2') != 0

In this instance ‘string1’ is the string you’re searching for in ‘string2’. Obviously these could be substituted for columns in a table or whatever.

Crystal Reports – Conditionally supress a Line object

Crystal Reports seem to be a bit like Marmite, people either love it or hate it! I’m actually quite indifferent, sure there are a number of bug-bears and ‘quirks’ to put up with, but at the end of the day it’s free and it’s what I’m stuck using, so I have little option but to get on with it and make do the best I can.

One particularly irksome feature is the lack of an option to conditionally suppress a Line or Box object, especially when you have a report with multiple total fields that are all underlined with a Line object as I do! It may be possible to draw a line object using a formula, I’m not sure, but an easier alternative is to display your total in a Textbox object, set only the bottom border and then suppress the Textbox using a formula as per usual.

Databind an Enum to a Combobox

Array enumValues = Enum.GetValues(typeof(your enum type here));
ArrayList enumList = new ArrayList();

foreach (Enum value in enumValues)
    enumList.Add(new KeyValuePair<Enum, string>(value, value.ToString()));

ComboBox.DataSource = enumList;

“DisplayMember” and “ValueMember” properties can then be set on the combobox to point to the Key and Value properties of the enumlist.

Compact Framework build and deployment time.

As anybody who has developed mobile applications with the compact framework (with VS2008 anyway) will know, the build and deployment time on even the smallest projects can be painfully long regardless of whether deploying to the device or the emulator.

Finding this has been a complete life saver (I was almost at the point of self harm waiting for builds!), and has slashed my build times by nearly a minute.

Make sure you take the time to read the full article so you know exactly what the implications are of disabling the ‘PlatformVerificationTask’ validation.

BindingContext – List controls that share a datasource

This one had me stumped for a while! If you have multiple list controls (combo’s etc) on the same form set to the same datasource, the default behaviour is for the controls to share the form’s BindingContext ,thus synchronising the current position. This means when you change list position in say ‘Combo A’, the selected index is also changed in ‘Combo B’.

To avoid this behaviour requires just one line of code applied to at least one of your controls.
Phew!


comboBox1.BindingContext = new BindingContext();

Custom Messagebox

9 times out of 10 the standard Windows Forms messagebox will do, but for those occasions it won’t, it’s pretty straight forward to design your own. I have an enum ‘MyMessageBoxButtons’ set up to accept ‘ok’ and ‘okCancel’, I have also set the OK button to fill the width of it’s parent panel if it is the only button to be displayed (this messagebox form is being displayed full screen in a mobile application), but you get the idea.


public partial class MyMessagebox : Form
{
    private static MyMessagebox messageBox;
    private static DialogResult messageboxResult;

    public MyMessagebox ()
    {
        InitializeComponent();
    }

    public static DialogResult Show(string messageText)
    {
        messageBox = new MyMessagebox ();
        messageBox.lbMessageboxMessage.Text = messageText;
        ShowButtons(messageBox, MyMessageBoxButtons.Ok);
        messageBox.ShowDialog();

        return messageboxResult;
    }

    public static DialogResult Show(string messageText, string messageCaption, MyMessageBoxButtons buttons)
    {
        messageBox = new myMessagebox();
        messageBox.lbMessageboxMessage.Text = messageText;
        messageBox.lbMessageboxCaption.Text = messageCaption;
        ShowButtons(messageBox, buttons);
        messageBox.ShowDialog();

        return messageboxResult;
    }
    
    private static void ShowButtons(MyMessageBox myMessageBox, MyMessageBoxButtons buttons)
    {
        myMessageBox.btnMessageboxOk.Enabled = true;
        myMessageBox.btnMessageboxOk.Visible = true;

        if (buttons != MyMessageBoxButtons.Ok)
        {
            myMessageBox.btnMessageboxCancel.Enabled = true;
            myMessageBox.btnMessageboxCancel.Visible = true;
        }
        else
        {
            myMessageBox.btnMessageboxOk.Width = myMessageBox.pnMessageboxDetail.Width;
        }
    }

    private void btnMessageboxOk_Click(object sender, EventArgs e)
    {
        messageboxResult = DialogResult.OK;
        messageBox.Close();
    }

    private void btnMessageboxCancel_Click(object sender, EventArgs e)
    {
        messageboxResult = DialogResult.Cancel;
        messageBox.Close();
    }        
}