How to retrieve text from a TextBox that was created programatically - c#

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;

Related

c#, wpf, Dynamically naming controls

How do i create a new Button/Canvas with a dynamic name?
Button {buttonname read from text file} = new Button;
I have googled this for a while now but i can't find the solution.
Thank you!
I'm not sure if I understood correctly, but that name in your example is not the button name, it's just the reference name used in code to access the button. The button name would be set like this:
buttonRefName.Name = "ButtonName1";
So you can set the name to whatever you want: dynamically generated names inside a loop, names read from a file, etc...
You can use the same reference name for multiple buttons, just be sure to add it to List or to WPF Window, Panel, etc... before creating the new one:
var buttonList = new List<Button>();
var buttonRef = new Button { Name = "YourButtonName" };
buttonList.Add(buttonRef);
buttonRef = new Button { Name = "YourButtonName2" };
buttonList.Add(buttonRef);
It not possible the way you want to do it. If you are reading from a text file better use a List or better a Dictionary... an example use is as follows:
var buttons = new Dictionary<string, Button>();
buttons["yourName"] = new Button();
// logic goes here

How to append a link label to an existing label?

I have a label inside a panel to display simple text. Now I have a link label that is added dynamically that displays some more information. How can I show this link label right next to the label text at run time? For example, the lable displays
A record is added.
I need to show with a link label "View Additional Details" next to the label text.
A record is added. View Additional Details
I have the code as below but it overlaps the existing label text. Thanks for any help!
LinkLabel details = new LinkLabel();
details.Text = "View Additional Details";
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = infoDetails;
details.Links.Add(link);
details.LinkClicked += new LinkLabelLinkClickedEventHandler(details_LinkClicked);
//Adding the link label control to the existing label control
lblInfo.Visible = true;
lblInfo.AutoSize = true;
lblInfo.Controls.Add(details);
Why are you trying to add a LinkLabel to a label? Add the LinkLabel to the same form as the label, and set the location of the LinkLabel appropriately.
In the example below, I'm assuming the code is being called from the form's class (or to a panel if you are using one). If not, replace this with your form instance. I'm setting the Y location as the same as lblInfo so the LinkLabel appears next to it. Adjust lblInfo.Margin.Right and details.Margin.Left as desired.
details.Margin.Left = 5;
details.Location = new Point(
lblInfo.Location.X + lblInfo.Width + lblInfo.Margin.Right + details.Margin.Left,
lblInfo.Location.Y
);
this.Controls.Add(details);
Update: changed padding to use Margin (thanks Anthony).

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.

How to add a control to a added tab 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)

C# code behind Required validation

I am writing a c# code for a Required field validator for a Multiline text box.
I have a issue in the run time:
when i won't enter any text inside the
text box
For first Click on submit (Button) it shows the error message
For second Click on submit it won't validate the text box and the form is submitted.
Same two issues when i even enter any
text inside the text box.
Overall it is not validating...
Please help me on what could be the possible bug in the below code.
txtReport = new InputFormTextBox();
txtReport.TextMode = TextBoxMode.MultiLine;
txtReport.RichText = true;
txtReport.RichTextMode = SPRichTextMode.Compatible;
txtReport.Rows = 5;
txtReport.Width = new Unit(200);
txtReport.ID = "txtReport";
txtReport.Text.Trim();
this.Controls.Add(txtReport);
reqVal = new RequiredFieldValidator();
reqVal.ID = "reqVal";
reqVal.ControlToValidate = txtReport.ID;
reqVal.SetFocusOnError = true;
reqVal.ErrorMessage = "*Comments field is mandatory";
reqVal.Enabled = true;
this.Controls.Add(reqVal);
Thanks in advance
From what it sounds like you are not re-adding the validator after the first submit, causing the second submit not to validate. But it's hard to tell from the fragment you posted (in what event/method is this being called?).

Categories

Resources