有没有一种方法可以设计表格5次而无需添加5种不同的表格

时间:2019-02-25 16:12:34

标签: vb.net winforms devexpress

我有一个新的非常大的项目我的问题  我在程序中有5个不同的用户(admin-user -account-admin_account-reception),如果我使用visible = false将该工具隐藏起来,则该程序的设计将变得非常难看。有没有一种方法可以在不更改代码的情况下为同一表单保存多个设计? 顺便说一句 我只是在寻找主意 因此,我永远不能重复相同的表格5次,因为它已经很大了,我不想将其放大。

1 个答案:

答案 0 :(得分:0)

I suggest keeping the information on users and controls to hide in a Module. This will be easier to configure and to handle.

You also need a means of iterating not only through top-level controls but also controls nested on a container control, like a panel or tab-control. For this, you can create a reusable recursive extension method:

Imports System.Runtime.CompilerServices

Module FormExtensions
    <Extension()>
    Public Iterator Function AllControls(ByVal parent As Control) As IEnumerable
        For Each c As Control In parent.Controls
            Yield c
            For Each nested As Control In c.AllControls()
                Yield nested
            Next
        Next
    End Function
End Module

In another module you can implement the logic

Module UserAccessControl
    ' Dictionary with users as key containing nested dictionaries with form names as key
    ' containing names of controls to be hidden in a HashSet.
    Private m_controlsToHide As New Dictionary(Of String, Dictionary(Of String, HashSet(Of String))) From
    {
        {"user", New Dictionary(Of String, HashSet(Of String)) From {
                    {"Form1", New HashSet(Of String) From {"TextBox1", "ComboBox2", "TextBox17"}},
                    {"Form2", New HashSet(Of String) From {"TextBox3", "Button1"}}
                }
        },
        {"account", New Dictionary(Of String, HashSet(Of String)) From {
                    {"Form1", New HashSet(Of String) From {"TextBox17"}},
                    {"Form2", New HashSet(Of String) From {"TextBox3", "Button1", "Button2"}}
                }
        }
    }

    Public Sub HideControls(ByVal user As String, ByVal form As Form)
        Dim userDict As Dictionary(Of String, HashSet(Of String))
        Dim controls As HashSet(Of String)

        If m_controlsToHide.TryGetValue(user, userDict) AndAlso
            userDict.TryGetValue(form.Name, controls) Then

            For Each control As Control In form.AllControls() _
                .Where(Function(c) controls.Contains(c.Name))

                control.Visible = False
            Next
        End If
    End Sub
End Module

If you prefer, you can also switch "user" and "form" and make the forms first level of the data structure.

Inside the form you can call

Public Sub New()
    InitizialeComponent()
    UserAccessControl.HideControls(userName, Me)
End Sub

By using a compound Tuple键的onclick,我们可以展平数据结构并摆脱一级字典:

Private m_controlsToHide As New Dictionary(Of (String, String), HashSet(Of String)) From {
    {("user", "Form1"), New HashSet(Of String) From {"TextBox1", "ComboBox2", "TextBox17"}},
    {("user", "Form2"), New HashSet(Of String) From {"TextBox3", "Button1"}},
    {("account", "Form1"), New HashSet(Of String) From {"TextBox17"}},
    {("account", "Form2"), New HashSet(Of String) From {"TextBox3", "Button1", "Button2"}},
}

Public Sub HideControls(ByVal user As String, ByVal form As Form)
    Dim controls As HashSet(Of String)

    If m_controlsToHide.TryGetValue((user, form.Name), controls) Then
        For Each control As Control In form.AllControls() _
            .Where(Function(c) controls.Contains(c.Name))

            control.Visible = False
        Next
    End If
End Sub

除了将访问信息存储在代码中之外,您还可以将其存储在数据库中。我经常在超级用户可以从应用程序内部更改这些设置的应用程序中使用。这比对信息进行硬编码更为灵活。