Accessing objects in several forms in C# [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using and instance of a class on two forms
As I am pretty new to OOP - especially with Visual C# - I have a problem understanding the following:
A Windows Forms application has several forms. They all shall have access to a object Client (for communicating with a server) created in one of the forms. But how do I make exactly this object instance available in the other forms?

You can make the Client public static in the form parent form and access it from other forms. You have to make it thread safe if access synchronously.
Public will make it available to other forms where as make a single instance for all forms. To make it public static.
public static Client client.
To access it from other forms, assuming client is in Form1 and being accessed from other form.
Client client1 = Form1.client;

Related

Why can't I start my project with the form I want in c# .net [duplicate]

This question already has answers here:
How to setup the starting form of a winforms project in Visual Studio 2010
(4 answers)
Closed 2 years ago.
In my project there are many forms. I want to start my project with a specific form but my project only allows 1 form to be the initial form.
Why is that and how to fix it?
Startup object points to a class that contains the Main function. In your case, the TeleDoc.FrmMain.
Inside this class you'll find the Main method that somehow creates the main form of the app and shows it, e.g.
public static void Main()
{
Application.Run(new TeleDoc());
}
The argument passed to the Application.Run points to a form that it created as the main app form. If you alter this code, you could either point to another form or even, point to any form depending on a custom condition.

Create new/Reuse an instance of an application (Windows, .NET, WPF) [duplicate]

This question already has answers here:
What is the correct way to create a single-instance WPF application?
(39 answers)
Closed 6 years ago.
I've been struggling with this for more than several hours and cannot think of a solution.
I have an application that can be started in this way:
TestApplication.exe ID={GUID} filename={filename}
If there is not an instance of the application with the same GUID, a new instance should be started with ID={GUID} and the specified file should be loaded in it.
If there is an instance of the application with the same GUID, the user should be asked if he wants to close the file he is working on and if he confirms it - then the new file specified should be opened in the running instance.
Any ideas how to implement this?
Use a Mutex. See the first answer of this question:
What is the correct way to create a single-instance application?
Any ideas how to implement this?
Yes. Question answered. You never ask us to show us our ideas.
Let's get real.
One way is by window title, but having the GUID there is seriously not optimal.
As such, read up on NAMED MUTEXES. You can use that one to identify a program already running.
Alternatively - and better given you must actually send a signal - named pipes. You can register a named pipe with the GUID. Fails: Already exists. THAT allows the new application to actually signal the old one and or send a shutdown command.

How can I launch a Windows application ONLY from another application? [duplicate]

This question already has answers here:
C# : Making an exe to not run directly
(4 answers)
Closed 7 years ago.
I want to have a WPF C# program launch a second WPF C# program, and prevent this second program from being launched any other way. Any ideas?
You could pass the 1st application's Process Id as a parameter, and have the 2nd application look up and validate the calling process.
Compile the second program as a dll, make the entry point internal, and have both applicatios share namespaces. Depending on your level of security, you might want to implement some handshake protocol between both applications.

how to access serialPort1 in another form? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best way to access a control on another form in WinForms?
how can I access serialPort1 which is in form2 in the MainForm(Form1) ?
I want to use the methods which is related to the serialPort1 control ?
Store your SerialPort object in a static location, like in your entrypoint Program class, then it's accessible from anywhere in your project during the life of your program.
Also consider giving your fields more descriptive names than "serialPort1".

Single Instance program in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
single instance and notify in system tray
I am trying to implement a program in C#.NET. My program uses the notification icon. I have made my program a single instance program using the class singleinstance.cs from codeproject.
Now, what I am trying to do is, if I close the main window and then if I run the application again, it should open the main window of the already running program instance in the system tray.
I have googled a lot over this but didn't find something useful for me.
You need to maximize the window of an existing process, correct?
Here is an example of another question: Maximize another process' Window in .NET
To note, in the future, this is a borderline duplicate question, in my opinion. You could have used the search box in the top right of this page to find this answer on your own.

Categories