|
Your textbox belongs to a class as well, the form class and by default this textbox has private access so you can't get at it.
Take a step back though, what you are trying to do here is tightly couple the GUI to the logic. You end up with a calculation class which can only be used in conjunction with a particular form. You should call out from the form class to the calculation class, collect the answer in the form class and then assign it to the textbox. The calculation class should be made as reusable as possible and not have a clue about the GUI of the application it is to be used in.
You need to be particularly careful if using worker threads - these should not access GUI components without a thread switch provided by BeginInvoke .
Regards,
Rob Philpott.
|
|
|
|
|
Ok, about not calling a certain form in the class, I agree with you, but I have a problem with "collecting the answer in the form class and then assigning it to the textbox" as you said, because I have to change the text in the textbox everytime the thread calculates a new value for the number. how can I do this in the form? how can I know when to collect the number?
about the last sentence you wrote:
"You need to be particularly careful if using worker threads - these should not access GUI components without a thread switch provided by BeginInvoke."
I didn't understand what u meant.
I'm fairly new to C#, and to threads in particular..
if u could explain further..
The thread I wrote is like this (it actually reads data and then does some calculation)
public class classA
{
Thread ReadThreadProc;
classA() //constructor
{
ReadThreadProc = new Thread(new ThreadStart(OpticsReadThread));
ReadThreadProc.Name = "ReadThread";
ReadThreadProc.IsBackground = true;
ReadThreadProc.Start();
}
private void ReadThread()
{
while (true)
{
Receive();
}
}
public void Receive()
{
//function that handles what the thread reads...
}
public void CloseThread()
{
ReadThreadProc.Abort();
}
}
I don't know if this is called a worker thread...
hope I was clear. if u need any further explanations, let me know.
I'm really stuck here...
thanks ))
|
|
|
|
|
Hi again,
Yes, that's a worker thread you are creating there. A worker thread is a thread which doesn't contain a message pump (ie. GUI message handler). It's not a simple subject but the golden rule is that only the thread which creates a control should access it. Your main thread creates the textbox, and only it should access it, so you need to do a BeginInvoke to switch from the worker thread to the main thread to do the update.
Firstly though, try to decouple the calculations class from the GUI completely. A nice way to do this would be instantiate your calculations class and register the forms interest in an update event it exposes. You can then get the calculations class to run its thread and it will notify the form class via events when it needs to update the GUI. In the forms event handler we will have our BeginInvoke thread switch to ensure the correct thread does the update and the job's a goodun.
Incidentally, I'm not sure about the use of Abort for terminating a thread - I think that's bad but need to check the correct way to do it.
There are some tricky concepts here - thread switching/delegates/events etc., so if I've lost you completely let me know and I'll bosh you together a sample app...
Regards,
Rob Philpott.
|
|
|
|
|
I don't undersatnd what u meant by: "In the forms event handler we will have our BeginInvoke thread switch to ensure the correct thread does the update and the job's a goodun."
what is BeginInvoke thread switch?
can u please write an example of code?
about the termination of the thread - I went by an example I found, and I'm also not sure it's the best way to end a thread. If u have any suggestions, they will be welcomed...
thanks
|
|
|
|
|
Your app will have two threads in it - the primary thread which creates your form and control and then spends most of its life idling but handles button clicks and the like, and the worker thread which churns away in the background.
This worker thread wants to update the GUI but we can't allow this - this must be done by the primary thread. If you do the update on worker thread all will seem well and then one day your program will hang and your life goes down the toilet. This is where BeginInvoke comes in, the worker thread calls this to get the primary thread to do the update for it. I'm not 100% on this but I think it does a PostMessage under the covers.
I'll put a sample together and email it to you. Are you using VS2003 or VS2005? Incidentally, I understand that VS2005 complains if you attempt to update the GUI from the worker thread - this is not the case in 2003.
Regards,
Rob Philpott.
|
|
|
|
|
I'm using VS2005 beta version..
|
|
|
|
|
Will for just an easy way to get access to the text box in simple thoughts
you just have to go to the Form Designer Code And Then change
private System.Windows.Forms.TextBox textBox1;
to
public static System.Windows.Forms.TextBox textBox1;
and also change all
this.textBox1....etc
to
Form1.textBox1....etc
and then you can control the textbox from the class or any where...;D
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
class testing_textbxx
{
int a;
public void change()
{
Form1.textBox1.Text = "This is a good way for Solving";
}
}
|
|
|
|
|
You could do this, but I'd argue it's not the way forward. It defeats the purpose of encapsulation and leads to hideously structured code. And you can't control the textbox from anywhere - doing this from a worker thread as in this example is positively dangerous.
Regards,
Rob Philpott.
|
|
|
|
|
1, do ne one know how to avoid the access prompt that come up when you access emails through code.
2, it prompt several times when u access different Public Properties of MailItemClass's object.
3, How to access the Mail Profiles and Accounts. as I want to send an email through ne particular email account.
Thanks
|
|
|
|
|
The security message box pops up due to the security mechanism in the Outlook object model. One of the ways to circumvent this is to use this Outlook Redemption library here is the link:
http://www.dimastr.com/redemption/
Here is some more info on Outlook security.
http://www.outlookcode.com/d/sec.htm
Also you will find more information about Outlook programing on outlookcode.com
Hope this helps.
|
|
|
|
|
Printing seems to be a pain in .NET. I am currently working on a Word Processor in C# that I started a while back to learn C#. The application is pretty much done except for printing. I can't seem to get formatting to show up in printing or previewing. Is it possible to have a WYSIWYG printing? Wanted to finish it while I'm on Christmas break.
Thanks
|
|
|
|
|
Try this:
http://www.c-sharpcorner.com/winforms/texteditor.asp
http://www.ondotnet.com/pub/a/dotnet/2002/06/24/printing.html
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingprintingprintdocumentclassprinttopic.asp
"Don't worry about the world coming to an end today. It's already tomorrow in Australia"
|
|
|
|
|
|
Hi,
I want the details of IE window as soon as IE window is opened either by double clicking from Desktop icon or from Start menu. So is there any event which gets fired when the IE window is opened which I can capture in my program. My program will be a Windows service written in C#.
Please let me know how it can be done.
Thanks in advance.
Sunil
|
|
|
|
|
Here is a function I wrote that checks if Internet Exlplorer is running and if it is, the function returns true. You can put this function in a function of a Timer class that executes the IsExplorerRunning function at a specified interval - therefore checking every let's say 100 milliseconds. Let me know if you have any questions.
Make sure you include System.Diagnostics:
using System.Diagnostics;//put at the beginning
//put function in class
private bool IsExplorerRunning()
{
Process[] myProcess = Process.GetProcesses();
int i = 0;
try
{
while (myProcess[i] != null)
{
if (myProcess[i].ProcessName.ToUpper() == "IEXPLORE")
{
int i2 = 0;
try
{
while (myProcess[i2] != null)
{
myProcess[i2].Close();
i2 += 1;
}
}
catch { }
return true;
}
i += 1;
}
}
catch { }
int i3 = 0;
try
{
while (myProcess[i3] != null)
{
myProcess[i3].Close();
i3 += 1;
}
}
catch { }
return false;
}
|
|
|
|
|
Hi,
Thanks for your solution. But won't it slow down the system running this code every 100 milliseconds?
Regards,
Sunil
|
|
|
|
|
It works fine on my computer, but I agree there should be a more efficient way of checking. Note the function doesn't contain the code for the Timer object - you can add that to it if you need to. This function checks if Internet Explorer is running. There should be another function which invokes this (IExplorer checking function) The Process object doesn't seem to have a function that checks if a program has started running. If you find a class that has a function that checks that, you don't need the Timer object function to check every so milliseconds.
|
|
|
|
|
I may be wrong but I think if you create a new instance of the internet explorer object and one already exists it will take ownership of the running process
as for the timer object, I would instead start a running thread and set it to sleep for an acceptable durration of time, upon waking it does the check.
|
|
|
|
|
I agree about the thread sleep solution. I didn't have it on a thread though, but it would be better to have it on a separate thread. This was just an example - if you want to you can modify the function so it accepts a ref parameter. Then pass your existing object as a ref parameter to this function.
|
|
|
|
|
I am using C# 2005 beta version. My problem is whenever I stop my code through a breakpoint while debugging the tooltips shown for the functions definations get bold and it remains bold afterwards for ever untill i restart my computer. I am too irritated with this bug. Do anyone know the solution to this problem.
Thanks in advance.
Regards
Sheel
Sheel Gohe
|
|
|
|
|
|
Just searched Google and this message bord without success. Therefore my question:
Is there a GUI library available (both, commercial or free) for creating Windows Forms applications with Windows Forms 2.0?
I'm looking for more advanced controls than currently present in the standard set, like the Visual Studio 2005 IDE uses itself (floating, attachable Windows etc.).
Thanks
Uwe
--
Affordable Windows-based CMS for only 99 €: try www.zeta-producer.com for free!
|
|
|
|
|
|
Thanks, Leppie!
I'm just browsing now...
Did you find any specific? The best would be to have a native .NET version of the great Codejock "Xtreme Command Bars" which I am using successfully for MFC already.
--
Affordable Windows-based CMS for only 99 €: try www.zeta-producer.com for free!
|
|
|
|
|
I was looking for a pretty and free docking control, but couldnt find any. Daniel from #dev suggested DockPanelSuite, which seems ok, but a but buggy and resource hungry. I did see various companies do the VS2005 style docking (which is vewy pwetty)
xacc.ide-0.1.1 released! :) Download and screenshots
|
|
|
|