Skip to content

Instantly share code, notes, and snippets.

@joebuschmann
joebuschmann / optional_pronoun.feature
Created October 17, 2014 13:35
Gherkin example with optional preceding pronoun
Scenario: Make "I" optional
Given the following devices
| Product Name |
| Galaxy IV |
| iPhone |
| Windows Phone |
| Note |
| Kindle |
| Blackberry Storm |
| iPad |
@joebuschmann
joebuschmann / optional_pronoun.cs
Created October 17, 2014 13:37
Step binding with optional preceding pronoun
[When(@"(?:I\s)?remove the (\d+)(?:st|nd|rd|th) item")]
public void WhenIRemoveTheItem(int index)
{
_products.RemoveAt(--index);
}
@joebuschmann
joebuschmann / generate_sql_delete_in_fk_order.cs
Created December 2, 2014 03:00
C# program to generate SQL delete statements in FK order
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Text;
namespace TruncateData
{
public class Program
{
@echo off
:: Save the current directory to the directory stack and
:: change the current directory to the one in which the current batch file is located.
pushd %~dp0
echo %CD%
:: Restore the previous current directory.
popd
echo %CD%
@joebuschmann
joebuschmann / winforms_dialogresult_none.cs
Created December 18, 2014 20:05
Prevent a dialog from closing by setting Form.DialogResult to None
private void btnOK_Click(object sender, EventArgs e)
{
if (Validate())
{
// There's no need to explicitly hide the form
// because the button's DialogResult property
// has a value of DialogResult.OK
// Details elided...
}
@joebuschmann
joebuschmann / winforms_using.cs
Last active October 22, 2015 18:31
Employ the using statement to dispose of a modal form
using (var form = new SelectUserForm())
{
// Set form properties...
if (form.ShowDialog(this) == DialogResult.OK)
{
// Process results...
}
}
@joebuschmann
joebuschmann / winforms_override_onload.cs
Created December 18, 2014 20:11
Override Form.OnLoad()
protected override void OnLoad(EventArgs e)
{
// Details elided
base.OnLoad(e);
}
@joebuschmann
joebuschmann / winforms_processdialogkey.cs
Last active August 29, 2015 14:11
Override Form.ProcessDialogKey to suppress automatic handling of accept button
protected override bool ProcessDialogKey(Keys keyData)
{
// Suppress the accept button when the filter editor has the focus.
// This doesn't work in the KeyDown or KeyPress events.
if((keyData == Keys.Return) && (filterEditor.ContainsFocus))
return true;
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDialogKey(Keys keyData)
{
// Suppress the accept button by preventing the parent form
// from processing the return key.
// Return false to indicate that the key press has not been
// handled and the child control should handle it.
if (keyData == Keys.Return)
return false;
return base.ProcessDialogKey(keyData);
@joebuschmann
joebuschmann / winforms_display_modal_good.cs
Created December 18, 2014 20:24
The correct way to display a WinForms modal dialog
private void btnShowUserDialog_Click(object sender, EventArgs e)
{
using (var form = new SelectUserForm())
{
form.SelectedUsers = SelectedUsers;
if (form.ShowDialog(this) == DialogResult.OK)
{
SelectedUsers = form.SelectedUsers;
lblUsers.Text = SelectedUsers.Values.ToDelimitedString(", ");