Access Child controls of split container .net - c#

I am trying to open a file inside a rich text box that is dynamically created in a tabpage on click. But it is inside a split container along with another element. For some reason when I try to access it, I get a Object reference not set to an instance of an object error.
Here's the code:
Dynamic creation of said tab page:
public class Texttab : TabPage
{
readonly RichTextBox _text = new RichTextBox();
ConsoleControl.ConsoleControl ca = new ConsoleControl.ConsoleControl();
private SplitContainer split = new SplitContainer();
public Texttab()
{
split.Dock = DockStyle.Fill;
split.Orientation = Orientation.Horizontal;
split.Name = "split";
_text.Dock = DockStyle.Fill;
_text.Name = "textbox";
_text.Font = fontx;
_text.BackColor = Color.FromName(back);
_text.ForeColor = Color.FromName(front);
ca.Dock = DockStyle.Fill;
ca.Name = "cmdbox";
ca.StartProcess("cmd", null);
ca.Font = fonty;
Controls.Add(split);
split.Panel1.Controls.Add(_text);
split.Panel2.Controls.Add(ca);
}
}
Code accessing the TEXTBOX and opening the text file:
private void OpenToolStripMenuItemClick(object sender, EventArgs e)
{
var dx = new OpenFileDialog();
dx.ShowDialog();
dx.Filter = Resources.Form1_openToolStripMenuItem_Click_Text_Files___txt____txt_Python_Files___py____py_Javascript_Files___js____js_C_Files___c____c_CPP_Files___cpp____cpp_Shell_Files___sh__bat____sh___bat_All_Files__________;
RichTextBox selectedRtb = (RichTextBox)tabControl1.SelectedTab.Controls["split"].Controls["textbox"];
selectedRtb.LoadFile(dx.FileName, RichTextBoxStreamType.PlainText);
}
Thanks, any help on the issue is appreciated. I am almost positive the issue has something to do with the split control. Thanks again!

You will have to access the RichTextBox like this instead since the Panels in the SplitContainer are not named items.
(RichTextBox)((SplitContainer )tabControl1.SelectedTab.Controls["split"]).Panel1.Controls["textbox"]

Related

TableLayoutPanel: How can i navigate with tab key through dynamically added controls?

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.

Programmatically added controls not showing when added to a tab page

Up till now I have been just displaying my programmatically created controls to this.controls but now I want to add in tabbing functionality for large sets of data. I added in a tab control and in the code I have the programmatic controls added to the tabpage but I cannot get the controls to display... help what do I need to do get the controls to display
right now what i have is
private void Form1_Load(object sender, EventArgs e)
{
panel = new Panel();
panel.Location = position;
panel.BorderStyle = BorderStyle.Fixed3D;
panel.Width = 240;
panel.Height = 210;
company = new Label();
company.Location = new Point(panel.Location.X + 10, panel.Location.Y + 10);
company.Text = tempServer.Value.companyName;
company.Font = new Font(company.Font.FontFamily, 12, FontStyle.Bold);
tabs.TabPages["1"].Controls.Add(company);
this.Controls.Add(tabs);
this.Controls.SetChildIndex(tabs, this.Controls.Count);
}
Edit(to help clarify)
I have an application which reads from a database for each tuple in the data base my WinForm application creates a new panel which is then populated with various information with dynamically created labels. the position is then offset and the next panel is created. I was informed that i need to now have my application support tabs. each tab will only show so many panels. my problem accured when i tried to add these dynamically created panels to the tab control instead of this.control. when i did so the panels and their information was no longer being drawn and I cant figure out how to make the panels display
You didn't set Text for your label so you didn't see any thing, try this:
company = new Label(){Text = "some text here"};
The whole code:
private void Form1_Load(object sender, EventArgs e) {
company = new Label{Text = "some text here"};
tabs.TabPages["1"].Controls.Add(company);
this.Controls.Add(tabs);
}

How can I add a hint or tooltip to a label in C# Winforms?

It seems that the Label has no Hint or ToolTip or Hovertext property. So what is the preferred method to show a hint, tooltip, or hover text when the Label is approached by the mouse?
You have to add a ToolTip control to your form first. Then you can set the text it should display for other controls.
Here's a screenshot showing the designer after adding a ToolTip control which is named toolTip1:
yourToolTip = new ToolTip();
//The below are optional, of course,
yourToolTip.ToolTipIcon = ToolTipIcon.Info;
yourToolTip.IsBalloon = true;
yourToolTip.ShowAlways = true;
yourToolTip.SetToolTip(lblYourLabel,"Oooh, you put your mouse over me.");
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip( Label1, "Label for Label1");
just another way to do it.
Label lbl = new Label();
new ToolTip().SetToolTip(lbl, "tooltip text here");
Just to share my idea...
I created a custom class to inherit the Label class. I added a private variable assigned as a Tooltip class and a public property, TooltipText. Then, gave it a MouseEnter delegate method. This is an easy way to work with multiple Label controls and not have to worry about assigning your Tooltip control for each Label control.
public partial class ucLabel : Label
{
private ToolTip _tt = new ToolTip();
public string TooltipText { get; set; }
public ucLabel() : base() {
_tt.AutoPopDelay = 1500;
_tt.InitialDelay = 400;
// _tt.IsBalloon = true;
_tt.UseAnimation = true;
_tt.UseFading = true;
_tt.Active = true;
this.MouseEnter += new EventHandler(this.ucLabel_MouseEnter);
}
private void ucLabel_MouseEnter(object sender, EventArgs ea)
{
if (!string.IsNullOrEmpty(this.TooltipText))
{
_tt.SetToolTip(this, this.TooltipText);
_tt.Show(this.TooltipText, this.Parent);
}
}
}
In the form or user control's InitializeComponent method (the Designer code), reassign your Label control to the custom class:
this.lblMyLabel = new ucLabel();
Also, change the private variable reference in the Designer code:
private ucLabel lblMyLabel;
I made a helper to make life easier.
public static class ControlUtilities1
{
public static Control AddToolTip(this Control control, string title, string text)
{
var toolTip = new ToolTip
{
ToolTipIcon = ToolTipIcon.None,
IsBalloon = true,
ShowAlways = true,
ToolTipTitle = title,
};
toolTip.SetToolTip(control, text);
return control;
}
}
Call it after controls are ready:
InitializeComponent();
...
linkLabelChiValues.AddToolTip(title, text);
It's an way to keep consistent tool tip styles.

Using dynamically created controls in C#

I am creating an application where a user will input grades and the program will output the weighted average. On load, it will ask for the number of categories for the assignments. The program will then dynamically create textboxes for the user to input information. The problem is that I can not figure out how to read the text that is inputed after I create the textboxes. Here is my code:
TextBox txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
How can I change this code so I can find the current text in the box when the user submits?
If you are dynamically generating controls then obviously you won't be able to have a field for each one. But if you are trying to access the Controls collection for a named control, the ControlCollection can be indexed by name. After adding the text box with the specified name, you can simply do:
TextBox txtbx = (TextBox)Controls["txtbx1"];
You could use the FindControl method of the Page class.
This method takes a parameter which is the TextBox's ID, which you have to set upon creation:
txtbx.ID = "txtbx1";
Then you can select it:
TextBox txtbx1 = (TextBox)FindControl("txtbx1");
and use it.
Edit: Since the initial question added that he is refering to Windows Forms, my reply above is off-topic.
In Windows Forms, you should simply use a class member variable instead of a local variable. E.g.:
public partial class MyForm
{
...
private TextBox txtbx;
...
private void createControls()
{
txtbx = new TextBox();
txtbx.Text = "";
txtbx.Name = "txtbx1";
txtbx.Location = new Point(10, 10);
txtbx.Height = 20;
txtbx.Width = 50;
Controls.Add(txtbx);
}
private void someOtherFunction()
{
// Do something other with the created text box.
txtbx.Text = "abc";
}
}
This code for the Dynamically Add Textbox On Button Click
int count = 1;
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = count * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.count.ToString();
count = count + 1;
return txt;
}
private void Onbutton_Click(object sender, EventArgs e)
{
//Call the method AddNewTextBox that uses for Dynamically create Textbox
AddNewTextBox();
}
I hope this code will help you .
Thank You
Happy Coding:)
Keep a list of references of all text boxes on the form. Add the textBox reference to the list when you create them dynamically.
Then you can simply iterate through all text boxes in the list when you want to read their text.
Make sure that you name the text boxes as per their related category names. Then you can also Find the control in the list by their names.
class MyForm : Form
{
IList<TextBox> _textBoxes = new List<TextBox>();
private void AddTextBox(string categoryName){
var myTextBox = new TextBox();
myTextBox .Name = categoryName + "txtbx";
// set other properties and add to Form.Controls collection
_textBoxes.Add(myTextBox);
}
private TextBox FindTextBox(string categoryName)
{
return _textBoxes.Where( t => t.Name.StartsWith(categoryName)).FirstOrDefault();
}
}
All you need to do is set up an OnClick listener for your submit button and have it do something like this
private void OnSubmit(object sender, EventArgs args)
{
string yourText = txtbx.Text;
}
You'll have to keep a reference to the text box after you create it. yourText will contain the value you need. Hope this helps

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