473,396 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adding controls to the form... help

I am trying dynamically create controls and add them to the form on the
Separate thread.
I know that I have to use delegate in order to update preexisting
controls on the form, but when I ma trying create new control I am
getting this message:
System.ArgumentException: Controls created on one thread cannot be
parented to a control on a different thread.
this is a sample code to reproduce error:

Delegate Sub MyDelegate()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Try
dd.BeginInvoke(Nothing, Nothing)
Catch ex As Exception
End Try
System.Threading.Thread.Sleep(500)
Next
End Sub
Sub addctrl()
Try
Dim x As New TextBox
Me.Controls.Add(x)
Debug.WriteLine(Me.Controls.Count)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

is there any way around it?

basicaly I have dataset which is updated and repopulated on the separate
thread and the it raises event and form gets recreated. IS there a way to
marshal event to proper thread??
any Ideas

Nov 20 '05 #1
3 4153
Hi Arthur,

You need to use the form's Invoke method to switch you call back to the
form's thread. The code basically stays the same. NOTE: I'm writing this
code in Outlook so no sytax check has been done (hopefull that feature will
be in Office 2004!)

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Me.Invoke(dd, Nothing)
Next
End Sub

For more information check out Chris Sell's series on Safe, Simple
Multithreading in Windows Forms.

http://msdn.microsoft.com/library/de...ms06112002.asp

--
Rob Windsor
G6 Consulting
Toronto, Canada


"Arthur Dzhelali" <a.********@theday.com> wrote in message
news:Xn********************************@216.168.3. 44...
I am trying dynamically create controls and add them to the form on the
Separate thread.
I know that I have to use delegate in order to update preexisting
controls on the form, but when I ma trying create new control I am
getting this message:
System.ArgumentException: Controls created on one thread cannot be
parented to a control on a different thread.
this is a sample code to reproduce error:

Delegate Sub MyDelegate()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Try
dd.BeginInvoke(Nothing, Nothing)
Catch ex As Exception
End Try
System.Threading.Thread.Sleep(500)
Next
End Sub
Sub addctrl()
Try
Dim x As New TextBox
Me.Controls.Add(x)
Debug.WriteLine(Me.Controls.Count)
Catch ex As Exception
Debug.WriteLine(ex.ToString)
End Try

End Sub

is there any way around it?

basicaly I have dataset which is updated and repopulated on the separate
thread and the it raises event and form gets recreated. IS there a way to
marshal event to proper thread??
any Ideas

Nov 20 '05 #2
"Arthur Dzhelali" <a.********@theday.com> schrieb
I am trying dynamically create controls and add them to the form on
the Separate thread.
I know that I have to use delegate in order to update preexisting
controls on the form, but when I ma trying create new control I am
getting this message:
System.ArgumentException: Controls created on one thread cannot be
parented to a control on a different thread.
this is a sample code to reproduce error:

[code]

is there any way around it?

basicaly I have dataset which is updated and repopulated on the
separate thread and the it raises event and form gets recreated. IS
there a way to marshal event to proper thread??
any Ideas

Where are the other threads? You must call the Invoke or BeginInvoke methods
of the _Control_ to call the procedure in the thread that created the
control. It does not help to call the BeginInvoke method of the _Delegate_.
In the other thread(s), and if the code is within the same Form:
Me.Invoke(New MyDelegate(AddressOf addctrl))

Note: In your example this does not make sense because there is only one
thread.
--
Armin

Nov 20 '05 #3
Thanks, I tryed this before it did not work.
I but I gave it one more try.
It works now.
It is hard to debug when you have several thread going on.

BTW, invoke doesnot produce async result, you have to use begin envoke in
order to create separate thread.

"Rob Windsor" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in
news:en*************@TK2MSFTNGP12.phx.gbl:
Hi Arthur,

You need to use the form's Invoke method to switch you call back to
the form's thread. The code basically stays the same. NOTE: I'm
writing this code in Outlook so no sytax check has been done (hopefull
that feature will be in Office 2004!)

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dd As New MyDelegate(AddressOf addctrl)
For i As Int32 = 0 To 10
Me.Invoke(dd, Nothing)
Next
End Sub

For more information check out Chris Sell's series on Safe, Simple
Multithreading in Windows Forms.

http://msdn.microsoft.com/library/de...y/en-us/dnform
s/html/winforms06112002.asp


Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terry | last post by:
I have a very basic program, but for some reason I can't get it to behave properly. What I want is a basic form with a TabControl that fills the entire form. The tab control should have 4 tabs...
2
by: avivgur | last post by:
Hello, I am writing a program in Visual C# and I have encountered a problem. In my program I want to dynamically create a multitude of controls (thousands) on a form. The problem is that calling...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a...
6
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
3
by: John Hughes | last post by:
I'm trying to add a user control to a form via the pages render method and I get the following error : "Control 'Button1' of type 'Button' must be placed inside a form tag with runat=server" ...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
6
by: Juan Pedro Gonzalez | last post by:
I wanted to add a Combobox to a toolbar... Kind of the look you get on VisualStudio's toolbar. I've been able to find some VB 6 samples, but the placeholder option is no longer available for...
3
by: Toe Dipper | last post by:
In short we have a lengthy process when a form is loaded that adds activex controls to our windows form. This process in itself works fine however we would like to push this processing to a thread...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.