当某些控件按程序删除时刷新面板

时间:2017-11-25 16:36:01

标签: vb.net

我正在使用此部分代码添加标签 TextBox a 按钮 RadioButton 进入面板

Private Sub AppsAdd_Button_Click(sender As Object, e As EventArgs) Handles AppsAdd_Button.Click
    Dim lbl As New Label()
    Dim count As Integer = Apps_Panel.Controls.OfType(Of Label)().ToList().Count
    lbl.Name = "AppLabel_" & (count + 1)
    lbl.Text = "Application #" & (count + 1) & ":"
    lbl.Location = New Point(0, 28 * count)
    lbl.AutoSize = False
    lbl.Size = New Size(114, 21)
    lbl.TextAlign = ContentAlignment.MiddleRight
    lbl.Tag = count + 1
    Apps_Panel.Controls.Add(lbl)

    Dim txtbox As New TextBox()
    count = Apps_Panel.Controls.OfType(Of TextBox)().ToList().Count
    txtbox.Name = "AppTextbox_" & (count + 1)
    txtbox.Text = "..."
    txtbox.Location = New Point(120, 28 * count)
    txtbox.Size = New Size(387, 21)
    txtbox.Tag = count + 1
    AddHandler txtbox.TextChanged, AddressOf TextBox_Changed
    Apps_Panel.Controls.Add(txtbox)

    Dim btn As New Button()
    count = Apps_Panel.Controls.OfType(Of Button)().ToList().Count
    btn.Name = "AppBrowseButton_" & (count + 1)
    btn.Text = "Browse"
    btn.Location = New Point(513, 28 * count)
    btn.Size = New Size(75, 21)
    btn.Tag = count + 1
    AddHandler btn.Click, AddressOf Button_Click
    Apps_Panel.Controls.Add(btn)

    Dim radiobtn As New RadioButton()
    count = Apps_Panel.Controls.OfType(Of RadioButton)().ToList().Count
    radiobtn.Name = "AppRadio_" & (count + 1)
    radiobtn.Text = ""
    radiobtn.Location = New Point(590, 28 * count)
    radiobtn.Size = New Size(14, 21)
    radiobtn.Tag = count + 1
    AddHandler radiobtn.Click, AddressOf RadioButton_Click
    Apps_Panel.Controls.Add(radiobtn)
End Sub
Private Sub TextBox_Changed(sender As Object, e As EventArgs)
    Dim textbox As TextBox = TryCast(sender, TextBox)
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
    Dim button As Button = TryCast(sender, Button)
End Sub
Private Sub RadioButton_Click(sender As Object, e As EventArgs)
    Dim RadioButton As RadioButton = TryCast(sender, RadioButton)
    AppToDel_Label.Text = RadioButton.Tag
End Sub

然后我用它来删除选定的控件:

Private Sub AppsDel_Button_Click(sender As System.Object, e As System.EventArgs) Handles AppsDel_Button.Click
    Dim Ctrl As Control
    For controlIndex As Integer = Apps_Panel.Controls.Count - 1 To 0 Step -1
        Ctrl = Apps_Panel.Controls(controlIndex)
        If Ctrl.Name Like "*" & AppToDel_Label.Text Then
            RemoveHandler Ctrl.TextChanged, AddressOf TextBox_Changed
            RemoveHandler Ctrl.Click, AddressOf Button_Click
            RemoveHandler Ctrl.Click, AddressOf RadioButton_Click
            Apps_Panel.Controls.Remove(Ctrl)
            Ctrl.Dispose()
        End If
    Next
End Sub

当选定的控件位于面板底部时,当我删除面板时,面板的高度会变小。但是如果我选择从面板的顶部或中间删除" list",则留下一个空的空间,面板不会缩小!任何的想法?无论如何都要刷新面板以便填充空白区域?

Here是一个视频示例。

1 个答案:

答案 0 :(得分:1)

请改用FlowLayoutPanel。控件自动添加[HttpPost] public ViewResult Index(MailModelViewModel _objModelMail) { try { TempData["message"] = "Email Sent Successfully"; return RedirectToAction("Index"); } catch (Exception ex) { // to do :Log your exception ModelState.AddModelError(string.empty,"Problem while sending email"); return View("../Error"); } } 布局。它可以水平或垂直排列项目。 添加它们时不指定位置,而是使用边距和填充。

我还将一行的所有控件(Label,TextBox,Button和RadioButton)组合成UserControl。这允许您以可视方式而不是以编程方式设计行。

Walkthrough: Arranging Controls on Windows Forms Using a FlowLayoutPanel

Working with Windows Forms FlowLayoutPanel(C#Corner)。