Click to See Complete Forum and Search --> : Updating the new controls
Saeed
July 20th, 2005, 08:14 PM
Hi there is a form .Form has some controls on it.one of the controls is clicked
when that takes place then i m moving the position of another control.
but i want to change te postion gradually on the form and view it
meaning
for(i=0 ; i<end ;i = i+step)
{
//1- change ths position of a control
//2- Update teh form
//3- delay
}
2 i guess its gonna be something like Form1.Update(false) if that MfC.
How do you do this in Vc# ?
Thanks in Adv.
exterminator
July 21st, 2005, 12:09 AM
Yes the algo you figured is a way to achieve what you want. There is a Refresh() method (instead of Update) for the forms, I hope this is what you were asking about.
Saeed
July 24th, 2005, 04:25 PM
Ok that makes sense.
Now how about if i create a thread in Program.cs and in that thread keep
inncrementing the positions and resfreshing it .. that shoudl do it.
the problem is how do you refrence the control in the thread routine?
i.e:
public class Alpha
{
public void Beta()
{
while (true)
{
// Woudl i be able to reference the control and form in here ?
//???
}
}
}
namespace TestingControls
{
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
// create the thread
// staring the thread ....
}
}
}
exterminator
July 25th, 2005, 01:26 AM
If the new thread method that is invoked in the new thread is in the same class scope as the thread starter method .. no problems .. you can access the controls easily but there is a safe and unsafe way of doing this. A quote from MSDNAccess to Windows Forms controls is not inherently thread-safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.Look at this MSDN article that explains how you would do this: How to: Make Cross-Thread Calls to Windows Forms Controls (http://msdn2.microsoft.com/library/ms171728(en-us,vs.80).aspx). Now, what I see from your query is that you are invoking some method in the new thread which is NOT a member of the same class where the controls were CREATED. Forget about threads - think of a way of accessing the members of one object (of first class) into a 2nd class. Now, when you find a way try to get this done in a separate thread. I am very immature at this, but I would go something like:
1. Call a method of the same class (say class 1) where the controls are created.
2. Create an object of class 2 from this method of class 1
3. Now call the various operations defined in class 2 (using 'this' or a specific member as an argument)to work upon the members of class 1 (i.e. the controls)
I am not sure about this algorithm, if it will work (if works, then not sure if this is the best). I just wrote out of the common sense that I possess. Try to do some search or may be some of the gurus would help you with this. :)
deesan
July 25th, 2005, 01:41 AM
I think it would be much easier to activate timer and move control in OnTimer event handler.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.