Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
after running
when click on any element from
accordionControl

it opens the same form twice although i made only one instance in code
??

What I have tried:

C#
using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SalesPro1._1.Forms; 
using System.Reflection;  


namespace SalesPro1._1.Forms
{
    public partial class frm_Main : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm
    {
        public frm_Main()
        {
            InitializeComponent();
            accordionControl1.ElementClick+= accordionControl1_ElementClick;
        }

        private void accordionControl1_ElementClick(object sender, 
 DevExpress.XtraBars.Navigation.ElementClickEventArgs e)
        {
            // Error: this code make 2 instances of form

            var tag = e.Element.Tag as string;
            if (tag != string.Empty)
            {
                OpenFormByName(tag);
            }
        }

        public static void OpenFormByName(string name)
        {
            var ins = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == name);
            if (ins != null)
            {
                var frm = Activator.CreateInstance(ins) as Form;
                frm.Show();
            }
        }

    }
}
Posted
Updated 10-Aug-20 7:24am

At a guess, you are adding the accordionControl1.ElementClick handler twice, once in that code and once in the designer.

Since events are "chained", if you add the same handler method to the same event twice, then it will be called twice when the Event occurs.

To prove it needs access to your whole project which we do not have.
Start by right clicking the method name in VS, and select "Find all references" from the drop down - that will list all the places it is used, including designer files.
Alternatively, use the debugger to put a breakpoint on the first line of the method and see how many times it is executed.

Sorry, but we can't do any of that for you!
 
Share this answer
 
Disclaimer: I am not familiar with the DevExpress variety Form used here.

1.Your app is probably creating the first instance of your main form when it runs ... and showing it: that is the standard behavior of a WinForm app. then, you create it again.

2 It is a mistake to create Forms and add them to a container Control: use UserControls, or another control.

3 in OpenFormByName you are creating a Form in a bizarre way using reflection, and not keeping a reference to it outside the method.

You need to study the basics of C# programming with Forms and Controls. Take advantage of DevXpress' technical support if you bought their expensive software.
 
Share this answer
 
Comments
[no name] 10-Aug-20 15:45pm    
A vague answer. So should I vote a #1? No I don't.
when i tried to handle each item alone, it worked through this code

private void ace_CompanyInfo_Click(object sender, EventArgs e)
{
    OpenFormByName("frm_CompanyInfo");
}

private void ace_Drawers_Click(object sender, EventArgs e)
{
    OpenFormByName("frm_Drawers");
}

private void ace_Stores_Click(object sender, EventArgs e)
{
    OpenFormByName("frm_Stores");
}

private void ace_StoresList_Click(object sender, EventArgs e)
{
    OpenFormByName("frm_StoresList");
}


-------------
someone helped me, just change code in
OpenFormByName
with this code:
public static void OpenFormByName(string name)
        {
            var ins = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == name);
            if (ins != null)
            {
                var frm = Activator.CreateInstance(ins) as Form;
                if (Application.OpenForms[frm.Name] != null)
                {
                    frm = Application.OpenForms[frm.Name];
                    frm.BringToFront();
                }
                else
                {
                    frm.Show();
                }
            }
        }
 
Share this answer
 
v2
Comments
Richard Deeming 10-Aug-20 6:52am    
If you want to update your question to add more information, click the green "Improve question" link and edit your question.

Do not post the update as a "solution" to your question.

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