tags:

views:

63

answers:

3

First: sorry for my poor English writing.

I remember that in VB6.0 days, we had some modules just for popular tasks like making disabled/enabled all buttons of a toolbar buttons or like calculating records count in a specific recordset.

Now, in .Net days, What is your approach for doing such these popular tasks? Do you create a static class?

Thank you

+1  A: 

http://msdn.microsoft.com/en-us/library/bb384936.aspx

Extension Method replaced static function for me. (no more Utility class)

EDIT

this is an example for setting backcolor for all controls. You declare them in modules.

<System.Runtime.CompilerServices.Extension()> _
Public Sub ChangeToRed(ByVal f As form)
    For Each c in f.Controls
        c.BackColor = Color.Red
    End For
End Sub

After that go to any form and you should see ChangeToRed function.

M.Shuwaiee
Thank you, but what about if we should do these tasks in every winform within our project with more thank 100 winform? where do you declare you extension methods?
odiseh
I updated my post with an example.
M.Shuwaiee
+2  A: 

Assuming Windows Forms here. There isn't anything in the class library that makes this especially easy. The Application.Idle event is however useful. It runs right after any mouse or keyboard input event, after all Windows notifications are processed. A good place to calculate button state that would otherwise be awkward to update directly from event handlers.

Here's an example that updates the standard Copy, Cut, Paste and Undo toolbar buttons:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        Application.Idle += new EventHandler(UpdateViewState);
    }
    private void UpdateViewState(object sender, EventArgs e) {
        bool canUndo = false;
        bool canCopy = false;
        bool canPaste = false;
        if (this.ActiveControl is TextBoxBase) {
            var box = this.ActiveControl as TextBoxBase;
            canUndo = box.CanUndo;
            canCopy = box.Text.Length > 0;
            canPaste = Clipboard.ContainsText();
        }
        undoButton.Enabled = canUndo;
        cutButton.Enabled = copyButton.Enabled = canCopy;
        pasteButton.Enabled = canPaste;
    }
}

Doing the same thing with event handlers for Enter, Leave, TextChanged for every single text box in your form would be quite painful.

Hans Passant
+1, I like the idea of doing this in the Application.Idle event, I'd not thought of that.
Matt Warren
@Hans: 1 -Thank you but I think I need some more explanation about what you've said....Could you please explain it more? 2 -If we have a project with (for example) 100 winforms, what is the best approach (where ?)for doing such popular tasks like enable/ disable ing toolbar buttons of these forms? Putting what you've written in EVERY form?
odiseh
I didn't say much, tried to make the code talk. Maybe you can ask a direct question? You only have to put this code in *one* form, you can inherit all the other forms from it.
Hans Passant
A: 

Yes I still use Static(C#) or Shared(VB) methods in my WPF applications to do some of these tasks. I use a XAML Method SetValue(dependancyproperty,Object) in side of generic methods to switch controls enabled/visible properties on/off on mass. This allows you to walk up and down the XAML visual tree to do things. I've found WPF methods to do these tasks contain less code that the VB6 way of doing things and much simpler codebase than WindowsForms.

VB.net WPF 3.5/4.0 Example - Control Visibility:

''' <summary>
''' Use the Tag property to indicate if the controls visibility should be set or not.
''' </summary>
Shared Function ControlVisibilityByTag(ByVal element As Visual, ByVal tagContents As String, ByVal controlVisibility As Visibility) As Boolean
    Dim ControlList As List(Of Visual)
    ControlList = GetControlsByTag(element, tagContents)
    For myLoop As Integer = 0 To ControlList.Count - 1
        '   LL: SetValue is a cool alternative to strongly typing controls and setting a controls visibility. Thanks to Linda Lui at Microsoft
        ControlList.Item(myLoop).SetValue(Control.VisibilityProperty, controlVisibility)            
    Next
    Return ControlList.Count > 0
End Function

Public Shared Function GetControlsByTag(ByVal element As Visual, ByVal tagContents As String) As List(Of Visual)
    If element Is Nothing Then
        Throw New ArgumentNullException([String].Format("Element {0} is null !", element.ToString()))
    End If
    If _NamedControllist Is Nothing Then
        _NamedControllist = New List(Of Visual)
    Else
        _NamedControllist.Clear()
    End If

    GetNestedControlsListByTag(element, 0, tagContents)

    Return _NamedControllist
End Function

Private Shared Sub GetNestedControlsListByTag(ByVal control As Visual, ByVal level As Integer, ByVal tagContents As String)
    Dim ChildNumber As Integer = VisualTreeHelper.GetChildrenCount(control)

    For i As Integer = 0 To ChildNumber - 1
        Dim v As Visual = DirectCast(VisualTreeHelper.GetChild(control, i), Visual)
        '   LL: GetValue is a cool alternative to strongly typing controls to read a standard control property value. Thanks to Linda Lui at Microsoft
        If Not IsNothing(v.GetValue(FrameworkContentElement.TagProperty)) AndAlso v.GetValue(FrameworkContentElement.TagProperty).ToString() = tagContents Then
            _NamedControllist.Add(v)
        End If            

        If VisualTreeHelper.GetChildrenCount(v) > 0 Then
            GetNestedControlsListByTag(v, level + 1, tagContents)
        End If
    Next
End Sub

Frameworks like CSLA also provide some inbuild control/menu functionality to enable and disable menus/buttons based on the state of the Classes that are bound via XAML databinding. This is particularly usefull for CRUD operations and record navigation. However, even using a framework, I've found that I also need some generic find controls and change control property methods in my WPF application and I'm happy to continue useing shared/static methods.

Jamie Clayton