Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

An absolute cracker of a nerdy post by James Iry, author of One Div Zero.

See it here: A Brief, Incomplete, and Mostly Wrong History of Programming Languages

Couldn't agree more with 1996-2001 =P

C#: ShowDialog() keeps returning DialogResult.Cancel

When you call a dialog with dlg.ShowDialog() and check the result, you may find it always returning DialogResult.Cancel.

private void AddItem()
{
FormAdd dlgAdd = new FormAdd();
DialogResult res = dlgAdd.ShowDialog(this);

if (res != DialogResult.OK)
{
return;
}

// All good to go here
}

So with your dialog, ensure that the correct "AcceptButton" property has been set to the correct button.

A simple mistake to make is to forget setting the result when you click OK/Save/etc.

private void btnAdd_Click(object sender, EventArgs e)
{
// Do some validation here

this.DialogResult = DialogResult.OK;
}

That should set the correct value by the time the dialog is closed.

C# - Use different fore/back color for each subitem

Frustrating sometimes when you cant even set a simple color.

The C# listview is an awesome control for displaying data, but when you find yourself checking your code over and over to see if its setting the colors correctly, its quite annoying when you find out its a default setting thats fucking up your groove.

You see, .NET has a little flag on the ListViewItem object called 'UseItemStyleForSubItems' that uses the ListViewItem style for all subitems in that row. Most likely enabled by default for performance reasons, but also the cause of alot of wasted time.

ListViewItem lvi = new ListViewItem();
lvi.UseItemStyleForSubItems = false;

C# threading > update progress bar

C#, placed so highly on a pedestal by many.
so much shit hits fan when you try to update a label, progressbar or windows control from a thread.

Normally people would go through the trouble of declaring delegates, calling invoke with empty object arrays, shit like that which takes up roughly 6-7 lines of code.

//Declare a new delegate specific to updating the text of a label
delegate void UpdateLabelTextDelegate(Label lbl, string text);

//This method will invoke the delegate and pass our args along
void UpdateLabelText(Label lbl, string text)
{
UpdateLabelTextDelegate dlg = new UpdateLabelTextDelegate(UpdateLabelTextByDelegate);
//Invoke this method on the control's original that owns the label's handle
lbl.Invoke(dlg, new object[] { lbl, text });
}

//This method is invoked by our delegate and actually updates the label text
void UpdateLabelTextByDelegate(Label lbl, string text)
{
lbl.Text = text;
}

//Call the method now
UpdateLabelText(myLabel, "What a great post");
What a waste of time just to change a frikken label!
To make matters worse, it turns your code to garbage and makes it utterly unreadable.

Luckily, Microsoft decided to stop pounding our balls against a wall and provide us with MethodInvoker.

pbarLoading.Invoke((MethodInvoker)delegate
{
pbarLoading.Value++;
});
Short sweet and simple!

[ ref ]
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog