开发者

Wait until user press enter in textbox in another form and return value

开发者 https://www.devze.com 2023-02-13 23:19 出处:网络
I am new to C# and I\'m trying to do sth like this: myList = list of 1000+ string values; 1.StartNewThreads(50); //50 is the numbers of ne开发者_如何学Gow threads

I am new to C# and I'm trying to do sth like this:

  myList = list of 1000+ string values;
  1.StartNewThreads(50); //50 is the numbers of ne开发者_如何学Gow threads
  2.DoSth1(next value from myList);
  3.DoSth2();
  4. var value = {
     ShowNewImageForm(); //show only if not another ImageForm is displayed if another is show - wait
     WaitUntilUserPressEnterInTextBox();
     ReturnValueFormTextbox();
  }
  5.DoSth3();
  6.StartNewThread();

For now I have:

foreach(String s in myList ) {
 DoSth1(s);  
 DoSth2();     
 DoSth3();    
}

And now I'm looking for ideas to points 1,3,6 Can You suggest me how to resolve this?

  1. How to start 50 threads
  2. How to get value from textbox in another form when user press enter


For your second question: Just pass your parent form/owner form in the sub form's constructor and use that reference to pass/set the return value or (more elegant) define a event that will fire on the child form once the user presses the return key and then add an event handler in your parent form.


For start some Threads, you can use a Thread[]

    public static void StartThreads(int n)
    {
        System.Threading.Thread[] threads = new System.Threading.Thread[n];
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i] = new System.Threading.Thread(new System.Threading.ThreadStart(DoThread));
        }
    }

    public static void DoThread()
    {
        //do some here
    }

In the second form you can define this handler:

    public static void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            TextBox myText = (TextBox)sender;
            //your code here...
        }
    }

Then in the first form:

textBox1.KeyDown += new KeyEventHandler(Form2.textBox1_KeyDown);
0

精彩评论

暂无评论...
验证码 换一张
取 消