1

Using: C# Forms VS2015

What I'm trying to do:

  1. On form1, I have a textbox (tbJobTitle) and a button (bChooseJobTitle -> form2) for a "Job Title" of an employee.
  2. The textbox(enabled=false) displays the chosen Job Title of an employee.
  3. The button bChooseJobTitle opens another form (form2) that has a datagrid and 2 buttons (Choose & Cancel)

    using System.Threading;
    ...
    Thread the1;
    ...
    private void bChooseJobTitle_Click(object sender, EventArgs e)
    {
        the1 = new Thread(OpenNew_tblJobTitle);
        the1.SetApartmentState(ApartmentState.STA);
        the1.Start();
    }
    private void OpenNew_tblJobTitle(object obj)
    {
        Application.Run(new form2());
    }
    ...
    
  4. I initially set a global string MyVar.Employee_Choose_idJobTitle (default "" ) to store the choosen index primary key if the user selected content and click the Choose button. If the Cancel button is click the MyVar.Employee_Choose_idJobTitle will remain = "".

    //... at form2 "Choose" button
    private void bChoose_Click(object sender, EventArgs e)
    {
        MyVar.idJobTitle = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        this.Close();
    }
    
    private void bCancel_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    
  5. When form2 is closed either by "Choose" button or "Cancel" button, the focus goes back to form1's bChooseJobTitle button.

How do I trigger this event?

...so that can test if the content of MyVar.idJobTitle is not null and add the proper value to my textbox.

I was looking for the button events like onFocus or activate but could not find any. Do I use form events instead to do this?

1 Answer 1

1

Quite simply, use event Form Activate if you like.

    private void form1_Activated(object sender, EventArgs e)
    {
        if (MyVar.idJobTitle != "")
        { 
            tbJobTitle.Text = Choose_idJobTitle; 
            MyVar.idJobTitle = "";
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.