C# newbie question re: communicating 'between forms' (no flames pls)

  • Thread starter Thread starter bob
  • Start date Start date
B

bob

I'm very new to C# (excuses out of way)

I have a somewhat ambitious if not real world app in mind.

Essentially my windows form runs a timer and every n ticks I read from 1 DB
update some values on the screen and then do a DB write. ( nothing hard here
an app that polls )

however I want to be able to open other windows capture and process some
entered values and return some values to the main windows form ... without
the main windows form waiting till the second windows form has closed ...
same behaviour as non-modal dialog boxes exhibit.

So I guess I'm asking how do I make my "child winforms" non-modal and how do
I pass values back and forth.

can any-one point me at an appropriate tutorial or perhaps give me a
starting point. The last time I tried anything like this I was using Win32
API and doing the application in Dialog boxes .. C# is quite a shift.

regards Bob
 
Create a form object and use the Show() method instead of ShowDialog()
method. You can pass values by creating a member variable and calling
methods or setting properties. Hope this helps.
Curtis
http://www.ghostclip.com
The Premier Help System For Developers
 
Bob,

See inline:
Essentially my windows form runs a timer and every n ticks I read from 1
DB update some values on the screen and then do a DB write. ( nothing hard
here an app that polls )

Also keep in mind that the timer will return and execute on a thread pool
thread and you cannot update the user interface on this thread. See
this.InvokeRequired and this.BeginInvoke or this.Invoke to get your changes
to the User Interface. Furthermore, if you are using a single database
connection on both the main thread and the timer thread, you can run into
issues. For example, OdbcConnection is not thread safe.
however I want to be able to open other windows capture and process some
entered values and return some values to the main windows form ... without
the main windows form waiting till the second windows form has closed ...
same behaviour as non-modal dialog boxes exhibit.

See the Show method on the form you create.

So I guess I'm asking how do I make my "child winforms" non-modal and how
do I pass values back and forth.

Make properties public so they can be accessed by the main for or use
delegates to signal/pass data back to the main form when changes occur.
 
Argh "Threads" ... thats just updated the weekend reading list (but saved a
post later) ... :-(
 
Some stuff you might want to look into, that goes a bit beyond what you
asked.

1. If you're opening non-modal dialogs to prompt the user for
information, I assume that you want only one of each dialog open at any
given time. See Jon Skeet's page on the Singleton pattern. Works with
Forms, too.

http://www.yoda.arachsys.com/csharp/singleton.html

2. If you want multiple forms showing different aspects of the same
information, and you want them all kept in synch, you'll want to look
into the Model-View-Presenter pattern. It may seem like more work at
first, but believe me it makes the application much easier to manage as
its complexity grows.
 
Bob,

Here is an example of guarding against a thread updating a label on a form:

public delegate void StringUpdateDelegate(string sMsg);

protected void UpdateStatusLabel(string sMsg)
{
if (this.InvokeRequired == false)
lblStatus.Text = sMsg;
else this.BeginInvoke(new StringUpdateDelegate(UpdateStatusLabel),
new object[] {sMsg});
}

The funny thing is that if you drop a System.Windows.Forms.Timer control on
the form and send text to this method on the OnTick event, the
InvokeRequired is false, which means you could update the form on the OnTick
event directly. I tend not to use the System.Windows.Forms.Timer control.
I use the System.Threading.Timer, which will definitely require a method
like the one above.

According to MSDN, the System.Windows.Forms.Timer:
(http://msdn.microsoft.com/library/d...ml/frlrfsystemwindowsformstimerclasstopic.asp)
This Windows timer is designed for a single-threaded environment where UI
threads are used to perform processing. It requires that the user code have
a UI message pump available and always operate from the same thread, or
marshal the call onto another thread.

In other words, you can update the UI directly with the
System.Windows.Forms.Timer.

Sorry for the confusion. Like I said, I don't use it. I use the
System.Threading.Timer....

Later,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top