After lots new coding and refactoring, dotNetTips.Utility 3.5 R2 is finally released! This assembly is much of the common code I have been writing for the past 9+ years all wrapped up in a nice package and easy to use. Here is just some of what is in the new version:

New Classes

  • EmailTraceListener - Send emails when trace events happen (I suggest using filters here)!
  • WebServiceTraceListener - Send trace events to a web service. I use this for logging events to a central back-end database!
  • Lots of new Extension methods and classes:
    • Color
    • DateTime
    • Decimal
    • Entity Framework
    • Image
    • Nullable
    • Xlement
    • And more!
  • GeoInfoHelper - Get geo location info based on IP address.
  • And lots more!
The documentation, binary and source code can be downloaded from CodePlex.

Coming soon... .NET 4.0 version!


 

As most of you might know (I hope) it is unwise to call methods on a Control from a non-UI thread. This can cause all sorts of issues. Actually, I think in most cases .NET will throw and Exception. If you need to set a property on a Control this is how you would properly do it.

VB.NET

 Delegate Sub SetPropertyValueDelegate(ByVal obj As Object, ByVal val As Object, ByVal index As Object())

 

    Public Sub SetControlProperty(ByVal ctrl As Control, ByVal propName As String, ByVal val As Object)

        Dim propInfo As PropertyInfo = ctrl.[GetType]().GetProperty(propName)

        Dim propertyDelegate As [Delegate] = New SetPropertyValueDelegate(propInfo.SetValue)

        'index

        ctrl.Invoke(propertyDelegate, New Object(2) {ctrl, val, Nothing})

    End Sub

 

    Private Sub UpdateSent(ByVal item As String)

        _chat.AppendLine(item)

 

        If Me.SentTextBox.InvokeRequired Then

            ' This is a worker thread so delegate the task.

            SetControlProperty(SentTextBox, "Text", _chat.ToString())

        Else

            ' This is the UI thread so perform the task.

            Me.SentTextBox.Text = _chat.ToString()

        End If

 

 

    End Sub

C#

        delegate void SetPropertyValueDelegate(Object obj, Object val, Object[] index);

 

        public void SetControlProperty(Control ctrl, String propName, Object val)

        {

            PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);

            Delegate propertyDelegate = new SetPropertyValueDelegate(propInfo.SetValue);

            ctrl.Invoke(propertyDelegate, new Object[3] { ctrl, val, /*index*/null });

        }

 

        private void UpdateSent(string item)

        {

            _chat.AppendLine(item);

 

            if (this.SentTextBox.InvokeRequired)

            {   // This is a worker thread so delegate the task.

                SetControlProperty(SentTextBox, "Text", _chat.ToString());

            }

            else

            {   // This is the UI thread so perform the task.      

                this.SentTextBox.Text = _chat.ToString();

            }

 

        }

UpdateSent is example code on how to use the code. Also, _chat is a StringBuilder object. This code is taken from a chat program I wrote at where I work.

Tip Submitted By: David McCarter


 
Categories: Csharp | VB.NET | WinForms

September 8, 2008
@ 09:29 AM
Finally... Microsoft has released chart controls for .NET (only 8 years late). They include controls for Win and Web forms. To download go here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=130f7986-bf49-4fe5-9ca8-910ae6ea442c&DisplayLang=en


 
Categories: ASP.NET | Link | WinForms

If you are coming to the San Diego .NET Developers Group meeting tonight I hope you will be their early for my talk titled "What’s New In VS 2008 SP1". Lots of new additions to this SP, not just bug fixes. Below is a link to the presentation.

VS2008Sp1.pdf (715.88 KB)
 
Categories: .NET | ADO.NET | AJAX | ASP.NET | Csharp | dotNetDave | Entity Framework | LINQ | MVC | Silverlight | VB.NET | VS.NET | WCF | WinForms | WPF

September 20, 2007
@ 12:12 PM

If you need to convert RTF to text, here is a simple way of doing it:

    Public Shared Function ConvertRtfToText(ByVal input As String) As String
      Dim returnValue As String = String.Empty
      Using converter As New System.Windows.Forms.RichTextBox()
        converter.Rtf = input
        returnValue = converter.Text
      End Using
      Return returnValue
    End Function

Tip Submitted By: David McCarter

This code can be found in the open source dotNetTips.Utility assembly


 
Categories: VB.NET | WinForms

The code below shows you how to ignore all input unless it's a number:

private void searchText_KeyPress(object sender, KeyPressEventArgs e)
{
 if (e.KeyChar != 8)
 {
  if (char.IsNumber(e.KeyChar) == false)
  {
   e.Handled = true;
}
}

I allow the backspace keypress by checking for character 8. You can easily modify this code to suit your needs.

 

Tip Submitted By: David McCarter


 
Categories: WinForms

The Power Pack controls consist of:

  • BlendPanel. This provides a background for a form where the color fades from one shade to another.
  • UtilityToolbar. This is a toolbar whose look and feel is similar to the Internet Explorer toolbar.
  • ImageButton. This is a button that displays a graphic over a transparent background.
  • NotificationWindow. This displays text and graphics in a pop-up window (commonly known as "toast").
  • TaskPane. This is a container that provides collapsible frames for displaying additional information on a form.
  • FolderViewer. This displays directories in a hierarchical format.
  • FileViewer. This displays a list of the files in a specified directory.

For more information, go to: http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dv_vstechart/html/vbpowerpack.asp


 
Categories: dotNetDave | Link | VB.NET | WinForms

The User Interface Process Application Block provides a simple yet extensible framework for developing user interface processes. It is designed to abstract the control flow and state management out of the user interface layer into a user interface process layer. This helps you write generic code for the control flow and state management for different applications types (for example, Web and Windows) and helps manage user's tasks in complex scenarios (for example, suspending and resuming tasks).

 

http://www.microsoft.com/downloads/details.aspx?FamilyID=98c6cc9d-88e1-4490-8bd6-78092a0f084e&DisplayLang=en


 
Categories: WinForms

November 6, 2003
@ 01:46 AM

It’s not as hard as you might think in .NET. In the code below, just send the text and the Font object that, for example, the TextBox is using.

public static int GetTextWidth(string dataText, Font dataFont)
{
  return Graphics.FromImage(new Bitmap(1,1)).MeasureString(dataText, dataFont).ToSize().Width;
}

Tip Submitted By: David McCarter


 
Categories: Csharp | WinForms

When I was putting data from a DataTable as shown in the code below:

DataView locationsView = new DataView(MyDataSet.Tables["locations"]);
locationsView.Sort = "name ASC";
lstLocations.ValueMember = "key";
lstLocations.DisplayMember = "name";
lstLocations.DataSource = locationsView;

I got a very strange result as seen below:

Once I set the Sorted property to False, it worked fine. Go figure.

Bug Submitted By: David McCarter


 
Categories: Bugs | Csharp | WinForms

Because when you databind the data to the control, all previous data gets wiped out, so you have to add the new item afterwards. Also, it will appear at the end of the list. I don't know of a way around that.

This code adds and selects an empty list item:

nameList.DataSource = MyDataTable
nameList.DataTextField = "Name"
nameList.DataValueField = "ID"
nameList.DataBind()
nameList.Items.Add("")
nameList.Items.FindByText("").Selected = True

Tip Submitted By: David McCarter


 
Categories: WinForms