如何在Vb 2008中从后台工作者添加新按钮 [英] How Can I Add A New Button From A Backgroundworker In Vb 2008

查看:73
本文介绍了如何在Vb 2008中从后台工作者添加新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中检索一些按钮...大约40-50但是由于数据库它会冻结gui 2-3秒。

可以添加按钮到来自背景工作者的flowlayoutpanel?

i尝试下面的代码但不起作用...不允许跨线程操作



 私有  Sub  adaugabutoaneworker_DoWork( ByVal  sender  As  System。 Object  ByVal  e  As  System.ComponentModel.DoWorkEventArgs)句柄 aratacategoriiworker.DoWork 
Dim butoanee 作为 imagini.butoane

Dim categoriiprodus e() As Data.DataRow = dss.Tables( categoriiproduse)。选择()
对于 i = 0 categoriiproduse.Length - 1
Dim 按钮作为 按钮()
Dim count = FlowLayoutPanel1.Controls.OfType( Of Button)()。ToList()。数
button.Location = System.Drawing.Point( 150 25 * count)
button.Size = System.Drawing.Size( 130 40
button.Name = categoriiproduse(i).Item( 0 ' numele butonului este id-ul categoriei
button.Text = categoriiproduse(i).Item( 2
button.BackgroundImageLayout = ImageLayout.Stretch
butoanee.albastru(button)
AddHandler 按钮.Click, AddressOf Button_Click
addbutton(button)
' FlowLayoutPanel1.Controls.Add(按钮)
Threading.Thread.Sleep( 1000
下一步

结束 Sub
委托 Sub addbutton_Delegate( ByVal [Button] As Button)


私有 Sub addbutton( ByVal [按钮] 作为按钮)
如果 [按钮] .InvokeRequired = True 然后
Dim MyDelegate 作为 addbutton_Delegate( AddressOf addbutton)
Me .Invoke(MyDelegate, New 对象(){[Button]})
否则
FlowLayoutPanel1.Controls.Add([Button])
结束 如果
结束 < span class =code-keyword> Sub

解决方案

nevermine,我发现了问题......我不得不将调用请求发送到flowlayoutpanel。

如果有人需要,可以提供解决方案。



 私有  Sub  adaugabutoaneworker_DoWork( ByVal  sender 作为系统。对象 ByVal  e  As  System.ComponentModel.DoWorkEventArgs)句柄 aratacategoriiworker.DoWork 
Dim butoanee 作为 imagini.butoane

Dim categoriiproduse() As Data.DataRow = dss.Tables( categoriiproduse)。选择()
对于 i = 0 categoriiproduse.Length - 1
Dim 按钮作为 按钮()
Dim count = FlowLayoutPanel1.Controls.OfType( of 按钮)()。ToList()。计数
button.Location = System.Drawing.Point( 150 25 * count)
button.Size = System.Drawing .Size( 130 , 40
button.Name = categoriiproduse(i).Item( 0 ' numele butonului este id-ul categoriei
button.Text = categoriiproduse(i).Item ( 2
button.BackgroundImageLayout = ImageLayout.Stretch
butoanee.albastru(button)
AddHandler 按钮。点击, AddressOf Button_Click
addbutton(button,FlowLayoutPanel1)
' FlowLayoutPanel1.Controls.Add(按钮)
下一步

结束 Sub
代表 Sub addbutton_Delegate( ByVal [Button] As Button, ByVal panel As FlowLayoutPanel)


私有 Sub addbutton(< span class =code-keyword> ByVal
[Button] 作为按钮, ByVal 面板 As FlowLayoutPanel)
如果 panel.InvokeRequired = True 然后
Dim MyDelegate 正如 addbutton_Delegate( AddressOf addbutton)
.Invoke(MyDelegate, 对象(){[按钮], panel})
Else

panel.Controls.Add([Button])
结束 如果
结束 Sub


Hi, i want to retrive from a database some buttons ... around 40-50 but because of the database it freezes the gui for 2-3 seconds.
Can i add the buttons to the flowlayoutpanel from a backgroundworker?
i tried the code below but doesn't work ... cross-thread operation not allowd

Private Sub adaugabutoaneworker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles aratacategoriiworker.DoWork
        Dim butoanee As New imagini.butoane

        Dim categoriiproduse() As Data.DataRow = dss.Tables("categoriiproduse").Select()
        For i = 0 To categoriiproduse.Length - 1
            Dim button As New Button()
            Dim count = FlowLayoutPanel1.Controls.OfType(Of Button)().ToList().Count
            button.Location = New System.Drawing.Point(150, 25 * count)
            button.Size = New System.Drawing.Size(130, 40)
            button.Name = categoriiproduse(i).Item(0) 'numele butonului este id-ul categoriei
            button.Text = categoriiproduse(i).Item(2)
            button.BackgroundImageLayout = ImageLayout.Stretch
            butoanee.albastru(button)
            AddHandler button.Click, AddressOf Button_Click
            addbutton(button)
            'FlowLayoutPanel1.Controls.Add(button)
            Threading.Thread.Sleep(1000)
        Next

    End Sub
    Delegate Sub addbutton_Delegate(ByVal [Button] As Button)


    Private Sub addbutton(ByVal [Button] As Button)
        If [Button].InvokeRequired = True Then
            Dim MyDelegate As New addbutton_Delegate(AddressOf addbutton)
            Me.Invoke(MyDelegate, New Object() {[Button]})
        Else
            FlowLayoutPanel1.Controls.Add([Button])
        End If
    End Sub

解决方案

nevermine, i found the problem ... i had to make the invokerequest to the flowlayoutpanel.
heres the solution if someone need.

Private Sub adaugabutoaneworker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles aratacategoriiworker.DoWork
    Dim butoanee As New imagini.butoane

    Dim categoriiproduse() As Data.DataRow = dss.Tables("categoriiproduse").Select()
    For i = 0 To categoriiproduse.Length - 1
        Dim button As New Button()
        Dim count = FlowLayoutPanel1.Controls.OfType(Of Button)().ToList().Count
        button.Location = New System.Drawing.Point(150, 25 * count)
        button.Size = New System.Drawing.Size(130, 40)
        button.Name = categoriiproduse(i).Item(0) 'numele butonului este id-ul categoriei
        button.Text = categoriiproduse(i).Item(2)
        button.BackgroundImageLayout = ImageLayout.Stretch
        butoanee.albastru(button)
        AddHandler button.Click, AddressOf Button_Click
        addbutton(button, FlowLayoutPanel1)
        'FlowLayoutPanel1.Controls.Add(button)
    Next

End Sub
Delegate Sub addbutton_Delegate(ByVal [Button] As Button, ByVal panel As FlowLayoutPanel)


Private Sub addbutton(ByVal [Button] As Button, ByVal panel As FlowLayoutPanel)
    If panel.InvokeRequired = True Then
        Dim MyDelegate As New addbutton_Delegate(AddressOf addbutton)
        Me.Invoke(MyDelegate, New Object() {[Button], panel})
    Else
        panel.Controls.Add([Button])
    End If
End Sub


这篇关于如何在Vb 2008中从后台工作者添加新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆