Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I am new in C# and i need your help.
I have created a single instance csharp application that uses mdi forms using the necessary code that i find on the web most especially here at http://www.codeproject.com[^] .

My question is how do I create a new mdi tab or child when a user attempt to open/run the application twice or more? So instead of showing or setting focus to already the opened application as in Single Instance Application in C#[^], I would like to create a new mdi tab or child from. Please any help is greatly welcomed.

The single instance functionality was obtained from here Single Instance Application in C#[^]

Thank you very much
Posted
Updated 25-Jun-13 13:23pm
v3
Comments
Sergey Alexandrovich Kryukov 25-Jun-13 22:16pm    
What is "MDI tab"? MDI does not have tabs; and you really don't want to use MDI. You probably need to use tabbed interface, which is great.
How you use it depends on UI library, but it's very similar in all cases. Just read the documentation. What's the problem?
—SA
samuelkod 26-Jun-13 5:22am    
Thank you Sergey Alexandrovich Kryukov for your response. My situation is, I have a parent form that uses mdi control that allow child form to be created at run-time by means of button click event. I used the single Instance Application code in the link i have provided in my post to make the application run single instance only. In that code (found here: http://www.codeproject.com/Articles/4430/Single-Instance-Application-in-C), there is a method that determines a running instance of the application and returns true if an instance of the application is already running. What i want to do is to create mdi child form and add it to the parent form if the running instance returns true.
Thank you.
Sergey Alexandrovich Kryukov 26-Jun-13 9:52am    
What is "MDI control"? And MDI child form, MDI parent (the one with MDI client)? The thing is: you should avoid MDI by all means.
—SA

1 solution

C#
if (!OnlyOneProcess())
            {
[] send = Encoding.UTF8.GetBytes("ExecuteLink=Something");
SendUDP(IPAddress.Loopback, MY_PORT, send, false);
                }

C#
public static bool SendUDP(IPAddress ip, ushort port, byte[] send, bool is_broadcast)
       {
           try
           {
               IPEndPoint groupEP = new IPEndPoint(ip, port);

               UdpClient udp = new UdpClient();
               udp.EnableBroadcast = is_broadcast;
               udp.DontFragment = true;
               udp.Send(send, send.Length, groupEP);
               udp.Close();
               return true;
           }
           catch { }
           return false;
       }


And capture the message in socket like that


C#
 tudp = LIB.UDPRead(IPUB.PORT, search_udp);

static void search_udp(Socket sender, EndPoint ep, byte[] data, int recv)
        {
            
    string stringData = Encoding.UTF8.GetString(data, 0, recv );
                        
        }
C#
public static Thread UDPRead(ushort port, dOnUDPRead search)
        {
            Socket udp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, port);
            udp.Bind(iep);

            Thread h_b = new Thread(new ParameterizedThreadStart(TUDPRead));
            h_b.SetApartmentState(ApartmentState.MTA);
            h_b.Name = "BROADCAST";
            h_b.Priority = ThreadPriority.BelowNormal;
            h_b.IsBackground = true;
            h_b.Start(new object[] { udp, iep, search });

            return h_b;
        }
public delegate void dOnUDPRead(Socket sender, EndPoint e, byte[] data, int count);
        static void TUDPRead(object o)
        {
            object[] ar = (object[])o;
            Socket udp = (Socket)ar[0];
            IPEndPoint iep = (IPEndPoint)ar[1];
            dOnUDPRead ev = (dOnUDPRead)ar[2];

            try
            {
                byte[] data = new byte[20480];
                while (true)
                {
                    EndPoint ep = (EndPoint)iep;

                    int recv = udp.ReceiveFrom(data, ref ep);
                    if (recv <= 0) continue;

                    ev(udp, ep, data, recv);
                    Thread.Sleep(1);
                }
            }
            catch (Exception ex) { LIB.Error(ex, LIB.EError.OnlyLog); }
            finally { if (udp != null) udp.Close(); }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900