|
Your scenario I find a little ambiguous: you write, on the one hand, about connection threads on the same WinForm, but, then you write of having multiple Forms: can you clarify that a bit ?
In any case you don't want to be calling Application.Run more than once.
If you are describing a scenario in which there isn't the typical pattern of a single new instance of some Form passed as the argument to Application.Run(new MainForm), and you 'Show a bunch of forms before calling Application.Run():
If that is your goal, there's nothing wrong with that: but you then need to consider the implications of that strategy:
1. there's no single Form which, when closed, will close all the other open Forms and exit the Application: you could possibly, by closing all open Forms, leave the Application running without you having any way to access it, or terminate it.
An easy work-around for the problem of closing the Application in this scenario is to write a handler for each Form's 'Closing' event, check and see if the collection of open forms in Application.OpenForms.Count == 1, and if that's the case, do an Application.Exit().
A question: what reason do you have for not creating other forms inside a MainForm launched in the usual way ?
best, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
modified on Tuesday, August 23, 2011 2:38 AM
|
|
|
|
|
Thanks Bill,
Looks very useful the feature about checking the number of openforms for exiting the app.
According to your question, I must develop this application to be able to open up to 8 different forms, because that´s the maximum number of simultaneous connections that the external server can handle from a client (just changing the client_id number).
So, by now, I just have to open these 8 forms when launching the application and see how to handle them like they were independent one from the others.
After this, I´ll do the upper layer for managing how many forms can be launched and all the control stuff.
Thanks again for your time,
Regards,
D.
|
|
|
|
|
Hi, one idea that may be useful if you are required to instantiate all eight forms when the Application starts, rather than creating them on-the-fly in response to events or input: which means you must call 'Show for each Form in your Program.cs static Main method:
You may need to implement control over whether or not one, or more, of your eight forms can be closed, or not: since, if closed, they will have to be re-instantiated by using 'Show again: here's an example of one possible way to handle that case:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
(sender as Form).Hide();
e.Cancel = true;
}
However, in my opinion, if you really don't want the user to close a Form, then best to create the Form without a ControlBox ... but that leaves you with the issue of how, if you allow it, the user can hide forms.
The issue of how to best manage a WinForms application with multiple independent Forms is a very interesting one, and I hope to publish a CP article in the next month on that issue.
One strategy to consider is to have a static class that functions as a Form manager, to which Form events are routed, and in which messages between Forms are dispatched.
best, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
Thanks Bill, that will be useful when handling the quantity of forms that must be launched or when they must be closed, but now I´m in a lower layer so I don´t care about it until I can handle the eight forms independently.
Regards,
D.
|
|
|
|
|
I´ve seen this post update now, thanks bill.
I have done this:
public static List<frmPpal> FormsList { get; private set; }
static void Main()
{
FormsList = new List<frmPpal>();
for (int i = 0; i < 8; i++)
{
frmPpal form = new frmPpal(new DayTradeFacade());
FormsList.Add(form);
form.Show();
}
Application.Run();
So now, I can access to the FormList from the other classes of the Project.
But how can I know which one is active? I mean, from which one has the user called.
ActiveForm doesnt seem to work properly because frmPpal is not a default Form.
Any suggestions?
Thank you for your time and help,
Regards,
D.
|
|
|
|
|
1. a function that returns any type of object can be called without using the result:
private string whale(string theSpeciesOfWhale)
{
return theSpeciesOfWhale + " is a whale";
}
private void someMethod()
{
whale("Baleen");
} 2. You can add a new Class to a Project, remove the NameSpace declaration added by VS, and in it define classes, dynamic, or static: you can then access these classes from anywhere else in your Project directly:
using System;
public class anyClass
{
public int someInt;
} At first, I thought perhaps there was an 'implicit default' NameSpace based on the Assembly name, but trying to access a class defined in a no-NameSpace form by using the Assembly name + dot + class name: does not work.
Both of these use-cases were just 'stumbled upon' by accident, and somehow they seem, to me, to be not 'congruent' with the strongly typed nature of C#.
In the case of allowing a function to be called, and not handling its return value, I can kind of see the rationale for that: in one instance you might call said function just for a side-effect, and in another instance you might want to do something based on the value it returned. I would not want to write code that allows even a slight ambiguity of interpreted use, so, in that case, I would write two functions.
The second case just seems like a 'puzzle:' when 'anyClass is defined above, does it exist in a NameSpace ... or not ?
Well, what say you: is this another instance of my inability to perceive the glorious meta-structure of .NET and C# in Visual Studio ?
best, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
The first one is quite convenient in the case where a 'function' actually has side effects and you may not care about the return value. It stops you having to write
sink = Whale("baleen");
... and then probably getting a warning that 'sink is assigned but the value is never used' for your trouble. It's also inherited behaviour from C.
The second one is not in a namespace. The default namespace is just a VS thing, namespaces on classes are simply defined within the source file, and if you don't specify one, your code is not in a namespace.
|
|
|
|
|
I would expect neither of those cases to throw an error.
In the first one, you're just ignoring (dropping) the return value without using it. Lots of languages allow this behavior.
In the second, your code is still in a namespace. Open the Project Properties and you'll find "Default Namespace" specified.
|
|
|
|
|
Thanks, Dave,
My observation that using the return value of a function is not required in C# was, perhaps, surprising to me since I associate such lexical 'slack' as being found in more weakly typed languages (LISP, PostScript in my experience).
Personally I would like the C# compiler to flag cases where I ignore the return type of a function.
The C# compiler will certainly not let you get away with this:
private void someFunc(string someValue)
{
}
private string someFunc(string someValue)
{
}
best, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
Correct. You can't vary an overload just by the return type.
But, in this example, the compiler will throw if you specify a return type, but don't return anything.
|
|
|
|
|
Dave Kreskowiak wrote: In the second, your code is still in a namespace. Open the Project Properties and you'll find "Default Namespace" specified.
Wrong, code without a namespace ends up in the global:: namespace. The "Default Namespace" in the Project Properties is used for the autogenerated classes like resources and settings, etc.
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
Crap, my bad. I was thinking about VB.NET's version.
|
|
|
|
|
Must be a slow news day.
|
|
|
|
|
in C# a method may or may not have a return type (other than void ), and when it does you may or may not be interested in it.
The same idea has existed ever since C and C++ came to be; and it is different from the Basicy function/subroutine dichotomy.
So there is no error whatsoever.
However, there is one particular case where a warning message would be extremely welcome:
string text="...";
text.Insert(0, "aha");
since strings are immutable, string methods like the one shown are returning strings, ignoring the return value does not make much sense (unless you really want to test for valid parameters and dealing with a possible exception).
|
|
|
|
|
+5 . The example you mentioned is perfect. Beginners in .NET who are unaware of the immutability of strings write such code and wonder why their strings have not changed. A compiler error (or at least a warning) would be welcome.
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
In the first case, almost all languages allow you to do that, they just discard the return value.
In the second case, code not enclosed in a namespace declaration ends up in the global:: namespace. If there is a naming conflict between two classes, one inside a namespace and the other outside of any namespace, then the latter can be access using the global:: namespace.
namespace WindowsFormsApplication1 {
class Class2 {
}
static class Program {
[STAThread]
static void Main() {
Class2 c2 = new Class2();
c2.DoSomething();
global::Class2 c3 = new global::Class2();
c3.DoSomething();
}
}
}
class Class2 {
public void DoSomething() {
}
}
"Don't confuse experts with facts" - Eric_V
|
|
|
|
|
hello everyone,
i don't know much about service application,
i want to know dat can we create window services in VS2008
using c# ?
i didn't find window service template in my VS2008 professional edition.
and Is this possible that my window app which is develop in
c# VS2008 can work as window service on vista.
thank you.....
-- Modified Monday, August 22, 2011 3:20 AM
|
|
|
|
|
As far as I know the template is only available for C++. However, you may find some reasonable ideas here[^].
|
|
|
|
|
It's available in the Windows section of the c# project types and is called "Windows Service", although I'm not sure if it's installed by default in all versions of VS but would have thought it was in the professional edition.
|
|
|
|
|
yes i found it thank you......
but can i call my app exe in window service.
|
|
|
|
|
I've re-read you question.
Yes you can use your application as a service but not without some modification. If most of the functionality is in class libraries then your application (be it a console application or a windows service) acts as an entry point to your functionality.
You will need to do some extra work around error reporting and service specific things like startup options but a basic introduction to windows services via google will help with this.
modified on Monday, August 22, 2011 9:17 AM
|
|
|
|
|
Hi All,
First of all I would like to thank you for the support. I am new in coding and need assistance in getting a specific xml content to an excel spreadsheet or to a datagridview.
the XML file is not a standard one (i think and below is a sample).
what i need from this xml file is the following data:
the name (Towers xxx] of the alert and the gps points (dont need the utm)
this is the xml
[code]
="1.0"="UTF-16"
<layer>
<name>
SF Zones
</name>
<alert_area_object>
<creation_time>
1303889594
</creation_time>
<name>
Tower 1
</name>
<description>
Fence Alert
</description>
<event_types num="1">
<event_type>
11111
</event_type>
</event_types>
<event_severity>
2
</event_severity>
<event_override_flag>
false
</event_override_flag>
<identities num="0"/>
<categories num="0"/>
<groups num="0"/>
<target_appear_in_area>
true
</target_appear_in_area>
<target_enter_area>
true
</target_enter_area>
<target_leave_area>
true
</target_leave_area>
<from_hour>
0
</from_hour>
<from_minute>
0
</from_minute>
<to_hour>
23
</to_hour>
<to_minute>
59
</to_minute>
<from_speed>
0
</from_speed>
<to_speed>
98.8889
</to_speed>
<weighting>
<weight>
<type>
2
</type>
<value>
99
</value>
</weight>
<weight>
<type>
204
</type>
<value>
99
</value>
</weight>
<weight>
<type>
205
</type>
<value>
99
</value>
</weight>
</weighting>
<visual_feedback>
true
</visual_feedback>
<sound_repeats>
-1
</sound_repeats>
<wave_file>
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
</wave_file>
<utm_zone>
40
</utm_zone>
<shape type="ClosedPolygon">
<num_of_alloc_points>
22
</num_of_alloc_points>
<num_of_points>
5
</num_of_points>
<geo_position lat="40.76876678920284" long="-73.98097831075269"/>
<geo_position lat="40.76869191861909" long="-73.98078254132794"/>
<geo_position lat="40.76839192572469" long="-73.97999998924834"/>
<geo_position lat="40.76749000414105" long="-73.97804528571041"/>
<geo_position lat="40.76643902004108" long="-73.97593868227233"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
</shape>
</alert_area_object>
<alert_area_object>
<creation_time>
1303890003
</creation_time>
<name>
Towers 1 to 4
</name>
<description>
Fence Alert
</description>
<event_types num="1">
<event_type>
11111
</event_type>
</event_types>
<event_severity>
2
</event_severity>
<event_override_flag>
false
</event_override_flag>
<identities num="0"/>
<categories num="0"/>
<groups num="0"/>
<target_appear_in_area>
true
</target_appear_in_area>
<target_enter_area>
true
</target_enter_area>
<target_leave_area>
true
</target_leave_area>
<from_hour>
0
</from_hour>
<from_minute>
0
</from_minute>
<to_hour>
23
</to_hour>
<to_minute>
59
</to_minute>
<from_speed>
0
</from_speed>
<to_speed>
98.8889
</to_speed>
<weighting>
<weight>
<type>
2
</type>
<value>
99
</value>
</weight>
<weight>
<type>
204
</type>
<value>
99
</value>
</weight>
<weight>
<type>
205
</type>
<value>
99
</value>
</weight>
</weighting>
<visual_feedback>
true
</visual_feedback>
<sound_repeats>
-1
</sound_repeats>
<wave_file>
C:\4C\RunTime\OWP\Sounds\sirenwawa.wav
</wave_file>
<utm_zone>
40
</utm_zone>
<shape type="ClosedPolygon">
<num_of_alloc_points>
22
</num_of_alloc_points>
<num_of_points>
6
</num_of_points>
<geo_position lat="40.76876678920284" long="-73.98097831075269"/>
<geo_position lat="40.76869191861909" long="-73.98078254132794"/>
<geo_position lat="40.76839192572469" long="-73.97999998924834"/>
<geo_position lat="40.76749000414105" long="-73.97804528571041"/>
<geo_position lat="40.76643902004108" long="-73.97593868227233"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
<utm_points x="132131" y="132131"/>
</shape>
</alert_area_object>
</layer>
[/code]
thanks again
|
|
|
|
|
Use XSLT to transform the XML and extract the data you need into the proper format.
I know the language. I've read a book. - _Madmatt
"The OP herself was not sure about her question"
"The OP is from India and I know what she meant." - Shameel
|
|
|
|
|
Hi,
this is the code i currently have but it parse the xml but is missing some nodes and is not exactly what i need.
i need that it will get the name of the alert and all the gps poings while with this code it takes most of the data except the gps points
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(@"C:\Desktop\Book Application\zone.xml");
DataSet ds = new DataSet("book DataSet");
ds = xmlDatadoc.DataSet;
dataGridView1.DataSource = ds.DefaultViewManager;
dataGridView1.DataMember = "alert_area_object";
}
}
}
thanks
modified on Thursday, August 25, 2011 3:04 AM
|
|
|
|
|
Edit this and use the code block toolbar item to properly format the code snippet.
I know the language. I've read a book. - _Madmatt
"The OP herself was not sure about her question"
"The OP is from India and I know what she meant."
|
|
|
|
|