tags:

views:

98

answers:

2

I have a winform with a tab control. On the default tab I have a 'go' button tied to a function that retrieves the values from textboxes on the second tab(default values).

The values come up as "" if I don't first look at the 2nd tab which i'm guessing causes the textboxes to be poulated with the default values.

How do I make the form fill all it's controls on load?

A: 

This works like a charm:

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; 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ComboBox c = new ComboBox();
        Button b = new Button();

        public Form1()
        {
            InitializeComponent();            

            b.Text = "New Button";
            b.Click += new EventHandler(b_Click);
            this.tabPage1.Controls.Add(b);

            c.Items.Add("Hello World");
            c.Items.Add("My Program");
            c.SelectedIndex = 0;
            this.tabPage2.Controls.Add(c);            
        }

        void b_Click(object sender, EventArgs e)
        {
            MessageBox.Show(c.Text.ToString());
        }
    }
}

If not specified at load time the combox listindex defaults to -1 which is "" (blank) so if you know the exact index at which your default values get displayed in the combo box then set the listindex to that index.

Using the code below the first item in the combobox will get selected at run time.

comboBox1.SelectedIndex = 0 (The first item in the combo box)
Anand
+1  A: 

Data Binding doesn't work on invisible control. I found it here. For reference look at this MSDN thread

Jacob Seleznev