|
Hi,
I have the Class 'Buy.cs'. The method 'openBuyWindow' contains code to create and open a new Windows Form.
In the Program.cs class following code works correctly and the new Form is shown:
Buy b = new Buy();
b.openBuyWindow();
In the class Buy.cs I also have the method 'buy'. The method 'buy' calls 'openBuyWindow'. If i start the method 'buy' via threading, all procedures are done correctly, but the new Form is not shown:
Buy buy = new Buy();
Thread t1 = new Thread(new ThreadStart(buy.buy));
t1.Start();
Why?
Any suggestions appreciated
|
|
|
|
|
Just example:
...
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(InvokeNewForm);
t.IsBackground = true;
t.Start();
}
private void InvokeNewForm()
{
Invoke(new MethodInvoker(CreateForm));
}
private void CreateForm()
{
Form1 form1 = new Form1();
form1.Show();
}
...
|
|
|
|
|
Tank you very much, but I need to pass some parameters to my Form Object. Methodinvoker does not support this - how can I solve this problem?
|
|
|
|
|
It can be done like this
BeginInvoke(new MethodInvoker(
delegate() { method(parameters); }) );
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Tank you but somehow this won't work - BeginInvoke is not recognized from VS...
|
|
|
|
|
It works when I implement the function
private void BeginInvoke(MethodInvoker methodInvoker)
{
methodInvoker.Invoke();
}
but the form is also not shown.
For the glimpse of a second I see another window in the taskbar but it disappears almost instantly.
|
|
|
|
|
Ok I found out that when I stay in the method where the Form was created with the simple command
while (Program.buywindow_open)
{
Thread.Sleep(10);
}
the window is shown, but not responding....
|
|
|
|
|
That's because as soon as the form is shown the thread is done and so the form closes, here you're just delaying it by 10ms. What you need is the thread to not end until the form closes.
I'm no expert in threading, I know nothing apart from the principles, so treat these ideas with great caution!
You can call ShowDialog() instead of Show() as it doesn't return until the form is closed so it will stay open and be responsive, but not act as a Dialog to the form on the main thread.
void OpenBuyWindow()
{
Form form = new Form();
form.ShowDialog();
}
An ugly (maybe dangerous?) alternative is to replicate what happens in the Program.cs
void OpenBuyWindow()
{
Application.Run(new Form());
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Many thanks! Form.ShowDialog works!
|
|
|
|
|
gehbitte wrote: Why?
Controls (a Form is a Control) can only be operated upon by the thread that creates them, as they are not thread-safe. Controls reside in Forms, and Forms are interconnected in many ways (Parent, z-order, etc). The net result is all Controls need to be created and operated upon by the main thread only; for anything else you need to use Control.Invoke
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I was just wondering if anybody knows how to fix the scrollbar problem with the TextBox control in WinForms. When I set ScrollBars.Horizontal, it still doesn't show up, even when the text extends the width of the textbox.
Here's the code I'm using:
t.ScrollBars = ScrollBars.Horizontal;
I've searched on Google, Bing and MSDN, but all I keep seeing are Custom TextBox control's made by other people which is not what I'm after. I'm not opposed to the idea, but all I really want is to use a simple, standard control. Has anybody had this sort of problem before?
|
|
|
|
|
It'll only work if Multiline is true and WordWrap is false.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Sorry DaveyM69 I should have also mentioned that WordWrap is false and that Multiline is also set to true. MKy bad. Thanks for your response though.
|
|
|
|
|
|
Don't make accusations based on bullshi7. I only created this account no more than an hour ago. Idiot. I have not re-posted anything, ever.
modified on Monday, July 20, 2009 9:49 AM
|
|
|
|
|
iscript wrote: Don't make accusations based on bullshi7. ... Idiot.
Nice one. You want to become popular right away?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
I'm sorry Luc, I don't mean to offend you, or anybody for that matter, but I stand by my statement. I'm not proud of what I said, but I don't like being accused of doing something that I haven't done. I've been accused of such things in the past on other forums and won't tolerate it anymore.
I completely understand that if a message hasn't been answered right away that obviously other people don't know the answer OR don't feel like replying OR are too busy OR etc etc... Nobody on here has to give help. And I understand that.
What I don't understand is how someone could accuse me of re-posting when it obviously isn't the case.
Anyway, I think I might go and find another forum or something where I feel more relaxed and don't get accused of reposting.
|
|
|
|
|
Hi iscript,
chill out man. the reply you've got earlier probably was ill-advised. However that is no reason for name calling; we do get a lot of new members who are not familiar with the site rules, and start posting similar questions everywhere and all the time; and create several accounts for no good reasons. So we sometimes are on the defensive here.
I hope you'll get answers to your questions, and you learn to value this site, as CodeProject is a wonderful community.
Cheers.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Thank you Luc, for making me feel welcome.
I'm sorry to hear that you're (C.P Members) on the defensive at times due to similar questions. That must be irritating. I'll be on my best behavior from now on. I'm going to apologize to that person now.
Thanks
|
|
|
|
|
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi Stancrm,
Thanks for the link. I should have mentioned in my original post that I already had Word-Wrap set to false and Multiline set to true, I completely forgot to mention it. As for my insult, I'm sorry I lashed out at you like I did, it wasn't very nice. I'm sorry.
iScript.
|
|
|
|
|
iscript wrote: I've searched on Google, Bing and MSDN
You didn't search too hard then - its on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.scrollbars.aspx[^]
It says that Horizontal scroll bars are only avaiable when Wordwrap is set to false. So, either you have wordwrap on, or you have multiline off - in which case there is no space for a scroll bar so you don't get one either.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
I have multiline set to true and wordwrap set to false. The horizontal scrollbar has always worked. But for some reason it doesn't work anymore. It never shows up.
|
|
|
|
|
I just tried it (VS 2008 SP1) and I got it/them. As long as you also allowed space for the scroll bar.
|
|
|
|
|
In my application I have a MainMenu docked to the top, a ToolStrip docked beneith the MainMenu, a StatusStrip docked to the bottom and a TextBox control docked to fill the remaining part of the Form (in the middle). So maybe it's not working simply because there is a StatusStrip directly under it. Hmm... But then again, it has worked in the past. Which is why I'm confused.
I tried the code found in the pages I visited from the links above (replies) but even though the code is different, I have the same problem.
Thanks for your help, I appreciate it.
iScript.
|
|
|
|