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.
Related
I have a form which contains a dynamically added TableLayoutPanel, which contains some dynamically added Labels, TextBox, CheckBox. I am obtaining exactly the visualization I would like to have, but I am struggling to get the "tab key" to work for moving from one control to the other.
I have tried to add a:
control.TabIndex = tabIndex++;
control.TabStop = true;
But this doesn't seem to have any impact...
This is the (tested) stub code:
class MyForm : Form
{
public MyForm()
{
InitializeComponent();
string[] titles = {"first","second"};
var myLayout = new TableLayoutPanel();
myLayout.AutoSize = true;
int myTabIndex = 1; //Not really necessary
int rowNumber = 0;
foreach (var title in titles)
{
var label = new Label();
label.Text = title;
myLayout.Controls.Add(label, 0, rowNumber);
var control = new TextBox();
control.TabIndex = myTabIndex++; //Not really necessary
myLayout.Controls.Add(control, 1, rowNumber);
rowNumber++;
}
this.Controls.Add(myLayout);
}
}
This is the window I get, and I am not able to navigate from first to second field using the tab key.
Update:
Applying Visual Studio 2013 Update 5 did not help.
Something must have been corrupted in my project. The best I could do is to move on with a new clean Windows Form project, and everything now is working.
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.
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)
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";
I want to create dynamic tabPages in TabControl. In each tabPage I create dataGridView and i want to fill the entire space of each tabPage with this dataGrid. Here is code, where i do this:
private void tabControlMutants_SelectedIndexChanged(object sender, EventArgs e)
{
DataGridView dgw = new DataGridView();
DataGridViewTextBoxColumn testCaseCol = new System.Windows.Forms.DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn resultCol = new System.Windoows.Forms.DataGridViewTextBoxColumn();
//
// dataGridView1
//
dgw.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dgw.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
testCaseCol,
resultCol});
dgw.Location = new System.Drawing.Point(3, 3);
dgw.Name = "dataGridView1";
dgw.AutoSize = true;
dgw.Dock = System.Windows.Forms.DockStyle.Fill;
dgw.TabIndex = 0;
//
// TestCaseColumn
//
testCaseCol.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
testCaseCol.HeaderText = "Test Case";
testCaseCol.Name = "TestCaseColumn";
//
// ResultColumn
//
resultCol.HeaderText = "Result";
resultCol.Name = "ResultColumn";
tabControlMutants.TabPages[(sender as TabControl).SelectedIndex].Controls.Add(dgw);
((System.ComponentModel.ISupportInitialize)(dgw)).EndInit();
//fill dataGridView
}
But it doesn't work, becouse when i resize the main window, data gridView doesn.t change its size (although the dock property is set to fill). Any ideas?
Move the dgw.Dock = System.Windows.Forms.DockStyle.Fill; statement to below the tabControlMutants.TabPages[...].Controls.Add(dgw);line. And maybe below the EndInit(), I'm not sure.
And remove the dgw.Location = ... line because its not needed.
Edit:
I just ran a little test and this basically should work. That means the error is somewhere else, in the code not shown. Maybe in the 'fill rows part'.
I recommend you start removing parts of the code to eliminate the error.
And you do realize you create a Dgv each time a tab is selected, do you? I assume this is demo code.
Remove dgw.AutoSize = true;
Try to add the control first, then set the Dock property