0

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?

  • It's probably something with your code and it will be more dificult to help you without seeing it. – Alfred Myers Sep 10 '09 at 03:23
  • code: private void Initiate(object sender, EventArgs e) { string set_dateformat = combobox.date.Text; string set_nameformat = combobox.name.Text; } both comboboxes are on the 2nd tab of my tab control. This method is tied to a button on tab #1 –  Sep 10 '09 at 03:27
  • Your question says, "textboxes" but your code is referring to "combobox" – Anand Shah Sep 10 '09 at 06:11

2 Answers2

1

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

Community
  • 1
  • 1
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
0

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 Shah
  • 14,575
  • 16
  • 72
  • 110