textbox and label not immediatly showing up after button click

hello I am trying to make it so when I click a button it opens a new tab and shows a randomly generated account number. When I click my button it makes the new tab and takes me to it but the textbox and label do not show up unless I click the tab.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsApp1
{
internal class CreateNewCustomerTab
{
internal static void CreateNewCustomer(TabControl tabControl, string tabName)
{
// Create a new TabPage
TabPage newTab = new TabPage(tabName);

// Create controls: Label and TextBox for Account Number
Label acctNumLabel = new Label();
acctNumLabel.Text = "Account Number:";
acctNumLabel.Width = 300;
acctNumLabel.ForeColor = Color.Black;
acctNumLabel.Location = new Point(38, 15);

TextBox acctNumTextBox = new TextBox();
acctNumTextBox.Location = new Point(56, 22);
acctNumTextBox.Name = "acctNumTextBox";
acctNumTextBox.Enabled = false; // Prevent user input
acctNumTextBox.ReadOnly = true; // Read-only

// Generate a random account number and display it in the TextBox
Random random = new Random();
int accountNumber = random.Next(1000, 10000); // Change this range as needed
acctNumTextBox.Text = accountNumber.ToString();

// Add controls to the new tab's Controls collection
newTab.Controls.Add(acctNumLabel);
newTab.Controls.Add(acctNumTextBox);

// Add the new tab to the TabControl
tabControl.TabPages.Add(newTab);


// Show the newly added tab immediately
tabControl.SelectedTab = newTab;

// Refresh the control
tabControl.Refresh();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace WinFormsApp1
{
internal class CreateNewCustomerTab
{
internal static void CreateNewCustomer(TabControl tabControl, string tabName)
{
// Create a new TabPage
TabPage newTab = new TabPage(tabName);

// Create controls: Label and TextBox for Account Number
Label acctNumLabel = new Label();
acctNumLabel.Text = "Account Number:";
acctNumLabel.Width = 300;
acctNumLabel.ForeColor = Color.Black;
acctNumLabel.Location = new Point(38, 15);

TextBox acctNumTextBox = new TextBox();
acctNumTextBox.Location = new Point(56, 22);
acctNumTextBox.Name = "acctNumTextBox";
acctNumTextBox.Enabled = false; // Prevent user input
acctNumTextBox.ReadOnly = true; // Read-only

// Generate a random account number and display it in the TextBox
Random random = new Random();
int accountNumber = random.Next(1000, 10000); // Change this range as needed
acctNumTextBox.Text = accountNumber.ToString();

// Add controls to the new tab's Controls collection
newTab.Controls.Add(acctNumLabel);
newTab.Controls.Add(acctNumTextBox);

// Add the new tab to the TabControl
tabControl.TabPages.Add(newTab);


// Show the newly added tab immediately
tabControl.SelectedTab = newTab;

// Refresh the control
tabControl.Refresh();
}
}
}
2 Replies
M B V R K
M B V R K7mo ago
Could you summarize what exactly the issue ?
TheSovereignToad
hello. sorry for the late reply. I got frustrated not being able to fix this so i moved on to something new. I can get you a little video of it though