You're right, it's not being used correctly.
The Transaction needs to take place inside the Idling event.
The button click handler and Idling event handler should look something like this in Revit 2014:
void revitApp_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
{
UIApplication uiapp = sender as UIApplication;
UIDocument uidoc = uiapp.ActiveUIDocument;
ElementSet elems = uidoc.Selection.Elements;
if (elems != null)
{
label1.Text = elems.Size.ToString() + " items selected.";
}
else
{
label1.Text = "No elements selected.";
}
if (shouldRun)
{
using (Transaction trans = new Transaction(uidoc.Document, "Hide elements"))
{
trans.Start();
uidoc.Document.ActiveView.HideElements((from Element el in elems select el.Id).ToList());
uidoc.RefreshActiveView();
trans.Commit();
}
shouldRun = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
shouldRun = true;
}