1

I have developed an Text Box with XAML code, but now i need to convert it in to C# Code; but I'm not familiar with it. So, Hereby I am sharing my code, and i request you all to convert it into C# code;

My XAML Code;

<TextBox Name="txt1" Height="72" Width="130" Margin="193,3,0,0" InputScope="Number" MaxLength="3"/>
<TextBox Name="txt2" Height="72" Width="130" Margin="193,68,0,0" InputScope="Number" MaxLength="3"/>

I created TextBox by c# code TextBox txt1 = new TextBox(). But i dunno how to create textbox with above XAML TextBox Properties.

3 Answers 3

1

you can do like this..

 TextBlock txt1 = new TextBlock();
        txt1.Height = 72;
        txt1.Width = 72;
        txt1.Margin = new Thickness(193, 3, 0, 0);
        InputScope inputScope = new InputScope();
        InputScopeName inputScopeName = new InputScopeName();
        inputScopeName.NameValue = InputScopeNameValue.Number;

        inputScope.Names.Add(inputScopeName);
        txt1.InputScope = inputScope;
        SomeGrid.Children.Add(txt1); // somegrid is a parent control in which you wanted to add your textblock
Sign up to request clarification or add additional context in comments.

Comments

1

You can simply drag and drop from toolbox. However, You can do it like this via code.

System.Windows.Forms.TextBox txt1 = = new System.Windows.Forms.TextBox();
txt1.Location = new System.Drawing.Point(78, 183);
this.txt1.MaxLength = 3;
this.txt1.Multiline = true;
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(130, 72);
this.txt1.TabIndex = 2;
this.Controls.Add(this.txt1);

Now InputScope = Number

There are couple of ways here. You can use MaskedTextBox or you can restrict user by validating his input. First one is the preferred.

System.Windows.Forms.MaskedTextBox txt1= new System.Windows.Forms.MaskedTextBox();
this.txt1.Location = new System.Drawing.Point(149, 141);
this.txt1.Mask = "000";
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(100, 20);
this.txt1.TabIndex = 3;
this.txt1.ValidatingType = typeof(int);
this.Controls.Add(this.txt1);

1 Comment

You are right But InputScope="Number" is missing. I need that also. User should enter only number not words
0

You can create the TextBox the same way you have mentioned and then you can set the properties. Something like

TextBox txt1 = new TextBox();
tx1.Name = "tx1";

You should be able to set most of the properties like this. For few complex properties you may have to create an instance of some type and then set it.

2 Comments

You are right But InputScope="Number" is missing. I need that also. User should enter only number not words
Like I said, for few properties you have to create instance of a type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.