String to textbox casting - c#

string data = "test";
TextBox tb = (TextBox)data; // i want something like that in order to
tb.Backcolor = color.black; // do this line

Use TextBox.Text = "bla" instead of cast

Providing that you call the code with a method of a Form, you want something like that:
String data = "test";
// creation of a new TextBox with Text assigned to data...
TextBox tb = new TextBox() {
Parent = this, // <- text box is on the Form...
Location = new Point(10, 10),
Size = new Size(100, 20),
Text = data, // <- TEXT of the TextBox is data
BackColor = Color.Black,
ForeColor = Color.White,
};

String data = "test";
TextBox tb = new TextBox();
tb.text = data;
You can assign string directly to the textbox's using text attribute.
Hope this helps.

Related

How do I create a UI element from code that appears in Unity?

I'm trying to create text dynamically so I would like to create text in code. I tried doing it like this:
private void DrawIndexNumber()
{
Text txt = new GameObject().AddComponent<Text>();
txt.name = "Ind";
txt.transform.position = _posOfOrigin;
txt.material = materialNum;
txt.font = Font.CreateDynamicFontFromOSFont("arial", 16);
txt.text = "TEST";
}
The above code creates the text object but does not display it.
All UI elements must be inside a Canvas GameObject to be visible. So what you can do is add the Canvas GameObject in edit mode. Or alternatively in runtime:
private void DrawIndexNumber()
{
Canvas canvas = new GameObject().AddComponent<Canvas>();
Text txt = new GameObject().AddComponent<Text>();
txt.name = "Ind";
txt.transform.position = _posOfOrigin;
txt.transform.parent = canvas.transform;
txt.material = materialNum;
txt.font = Font.CreateDynamicFontFromOSFont("arial", 16);
txt.text = "TEST";
}

WPF textbox resizable text

I am trying to create resizable textboxes (so when the window is resized the textbox adjusts itself) programatically but the text inside the textbox is always very small in comparison to the textbox:
TextBox textBox = new TextBox();
textBox.Name = name;
textBox.Text = text;
textBox.SetValue(Grid.ColumnProperty, column);
textBox.SetValue(Grid.RowProperty, row);
textBox.SetValue(Grid.ColumnSpanProperty, columnspan);
textBox.SetValue(Grid.RowSpanProperty, rowspan);
So i added a binding to keep the text at the same size as the textbox:
Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
b.Path = new PropertyPath(TextBox.ActualHeightProperty);
textBox.SetBinding(TextBox.FontSizeProperty, b);
but when i do this the text becomes way too big for the textbox.
What am i doing wrong/missing here?
You are binding the fontsize which is pixels to actual height which is in device independent units so its apples to oranges.
Is there any reason to not use a viewbox?
Something like (I'm doing this freehand so beware..no ide may have typos)
TextBox textBox = new TextBox();
Viewbox vb = new Viewbox();
vb.Child = textbox;
vb.Stretch = Uniform;
textBox.Name = name;
textBox.Text = text;
vb.SetValue(Grid.ColumnProperty, column);
vb.SetValue(Grid.RowProperty, row);
vb.SetValue(Grid.ColumnSpanProperty, columnspan);
vb.SetValue(Grid.RowSpanProperty, rowspan);

Programmatically Added Textboxes not Appearing

I'm using WinForms in VS15 with C#.
I'm dynamically adding TextBoxs and Labels to my Form based upon a user selected value in a ComboBox (essentially this looks up a value in a data collection which tells my UI what controls it needs).
When I attempt to generate the controls, the Labels appear and layout just fine, however, the TextBoxs are remarkable in there absence.
I've tried fidelling with the MaximumSize and MinimumSize properties to see if they could be messing with something but it doesn't seem to be making any difference.
The code I use for doing this is below (I know the use of the List<Pair<Label,TextBox>> is pretty unecessary but I find it helps readability):
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
List<Pair<Label, TextBox>> controlPairs = new List<Pair<Label, TextBox>>();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size initialOffset = new Size(0, 30);
Size horizontalOffset = new Size(40, 0);
Size tBoxSize = new Size(40,20);
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += initialOffset)
};
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint + horizontalOffset
};
controlPairs.Add(new Pair<Label, TextBox>(label, textBox));
}
foreach (Pair<Label, TextBox> pair in controlPairs)
{
panel.Controls.Add(pair.First);
panel.Controls.Add(pair.Second);
}
}
I don't believe that my use of Point + Size is the issue as the Point class overrides the + operator like so:
Unfortunately for me the issue appears to be simply that the dX was not a big enough value to prevent the text boxes from being hidden under the labels, I forgot that labels don't have transparent backgrounds.
While I was at it: I've removed the redundant List<Pair<<>>; added support for dynamically adjusting TextBox location based on Label size; and split it out into two separate loops, so my code now looks as below and works just fine:
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size verticalOffset = new Size(0, 30);
Size tBoxSize = new Size(200,20);
int maxLabelLength = 0;
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += verticalOffset),
AutoSize = true
};
panel.Controls.Add(label);
if (label.Size.Width > maxLabelLength)
{
maxLabelLength = label.Size.Width;
}
}
Size horizontalOffset = new Size(maxLabelLength + 30, 0);
labelPoint = panel.Location + new Size(20, 20) + horizontalOffset;
foreach (string parameter in formParameters)
{
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint += verticalOffset
};
panel.Controls.Add(textBox);
}
}
Thanks everyone who helped!

Docking 2 rich text boxes

I am trying to make a text editor and I have 2 rich text boxes. I am uses the 1st rich text box as a number like and setting its Enabled property to false. Then the second text box is going next to it
I have currently set the dock of the first text box to left and the second one to fill. But the 2nd one keeps taking up the whole tabpage? And going slightly more left towards the number line and its hidden under there a little bit. Here is my create new document void I have... T2 is the number line and T is the default text box they will type into.
TabPage t = new TabPage("new " + getNumber());
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = false;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
The problem is that you define your fill-docked text box at the end of the control list... winforms likes the fill-docked elements first in the list.
Easily solved:
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
// Add this line
T.BringToFront();
Or you could also do T2.SendToBack(); after T is added to the controls collection.
Or you could simply create (and add to t.Controls), the fill-docked textbox first, and the left-docked textbox second.
Either way works
By the way, try to name your variables correctly. t, T, T2 are just not good names
Here you go:
t.Controls.Add(T2);
t.Controls.Add(T);
t.Controls.SetChildIndex(T, 1);
t.Controls.SetChildIndex(T2, 0);
t.PerformLayout(); // needed after SetChildIndex!
T2.Dock = DockStyle.Left;
T.Dock = DockStyle.Left;
If you want your boxes to grow with the TabPage here is one way to do it:
private void t_Resize(object sender, EventArgs e)
{
// assuming you want the two RTBs to fill the TabPage
// if you want something else, best add an anchored container panel
// and use its resize event instead
T2.Width = t.Width / 4;
T.Width = t.Width / 4;
}
And yes, t, T and T2 are really bad names!
There's a few things going on:
Fill does just that; it Fills the entirety of the PARENT control. Whether anything else is there or not.
Your other text box will be hidden until it is set to Enabled.
What you Might? be looking for is a way to have the text box size itself based on the size of the tab page being created:
{
TabPage t = new TabPage("new " + 1);
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = true ;
RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T2.Dock = DockStyle.Right;
var AdjustedSize = T2.Size;
AdjustedSize.Width = t.Size.Width * 2 / 3;
T2.Size = AdjustedSize;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();
}

display two textbox in customMessageBox?

Well, I want to display two text boxes next to next in customMessageBox. So i have coded for two Text Boxes. like below. I named soora and ayath for it. But in customMessageBox, i cannot call two text boxes in the same time. It shows error. How to display two text boxes next to next in customMessageBox. I only the error and it is form Content = soora + ayath
My C# CODE;
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = soora + ayath,
RightButtonContent = "Go !",
};
use a container control to hold both textboxes
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
StackPanel container = new StackPanel{
Orientation = System.Windows.Controls.Orientation.Horizontal
};
container.Children.Add(soora);
container.Children.Add(ayath);
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = container,
RightButtonContent = "Go !",
};
if you want to display text then
Content = soora.Text + ayath.Text,

Categories