How to add a control to a added tab C# - c#

I am creating a simple application that keeps track of coins. I have a button that creates a new tab in a tabcontrol container. I am wanting to add some textboxes to the newly added tabs. The below code adds a textbox to my main tab called "Control". I have tried playing around with that field, but it always adds it to the main page called control. How would I do this? I have the following code:
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "TextBoxName";
tb.Size = new System.Drawing.Size(184, 20);
Control.Controls.Add(tb);
//put data inside of textboxes
tb.Text = txtCoin.Text;

myTabPage.Controls.Add(tb);

You need to add the text box to the controls collection of the new tab:
//Control.Controls.Add(tb); //Replace this line
myTabPage.Controls.Add(tb);

change
Control.Controls.Add(tb)
to
tabControl1.TabPages.Last().Controls.Add(tb)
tabControl1.TabPages.OfType<TabPage>().Last().Controls.Add(tb)

Related

C# getting form reference in an eventhandler

My problem is not complicated but i've been looking up for quite some time now and can't seem to find the correct way to phrase it so if this is a duplicate I am sorry.
I have a Form created on a button click, within this Form is a table layout which contains a set of Controls (a label, a combo box and a button). What I want to do is to store the current index selected in the combo box when the button is pressed. However, I can't access the combo box when I'm in the button click event handling block.
This is the creation of my Form, nothing extraordinary.
Form settingsWindow = new Form();
settingsWindow.Text = "Connexion settings";
settingsWindow.MaximumSize = new System.Drawing.Size(200, 200);
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
Label label = new Label();
label.Text = "Choose an available COM port";
Button bt_OK = new Button();
bt_OK.Text = "valider";
bt_OK.Anchor = AnchorStyles.Bottom;
bt_OK.Name = "bt_OK";
bt_OK.Click += Bt_OK_Click;
ComboBox cb_portList = new ComboBox();
SerialComm serial = new SerialComm(); //Filling the combo box with a list of
List<string> portList = new List<string>(); //available COM ports
portList = serial.getAllPorts();
foreach(string portName in portList)
{
cb_portList.Items.Add(portName);
}
layout.Controls.Add(label);
layout.Controls.Add(cb_portList);
layout.Controls.Add(bt_OK);
What I'd like to be able to do is the following, imagine I made a class to store the data I want :
private void Bt_OK_Click(object sender, EventArgs e)
{
datacontainer.selectItem = cb_portList.SelectedItem;
settingsWindow.close();
}
Am I not able to do so because my Form is already created within an eventhandler ? Or is it just because they're in separate blocks. If so is there a way the new Form and its controls available from outside the block ?

How to retrieve text from a TextBox that was created programatically

Alright, so I have a form set up that contains a label and a button. When the button is pressed it creates several labels and and two textfields in a specific area.
I cannot for the life of me, figure out how to retrieve the text from those textfields and store it in a public string.
Any help would be wonderful and greatly appreciated.
Edit: As per request.
TextBox playertextbox = new TextBox();
playertextbox.Location = new Point(460, 200);
this.Controls.Add(playertextbox);
You can assign a name to the textbox and later use ControlCollection.Find to retrieve it
Try this
TextBox playertextbox = new TextBox();
playertextbox.Location = new Point(460, 200);
playertextbox.Name = "playertxtBox"; // Add some name
this.Controls.Add(playertextbox);
Then use the name in the button click handler or similar :
//Use that name to search here
TextBox playertextbox = ((TextBox) this.Controls.Find("playertxtBox",true)[0]);
string text = playertextbox.Text;

Adding Space into a StackPanel full of Textboxes

I am new at C# & XAML development. I created a metro app with several textboxes. These textboxes are loaded in XAML data through a StackPanel in C# code, it has to be hardcoded. The problem is, I have no clue how I can add some empty spaces between every single textbox. Has anyone an idea?
The Code :
private void AddLastestCreatedField()
{
// Load the last created Field From DB
DBFunction.FieldTypes latestField;
DBFunction.Class1 myDBClass = new DBFunction.Class1();
latestField = myDBClass.GetLastestField();
// add new textbox and put it on the screen
var dragTranslation = new TranslateTransform();
//Generate the TextBox
TextBox fieldTextBox = new TextBox();
fieldTextBox.Name = "fieldTextBox_" + latestField.ID.ToString();
fieldTextBox.FontSize = 15;
fieldTextBox.Background.Opacity = 0.8;
ToolTip toolTip = new ToolTip();
toolTip.Content = latestField.Description;
ToolTipService.SetToolTip(fieldTextBox, toolTip);
fieldTextBox.IsReadOnly = true;
// Add Drag and Drop Handler for TextBox
fieldTextBox.ManipulationMode = ManipulationModes.All;
fieldTextBox.ManipulationDelta += fieldTextBox_ManipulationDelta;
fieldTextBox.ManipulationCompleted += fieldTextBox_ManipulationCompleted;
fieldTextBox.RenderTransform = dragTranslation;
dragTranslationDict.Add(fieldTextBox.Name, dragTranslation);
fieldTextBox.RenderTransform = dragTranslation;
// Add TextBox to a List to control later
TxtBoxList.Add(fieldTextBox);
// Generate TextBlock for each TextBlock
TextBlock fieldTextBlock = new TextBlock();
// fieldTextBlock.Name = "fieldTextBlock_" + cnt.ToString();
fieldTextBlock.TextAlignment = TextAlignment.Right;
fieldTextBlock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
fieldTextBlock.Name = "fieldTextBlock_" + latestField.ID.ToString();
fieldTextBlock.Text = latestField.Name;
fieldTextBlock.FontSize = 15;
fieldTextBlock.Height = 33;
// Add Drag and Drop Handler for TextBlock
var dragTranslation2 = new TranslateTransform();
fieldTextBlock.RenderTransform = dragTranslation2;
dragTranslationDict2.Add(fieldTextBlock.Name, dragTranslation2);
// Add TextBlock to a list to control later
TxtBlockList.Add(fieldTextBlock);
TextBoxStack.Children.Add(fieldTextBox);
TextBlockStack.Children.Add(fieldTextBlock);
}
I'll skip the usual "What have you tried?" question and say you probably can get what you need by setting the Margin property on the TextBox - the Margin property will add "space" around the control size as a sort of padding (not to be confused with the Padding property, which will add space inside the control extents)
I don't know what you are really up to, but either use the Margin-property of the textbox. It defines, how much space there will be around the control,
See MSDN for more information.

Adding and removing dynamic controls Windows Forms using C#

I have three Tabs in my Windows Forms form. Depending on the selected RadioButton in the TabPages[0], I added few dynamic controls on the relevant TabPage. On the Button_Click event the controls are added, but the problem is I'm not able to remove the dynamically added controls from the other (irrelevant) TabPage.
Here's my code:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
Where am I making that mistake?
First of all, tbMax's name is not "tbMax", but "txtBoxNoiseMax". So for one, it won't be able to find the TextBox on RemoveByKey.
You're making new controls each time.
As lc already mentioned:
You named your TextBox variable tbMax, but you gave it the name txtBoxNoiseMax. If you take a look into the description of RemoveByKey, you'll see it works on the Name property. So you should change
tbMax.Name = "txtBoxNoiseMax";
into
tbMax.Name = "tbMax";

Adding and removing tab pages at runtime in C#

I have a program that I want each person to have their own tab, each tab would be identical, however I would like to remove a tab if I need to.
private void addPerson(string name)
{
TabPage tmp = new TabPage();
ListView tmpList = new ListView();
Button tmpButton = new Button();
this.SuspendLayout();
this.tabFrame.SuspendLayout();
tmp.SuspendLayout();
tmpList.SuspendLayout();
tmpButton.SuspendLayout();
...
//build the controll itself
tmp.Controls.Add(tmpButton);
tmp.Controls.Add(tmpList);
tmp.Location = new System.Drawing.Point(4, 22);
tmp.Name = name.Replace(' ', '_');
tmp.Padding = new System.Windows.Forms.Padding(3);
tmp.Size = new System.Drawing.Size(284, 240);
tmp.TabIndex = 3;
tmp.Text = name;
tmp.UseVisualStyleBackColor = true;
//add it to frame
this.tabFrame.Controls.Add(tmp);
tmpButton.ResumeLayout(true);
tmpList.ResumeLayout(true);
tmp.ResumeLayout(true);
this.tabFrame.ResumeLayout(true);
this.ResumeLayout(true);
{
Name will be in the form "Scott Chamberlain" so I remove the spaces and use underscores for the name field. I can add tabs fine, they show up correctly formated, however when I try to remove the tab using the code:
private void removePerson(string name)
{
this.SuspendLayout();
this.tabFrame.SuspendLayout();
this.tabFrame.Controls.RemoveByKey(name.Replace(' ', '_'));
this.tabFrame.ResumeLayout(true);
this.ResumeLayout(true);
}
The tab does not disappear from my program. What am I missing to remove a tab?
(source: codinghorror.com)
Creating a simple TabPage with a specific Name and adding it to Controls or TabPages works and so does removing it with RemoveByKey on both Controls and TabPages.
Is there any code that might later change the name?
Use tabFrame.TabPages instead of tabFrame.Controls, for both the Add() and RemoveByKey() operations.
TabPages is a more specified version of Controls, and if such a situation occurs you are better of with the more specialized option.

Categories

Resources