Open Form in tabcontrol specifies

Asked

Viewed 1,072 times

1

Command to open a form in a specific tabcontrol.

Example: in a form I have a tabcontrol with two tabs: register and consult. Then I wanted to (with a button) open this form right in the tab consult.

  • put the code you already have ready the form and open the form

2 answers

1


Create some way in which the form that will open knows which tab it needs to select and then select it using TabControl.SelectedTab.

You can, for example, pass a enum by parameter in the form constructor.

Vide:

public class Form1 : Form 
{
    public Form1(TipoTab tipoTab)
    {
        if(tipoTab == TipoTab.Cadastro)
        {
            tabControl1.SelectedTab = tabCadastro;
        }
        else
        {
            tanControl1.SelectedTab = tabConsulta;
        }
    }
}

public enum TipoTab
{
    Cadastro,
    Consulta
}

And use would be something like

public static void botaoCadastro_click(object sender, EventArgs e)
{
    var form = new Form(TipoTab.Cadastro);
    form.ShowDialog();
}

public static void botaoConsulta_click(object sender, EventArgs e)
{
    var form = new Form(TipoTab.Consulta);
    form.ShowDialog();
}

0

I’m no expert on C# and I needed that, too, so I took it:

This class locates a form if it is already open on a Tab, if not already, adds a new Tab, opens the form and puts it in evidence...

    // Abrir formulário em uma aba ou por a aba em evidência
    private void IniciaFormNaAba(Form frm)
    {
        int index = -1;

        for (int i = 0; i < tbApp.TabCount; i++)
        {
            if (tbApp.TabPages[i].Text.Trim() == frm.Text.Trim())
            {
                index = i;
                break;
            }
        }

        if(index != -1)
        {
            tbApp.SelectedIndex = index;
        }
        else
        {
            TabPage tabForm = new TabPage { Text = frm.Text };

            tbApp.TabPages.Add(tabForm);

            frm.TopLevel = false;
            frm.Parent = tabForm;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.Dock = DockStyle.Fill;
            frm.Show();

            // Seleciona a Tab
            tbApp.SelectedIndex = tbApp.TabPages.Count-1;
        }
    }

A rotary pushbutton...

    private void btnMedicos_Click(object sender, EventArgs e)
    {
        IniciaFormNaAba(new frmMedicos());
    }

The result is this...

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.