Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a simple tab control that lets me filter data based on companies, but i'm having problems in this particulary situation. How could i setup a button at the end of the text on the tabcontrol to close, and have a tab with a "+" sign to add more companies based on a table on mysql?

For example:

I've got 4 tabs, with this described companies:

Test 1
Test 2
Test 3
Test 4
(+)(This tab allows me to open a winform which will have a textbox where i insert the name of the company, which is stored in a table called company.

With that in mind, I wanted something to create tabs, based on a database. When i click the add button it shows me that winform window to insert the company name and then it shows based on the company id, ascending according to company_id, with it will do something like this:


Test 1 - 0 (This tab will be the 1st)
Test 2 - 2 (This tab will be the 2nd)
And so on...

Any ideias how to do this??


(Btw, i'm a newbie yet, so don't judge me...)

What I have tried:

tryied to replicate this without programaticaly create tabs.
Posted
Updated 28-Feb-16 22:16pm
Comments
BillWoodruff 29-Feb-16 14:01pm    
Why aren't you using the built-in WinForms Tab Control ? Adding new Tabs to it is easy.
Scribling Doodle 1-Mar-16 5:33am    
my application requires a programaticaly tabcontrol to add new tabs with companies that have a unique id that is filtered from the database, that's why.

1 solution

Here is what you need.

1. Main form with a TabControl in it. This control will have one blank TabPage with header text as "+".
2. On form load, Insert TabPage based on database content. Here, Insert method should be used rather than Add to ensure that + tab is right most.
3. Handle the TabIndexChanged event for the control. Here, if the index is TabPages.Count - 1, show the form using ShowDialog method.
4. In the pop form, set DialogResult property of OK/Save/Yes button to OK. This will be used in main form to know if OK or cancel was clicked. Also create a public string property so that main form can get the text.
5. In main form, check if PopUpForm.ShowDialog == DialogResult.OK.
6.a. If true, Insert a new tab page before the last one. Tab page name can be obtained from string property.
6.b. Do nothing.
7. Saving on new data can be done wherever you find suitable (either when OK is clicked on popup or from a save button on main form).
8. Dispose the pop up form object.
 
Share this answer
 
Comments
Scribling Doodle 29-Feb-16 4:22am    
I didnt quite understood all this steps, could you explain it more clearly? Thanks in advance!
dan!sh 29-Feb-16 4:23am    
Which ones? Where are you stuck?
Scribling Doodle 29-Feb-16 5:04am    
i couldnt even complete the 1st step :/ Here is what i got so far...

Prints:

https://gyazo.com/edd41bd32f3020915706f88051f2ad86


And this is the code i have so far:

namespace Portaria
{
public partial class Inicio : Form
{
public Inicio()
{
InitializeComponent();
}

private void toolStripComboBox1_Click(object sender, EventArgs e)
{

}

private void Grid_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
public void Gridprop()
{
Grid1.Columns[0].Visible = false;
Grid1.Columns[2].Visible = false;
Grid1.Columns[1].HeaderCell.Value = "Matricula";



}

private void LoadGrid()
{

this.WindowState = FormWindowState.Maximized;
string _con = "server=localhost;user id=root; password = 12345; persistsecurityinfo=True;database=portaria";
string _query = "SELECT ID_TEMP, ID_VEICULO 'Matricula', ID_EMPRESA, (DATAENT+HORAENT) 'Entrada', (DATASAI+HORASAI) 'Saida',VISITADO 'Pessoa Visitada' , EMPRESA_VISITANTE ' Empresa Visitante', EMPRESA_VISITADA 'Empresa Visitada',(OBSENT+OBSSAI) 'Observações' FROM temp_reldiario WHERE id_empresa=" + tabControl1.SelectedIndex +";";
MySqlConnection Con = new MySqlConnection(_con);
MySqlCommand Command = new MySqlCommand(_query, Con);
MySqlDataAdapter sda = new MySqlDataAdapter();
DataTable dt = new DataTable();
sda.SelectCommand = Command;
sda.Fill(dt);
try
{
Con.Open();
DataView dv = new DataView(dt);
Grid1.DataSource = dv.Table;
Grid1.AutoGenerateColumns = true;

for (int i = 0; i < Grid1.Columns.Count - 1; i++)
{
Grid1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
Grid1.Columns[Grid1.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

for (int i = 0; i < Grid1.Columns.Count; i++)
{
int colw = Grid1.Columns[i].Width;
Grid1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
Grid1.Columns[i].Width = colw;
}

Con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void tabControl1_Selected(object sender, TabControlEventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{
LoadGrid();
Grid1.Show();
Gridprop();
TabControl();

}

public void TabControl()
{
ToolStrip ts = new ToolStrip();
ts.Dock = DockStyle.Top;
ts.RightToLeft = RightToLeft.Yes;


ToolStripButton ToolStripButton = new ToolStripButton("X");
ts.Items.Add(ToolStripButton);
ToolStripButton.Click += new EventHandler(ToolStripButton_Click);
/*tabControl1.TabPages[currenttabpage].Controls.Add(ts);
*/
}

void ToolStripButton_Click(object sender, EventArgs e)
{
ToolStripButton t = (ToolStripButton)(sender);
ToolStrip ts = t.Owner;
TabPage tb = (TabPage)
(ts.Parent); tabControl1.TabPages.Remove(tb);
}

private void cmdEntrada_Click(object sender, EventArgs e)
{

}

private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

}

private void Grid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void toolStrip
dan!sh 29-Feb-16 5:07am    
There is lot more in the form than the tab. Since I do not know what your requirements are, I cannot really comment much. I would recommend first create a new form with the tab functionality to understand how it works and then integrate it with the application.
Scribling Doodle 29-Feb-16 5:10am    
Ok, thanks anyway my friend! :)

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