fetch value of text box outside thread

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 2
Reputation: nasirgul is an unknown quantity at this point 
Solved Threads: 0
nasirgul nasirgul is offline Offline
Newbie Poster

fetch value of text box outside thread

 
0
  #1
May 22nd, 2008
Hello,
I am new to C#. I want to know one thing. How can I get value from text box(or update text box value from another thread) while working on C# Threading. I tried but cant able to get. Below is the code. I want to use text box values in public void Run() function as for unlimited value and then further use in private void AddString(String s) for showing as GUI.
  1. private void AddString(String s)
  2. {
  3. n = hi.Text;
  4.  
  5. int val = Convert.ToInt32(m);
  6. int val2 = Convert.ToInt32(s);
  7. int val3 = Convert.ToInt32(n);
  8. int sum = 0;
  9. sum = sum + val3 + val2;
  10. listBox1.Items.Add(sum);
  11.  
  12. }
  13.  
  14. public void Run()
  15. { String s;
  16. int a = 2;
  17. s=a.ToString();
  18. while (true)
  19. {
  20. Thread.Sleep(400);
  21. m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
  22. // check if thread is cancelled
  23. if (m_EventStop.WaitOne(0, true))
  24. { // clean-up operations may be placed here
  25. // inform main thread that this thread stopped
  26. m_EventStopped.Set();
  27. return;
  28. }
  29. }
  30. }

Hope to hear soon.
Regards.
Nasir
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: fetch value of text box outside thread

 
0
  #2
May 22nd, 2008
  1. if (textbox1.InvokeRequired)
  2. {
  3. //txtFilePath.Invoke(ur delegate );
  4. }
  5. else
  6. {
  7. //call ur method
  8. }
Last edited by Ramy Mahrous; May 22nd, 2008 at 1:11 pm.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 2
Reputation: nasirgul is an unknown quantity at this point 
Solved Threads: 0
nasirgul nasirgul is offline Offline
Newbie Poster

Re: fetch value of text box outside thread

 
0
  #3
May 22nd, 2008
Hello,
Thanks for yours reply. I tried bt I am getting error at if (this.textBox1.InvokeRequired); while debugging....and break on this point.
////define public

public delegate void SetText(string text);

//////


private void AddString(String s)
{
// n = hi.Text;
int val = Convert.ToInt32(m);
int val2 = Convert.ToInt32(s);
int val3 = Convert.ToInt32(n);
int sum;
sum = val2;
listBox1.Items.Add(sum);

}

public void Run()
{
string s;// double v = 2;
int a = 2;
//hi.Text = v.ToString();

s = a.ToString();
while (true)
{
Thread.Sleep(400);
m_form.Invoke(m_form.m_DelegateAddString, new Object[] { s });
this.SetTextBoxValue("BOB");
string textBoxValue = this.hi.Text;


// check if thread is cancelled
if (m_EventStop.WaitOne(0, true))
{ // clean-up operations may be placed here
// inform main thread that this thread stopped
m_EventStopped.Set();
return;
}
}
}
public void SetTextBoxValue(string textValue)
{

if (this.hi.InvokeRequired)
{

SetText del = new SetText(this.SetTextBoxValue);

this.hi.Invoke(del, new object[] { textValue });

}

else
{

this.hi.Text = textValue;

}

}
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: fetch value of text box outside thread

 
0
  #4
May 23rd, 2008
Look I can't look at your code but I'll give you an example

  1. delegate void Hello();
  2.  
  3. void SetHello()
  4. {
  5. txtBox.Text = "Hello";
  6. }
  7.  
  8. void Perform()
  9. {
  10. Hello setHelloDelegateInstance = new Hello(SetHello);
  11. if (txtBox.InvokeRequired)
  12. {
  13. txtBox.Invoke(setHelloDelegateInstance, new object[] { });
  14. }
  15. SetHello();
  16. }
  17.  
  18. private void btn_Click(object sender, EventArgs e)
  19. {
  20. Thread t = new Thread(new ThreadStart(Perform));
  21. t.Start();
  22. }
You can also code your fetch textbox's text in the same mannar.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC