Results 1 to 10 of 10

Thread: [RESOLVED] Windows Form - Activated from another form, or from itself???

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Resolved [RESOLVED] Windows Form - Activated from another form, or from itself???

    I have a question about something I need to do in my WinForms application. I will preface the question by saying I hate the Form_Activated event.

    I am displaying the same data on two forms. The two forms are frmJE and frmVST. The data is a grid called gvBillableItems. The data can be edited by the user.

    So lets say both these forms are open and frmVST changes the grid. I want to be able to click into frmJE and have it reload its copy of the grid with the changed data. One would think this would be trivial. This is the code I had in frmJE_Activated():
    Code:
                //if ((othertabsCreated & OtherTabsCreated.BillableItems) == OtherTabsCreated.BillableItems)
                //    BillableItemsOperations.LoadBillableItems(jobRow.SONumber, gBillableItems, bJobIsLockedCompleted, cmdSetAllSLM, cmdSetAllTCH);
    It means if I've created the tabcontrol that contains the billable items, reload it. (If the user hasn't opened this tab, there's no need to reload).

    The problem is that within the grid, I have a column with a dropdownlist. It displays a list of employees that I may want to assign the cell value to. When I click the ddl and make a selection, the ddl closes and Form_Activated is called. This totally messes up that I've picked a new employee. That is why I had to comment out the code. Here is my running diatribe:
    Code:
                // TODO: Return to this.  Dilemma between sometimes wanting to reload and sometimes not and how do I know?  OK, pretty sure I took care of this with new switch in frmAddMultipleProductivity 
                //  and whether OK or Cancel is returned by the form as the dialogresult.
                // 01/19/17 - No can do.  This broke my being able to select a new SLM from the dropdownlist when I click a cell.  I guess when the dropdown closes, this is called.  Maybe need a switch to drive.
                //  Problem was reloading billable items on top of what i was trying to do with the current billable items grid.
                //// 01/17/17 - Adding, because I just went to frmEditMultipleProductivity from a cell click and I need to refresh Billable Items.
    There's probably more there than you care to know, but the gist is "this broke my being able to select a new SLM (that means salesman which equals an employee) from the dropdownlist when I click a cell. I guess when the dropdown closes, this is called." (this being the Activated event). "Problem was reloading billable items on top of what i was trying to do with the current billable items grid." " Dilemma between sometimes wanting to reload and sometimes not and how do I know?" That last question is key. If Activated is called because the ddl in the grid closed, I don't want to do anything. If it was called because I clicked on the form (frmJE) from frmVST, or because I closed frmVST, I want to reload the grid.

    Maybe I just hate Form_Activated because I don't understand 100% how to use it...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Windows Form - Activated from another form, or from itself???

    OK, in lieu of getting a good answer here (even though I know I will), my co-developer just said put a refresh button on the tab containing the grid and make the user own it. So if the user updates the grid in frmVST and goes back to frmJE, with or without closing frmVST, it's up to him to know the data is stale and update the grid. I know that is a cop-out but given the # of users of that particular grid on the two forms, it's an acceptable solution for our company. And probably better than the current version of this program, which is in VB6, and I noticed you have to completely close and reopen frmJE to see the updated data. Blah.

    If Sitten Spynne is reading, they will say my UI shouldn't own/manage the data, and I know that, from a previous thread, and I hope to rework this much better in the distant future. In the near future, I just have to get it working so this program can go live.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Windows Form - Activated from another form, or from itself???

    When I click the ddl and make a selection, the ddl closes and Form_Activated is called.
    When you make a selection from your dropdown what is the exact code execution path?

    Does it go straight to your Form_Activated Event from the action of making a selection? or is some other code running once you make a selection, which in turn makes the Form_Activation event run?

    It means if I've created the tabcontrol that contains the billable items
    Also are your 2 forms just 2 separate IDE forms that you have open, or somehow embedded in 2 separate tabs on your tab control? sorry its not completely clear?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Windows Form - Activated from another form, or from itself???

    I believe it's standard Windows behavior that if you open a combobox, for example, on your form then close the cmb, Activated is called. It shouldn't matter how the combobox was created, right? Whether I call a class and dynamically create it versus putting it in the designer? I will have to check that. I really don't think that's my problem though (that I'm creating all the controls programmatically shouldn't be the cause of the problem).

    I have two Windows forms. They both have a tabcontrol displaying a grid called BillableItems. So instead of having in each frmXX.Designer.cs module the same code, the tab is empty at design time and gets created when you run.

    Thanks for your interest in my issue. Today my company is having a big customer and employee appreciation day, and then tomorrow we are being appreciated with a day off, so I probably won't be back to this until early next week, and not first thing because I'm working on another issue my user has deemed "urgent".
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Windows Form - Activated from another form, or from itself???

    I just tested and didn't see the Activated event raised when closing a combo box in a DataGridView. I added a DataGridView to Form1 then added a text box column with the DataPropertyName set to "PersonName" and a combo box column with the DataPropertyName set to "GenderId". I then added this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Dim genderTable As New DataTable
    5.  
    6.         With genderTable.Columns
    7.             .Add("GenderId", GetType(Integer))
    8.             .Add("GenderName", GetType(String))
    9.         End With
    10.  
    11.         With genderTable.Rows
    12.             .Add(1, "Male")
    13.             .Add(2, "Female")
    14.         End With
    15.  
    16.         With genderColumn
    17.             .DisplayMember = "GenderName"
    18.             .ValueMember = "GenderId"
    19.             .DataSource = genderTable
    20.         End With
    21.  
    22.         Dim personTable As New DataTable
    23.  
    24.         With personTable.Columns
    25.             .Add("PersonId", GetType(Integer))
    26.             .Add("PersonName", GetType(String))
    27.             .Add("GenderId", GetType(Integer))
    28.         End With
    29.  
    30.         With personTable.Rows
    31.             .Add(1, "Peter", 1)
    32.             .Add(2, "Paul", 1)
    33.             .Add(3, "Mary", 2)
    34.         End With
    35.  
    36.         DataGridView1.AutoGenerateColumns = False
    37.         DataGridView1.DataSource = personTable
    38.     End Sub
    39.  
    40.     Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    41.         Console.WriteLine("Form1_Activated")
    42.     End Sub
    43.  
    44. End Class
    If I run the project and navigate around the grid, opening and closing combo boxes, I see no Activated events raised other than the initial one.

    That said, if there was an Activated being raised, you could just ignore it if a cell editing session was in progress:
    vb.net Code:
    1. Private cellEditingInProgress As Boolean = False
    2.  
    3. Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
    4.     cellEditingInProgress = True
    5. End Sub
    6.  
    7. Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    8.     If Not cellEditingInProgress Then
    9.         '...
    10.     End If
    11. End Sub
    12.  
    13. Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    14.     cellEditingInProgress = False
    15. End Sub

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: Windows Form - Activated from another form, or from itself???

    I just tested and didn't see the Activated event raised when closing a combo box in a DataGridView.
    JMC hit the nail on the head there as usual. I hadn't tested it but my thoughts were that it seemed strange that it would be the combo itself that was making Form_Activated be called and perhaps if you dug into you would find that its actually something else that is being triggered by your combo that is in turn making Form_Activated run.


    That said, if there was an Activated being raised, you could just ignore it if a cell editing session was in progress:
    I was going to suggest you had potentially 2 options but JMC's is probably the most simple and elegent.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Windows Form - Activated from another form, or from itself???

    Quote Originally Posted by NeedSomeAnswers View Post
    JMC's is probably the most simple and elegent.
    Much like myself.

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Windows Form - Activated from another form, or from itself???

    Hi guys. I am still on this. I don't know what happened over the past 3 months, but I think I left this unsolved. And as is the tendency with bugs, it's back (because technically it never left).

    I did not mention, because maybe I thought the details were irrelevant, but I am using DevExpress controls. The control that is closing up that is causing activated to fire is actually a RepositoryItemGridLookupEdit. Which I believe is a close cousin to combobox JMC tested with. And I have a XtraGrid not the Windows DataGridView (but similar) I will discover what I have that is equivalent to CellBeginEdit/CellEndEdit.

    I would like to also ask...when frm_Activated fires can I test "sender" to see what it is and know who is responsible for it being triggered? I don't know how to test this because it's an object. I don't know if I should cast it as a form or other controls and see what it is, or is there a way to find out what it is from its generic "object" type. Because I was thinking if sender = RepositoryItemGridLookupEdit, I would know to execute appropriate logic. This event is very hard to debug since the very nature of debugging it causes it to fire...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Windows Form - Activated from another form, or from itself???

    The details are relevant....I believe I've figured out what I need to do parallel to JMC. I will post back after I go test with someone. So no further info requested at this time.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  10. #10

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: Windows Form - Activated from another form, or from itself???

    I have the boolean variable JMC suggested.

    DevExpress has an event equivalent to CellBeginEdit() called ShownEditor(). (How I know this is from here).

    Code:
            private void gvBillableItems_ShownEditor(object sender, EventArgs e)
            {
                bCellEditingInProgress = true;
            }
    I found what I thought was the equivalent to CellEndEdit(), which was CellValueChanged() but the same thing was happening with frm_Activated(). That would get called, overriding or preempting CellValueChanged(). So in frm_Activated() I do this:

    Code:
                if ((tabsCreated & TabsCreated.BillableItems) == TabsCreated.BillableItems)
                {
                    if (!bCellEditingInProgress)
                    {
                        bool bEnableSetAllSLM = false;
                        bool bEnableSetAllTCH = false;
                        gBillableItems.gcBillableItems.DataSource = BillableItemsOperations.RunBillableItemsQuery(soNumber, ref bEnableSetAllSLM, ref bEnableSetAllTCH);
                        cmdSetAllSLM.Enabled = bEnableSetAllSLM;
                        cmdSetAllTCH.Enabled = bEnableSetAllTCH;
                    }
                    else
                        bCellEditingInProgress = false;
                }
    So if I'm not editing a cell, I reload the grid - assuming I am in frm_Activate because I am returning from some other form in my same app or the external app that changes this data.
    If I am editing a cell, I don't reload the grid and I turn the switch off.

    It works, so a belated thank you! And if JMC is thinking "no that's not what I meant at all" I am open to some tweaking but so far testing has been the proof.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width