我提问的:找一个通用函数,修改同一类别的控件的一些属性,或者优化、重构下面的代码。

    技术2022-05-12  13

    找一个通用函数,修改同一类别的控件的一些属性,或者优化、重构下面的代码。 

    比如有三个lable控件。在程序中要修改他们的字体,大小,颜色,位置,内容。 如何写一个通用的函数来修改他们呢。如:SetContrlPropertyValue(....)

    我的代码是分开各个来写,感觉很麻烦,多余。有什么办法优化,重构一下下面的代码。

    PS:

    可是我是跨线程的哦,如何传递这些实例给他们呢。 比如说,我在主线A程启动一个线程BB打开一个界面。 想在主线A程中更改线程BB中的一些Label的一些属性。 这个方法想运行用到splash screen 中。

    '修改LabelTitle的属性 Public Sub SetTitleFont(ByVal color As Color, ByVal size As Integer) SetTitleFont(color, size, Me.Font) End Sub Public Sub SetTitleFont(ByVal color As Color, ByVal size As Integer, ByVal font As Font) If color <> color.Empty Then Me.TitleLabel.ForeColor = color End If Me.TitleLabel.Font = New Font(font.Name, size) End Sub Public Sub SetTitleLabel(ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) SetTitleLabel(Point.Empty, LabelWidth, LabelHeight) End Sub Public Sub SetTitleLabel(ByVal Location As Point, ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) If Location <> Point.Empty Then Me.TitleLabel.Location = Location End If If LabelWidth = 0 AndAlso LabelHeight = 0 Then Me.TitleLabel.AutoSize = True Else If LabelWidth > 0 Then Me.TitleLabel.Width = LabelWidth End If If LabelHeight > 0 Then Me.TitleLabel.Height = LabelHeight End If End If End Sub '修改LabelStatus的属性 Public Sub SetStatusFont(ByVal color As Color, ByVal size As Integer) SetStatusFont(color, size, Me.Font) End Sub Public Sub SetStatusFont(ByVal color As Color, ByVal size As Integer, ByVal font As Font) If color <> color.Empty Then Me.StatusLabel.ForeColor = color End If Me.StatusLabel.Font = New Font(font.Name, size) End Sub Public Sub SetStatusLabel(ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) SetStatusLabel(Point.Empty, LabelWidth, LabelHeight) End Sub Public Sub SetStatusLabel(ByVal Location As Point, ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) If Location <> Point.Empty Then StatusLabel.Location = Location End If If LabelWidth = 0 AndAlso LabelHeight = 0 Then StatusLabel.AutoSize = True Else If LabelWidth > 0 Then StatusLabel.Width = LabelWidth End If If LabelHeight > 0 Then StatusLabel.Height = LabelHeight End If End If End Sub '修改LabelInfo的属性 Public Sub SetInfoFont(ByVal color As Color, ByVal size As Integer) SetInfoFont(color, size, Me.Font) End Sub Public Sub SetInfoFont(ByVal color As Color, ByVal size As Integer, ByVal font As Font) If color <> color.Empty Then Me.InfoLabel.ForeColor = color End If Me.InfoLabel.Font = New Font(font.Name, size) End Sub Public Sub SetInfoLabel(ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) SetInfoLabel(Point.Empty, LabelWidth, LabelHeight) End Sub Public Sub SetInfoLabel(ByVal Location As Point, ByVal LabelWidth As Integer, ByVal LabelHeight As Integer) If Location <> Point.Empty Then InfoLabel.Location = Location End If If LabelWidth = 0 AndAlso LabelHeight = 0 Then InfoLabel.AutoSize = True Else If LabelWidth > 0 Then InfoLabel.Width = LabelWidth End If If LabelHeight > 0 Then InfoLabel.Height = LabelHeight End If End If End Sub

     ========================C#==================================

    public string SetTitle { get { return Title; } set { Title = value; if (TitleLabel.InvokeRequired) { var TitleUpdate = new UpdateLabel(UpdateTitle); Invoke(TitleUpdate); } else { UpdateTitle(); } } } public string SetInfo { get { return Information; } set { Information = value; if (InfoLabel.InvokeRequired) { var InfoUpdate = new UpdateLabel(UpdateInfo); Invoke(InfoUpdate); } else { UpdateInfo(); } } } public string SetStatus { get { return Status; } set { Status = value; if (StatusLabel.InvokeRequired) { var StatusUpdate = new UpdateLabel(UpdateStatus); Invoke(StatusUpdate); } else { UpdateStatus(); } } } //修改LabelTitle的属性 public void SetTitleFont(Color color, int size) { SetTitleFont(color, size, this.Font); } public void SetTitleFont(Color color, int size, Font font) { if (color != color.Empty) { this.TitleLabel.ForeColor = color; } this.TitleLabel.Font = new Font(font.Name, size); } public void SetTitleLabel(int LabelWidth, int LabelHeight) { SetTitleLabel(Point.Empty, LabelWidth, LabelHeight); } public void SetTitleLabel(Point Location, int LabelWidth, int LabelHeight) { if (Location != Point.Empty) { this.TitleLabel.Location = Location; } if (LabelWidth == 0 && LabelHeight == 0) { this.TitleLabel.AutoSize = true; } else { if (LabelWidth > 0) { this.TitleLabel.Width = LabelWidth; } if (LabelHeight > 0) { this.TitleLabel.Height = LabelHeight; } } } //修改LabelStatus的属性 public void SetStatusFont(Color color, int size) { SetStatusFont(color, size, this.Font); } public void SetStatusFont(Color color, int size, Font font) { if (color != color.Empty) { this.StatusLabel.ForeColor = color; } this.StatusLabel.Font = new Font(font.Name, size); } public void SetStatusLabel(int LabelWidth, int LabelHeight) { SetStatusLabel(Point.Empty, LabelWidth, LabelHeight); } public void SetStatusLabel(Point Location, int LabelWidth, int LabelHeight) { if (Location != Point.Empty) { StatusLabel.Location = Location; } if (LabelWidth == 0 && LabelHeight == 0) { StatusLabel.AutoSize = true; } else { if (LabelWidth > 0) { StatusLabel.Width = LabelWidth; } if (LabelHeight > 0) { StatusLabel.Height = LabelHeight; } } } //修改LabelInfo的属性 public void SetInfoFont(Color color, int size) { SetInfoFont(color, size, this.Font); } public void SetInfoFont(Color color, int size, Font font) { if (color != color.Empty) { this.InfoLabel.ForeColor = color; } this.InfoLabel.Font = new Font(font.Name, size); } public void SetInfoLabel(int LabelWidth, int LabelHeight) { SetInfoLabel(Point.Empty, LabelWidth, LabelHeight); } public void SetInfoLabel(Point Location, int LabelWidth, int LabelHeight) { if (Location != Point.Empty) { InfoLabel.Location = Location; } if (LabelWidth == 0 && LabelHeight == 0) { InfoLabel.AutoSize = true; } else { if (LabelWidth > 0) { InfoLabel.Width = LabelWidth; } if (LabelHeight > 0) { InfoLabel.Height = LabelHeight; } } }


    最新回复(0)