Added control to form has no name - c#

I am completely noob in WPF. I have just opened MS Visual Studio Express 2013 Preview and I am trying to do some "Hello World!" app. I have noticed when I am adding a control to form, for example a TextBox, that control has no name assign.
Is this normal? And how can I change its properties from code?

In WPF, a control needs a name to be provided almost exclusively for the following reasons only:
The developer wants to reference the control in code-behind (frowned upon when using MVVM but sometimes necessary)
When the developer will be passing the XAML to a testing team that is using an automated UI testing tool
When a Binding on another control is using ElementName to reference a property on that control.
If it's not one of those, there's really no need to name your controls. You'll find, once you start using MVVM as your principal design pattern, that you rarely need to know the names of your controls in code. Start getting used to changing properties in XAML, not code.

You should use the Name attribute and provide a unique identifier:
<TextBox Name="UniqueName">
Value
</TextBox>
Assigning the Name value in code could only be achieved if you found a way to actually locate the control without a name in the first place - a little long-winded rather than using the markup, unless, that is, you're adding the controls to the window dynamically anyway, and so already have a direct reference to the element.

Add "Name" attribute
Msdn doc :
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.name.aspx

that control has no name assign. Is it normal? How can I change its properties from code
after creating the control, you can access all it's properties like
Textbox tb = new TextBox();
tb.Name = "textBox1";
tb.Text = "Hello world";
someStackPanel.Children.Add(tb);

Related

Change control name in fast way

I need to put many controls on form during design. Each time I must scroll in Properties in order to find and change control name name. Is it possible somehow change controls name in fast way during design?
Sorry, but currently native Visual Studio does not have the function you want.
You can press F4 on the control to quickly enter the property interface, but you need to find the Name property modification by yourself.
Using code modification is also an option if many controls need to be modified.

how to modify the text property for all object in the form at run windows form c#

I have a form like Invoice and more forms in my application and have a lot of controls in form like text box , label , datagrid , tree view and other
I want make a Method to read the object by object from this form to change text property or caption property - its will be useful to let the user change the control caption by himself when the program complete -
Note : some controls include object like Navbar control or datagrid control have a columns or a nodes and I want to change Column and node Caption by code
best Regards
For example,
Label.Text = "My text";
for datagrid you will need to access the cell of which text you wish to change.
I'd recommend you to go through all the methods and properties of all controls that appears in intellisense. It will guide and teach you alot.
such as write the control name and write Dot after it.
DataGrid.
an Intellisense will appear with methods with their summary. Read them to find out what they do and use them.
DataGrid.Rows[array as it is Enumerable].Cells[array as it is Enumerable].Value <- This is a hint for you to find out yourself. It will be slow but it will make you independent. Good luck :)
You can achieve to change the text for the treenode at any level with the guide i provided above. Just go through the Intellisense and you'll start to understand what you need to do.

Bind string to control

I wonder, if there is any way to bind a string to a Label or any other control.
I thought about this kind of pseudo code:
//label class value
label1.Text => LocalizedStrings.someString;
This should also edit the text shown at label1 when someString's value changes. Is there any way to do this either by something similar to the above pseudo code, using the Designer or editing the Designer.cs file?
PS: I'm using Visual Studio 15 RC, C# 6.0 and .NET 4.6.
I think there is no simple way to do that in WinForms (but if there is, I would like to know).
But you can do the contrary.
For example, you can adapt your class "LocalizedString" or extend it to get a reference to the Label. Than you add code to it so that when its content changes, it changes the content in the label.
Hum...
But also, you maybe can use this: https://msdn.microsoft.com/en-us/library/system.windows.forms.binding%28v=vs.110%29.aspx
It enables the binding of a property on a control and a property on an object. It seems exactly what you want...
Here there is an example: Bind a label to a "variable"
Try this:
Binding binding = new Binding( "Text", Properties.Settings.Default, "TranSvcAddr" );
txtTranSvcAddr.DataBindings.Add( binding );
The TextBox is bound to a Property and the value will change whenever the underlying value is changed. Works the same for a Label.

Is it possible to obtain and modify standard system context menu for textbox?

I have a simple winform app with textbox.
I didnt assigned any context menu to text field and "it uses" standard one. I desire to add some new items to textbox's standard context menu. But I cant obtain it for modification, if be more clearly I didnt know how to obtain HMENU native object.
ContextMenu property of my textbox equals null so I suppose that menu appers within textbox defaul windProc routing. It there a way to get the default context menu or get a copy of it?
Thanks in advance!
Depending on any plug-ins you're using, it may be easier, and more understandable, to create a custom control that derives from TextBox and defines a "default" contextual menu. Then, change all TextBoxes in your solution to be your custom control instead. Failing that, if you have a common window ancestor, set up some initialization code that assigns a given ContextMenu to all controls in the form's hierarchy of type TextBox. Instead of plugging in to low-level Windows hooks, you're using what the framework gives you to accomplish the same end, and your posterity doesn't have to know how Windows handles contextual menus at a message-passing level in order to alter this behavior.

Access sub-controls of custom control from code

I created a custom control in Expression Blend which consists of multiple TextBlocks placed in a Grid. Now I added this custom control to my phone page in Visual Studio and want to access and change the text of these TextBlocks from C# code.
How do I access these sub-controls in code?
I thought I could do something like this:
MyCustomControl.TextBlock1.Text = "New Text";
But it's not that easy. So how do I do it?
The property MyCustomControl.TextBlock1 exists but is internal, not public. You can use MyCustomControl.FindName("TextBlock1") as TextBlock to locate the resources by name instead.
Does GetTemplateChild(string name); work ? You should be able to use it, to access the elements of your control's template
Try below code both should work for your requirement:
Control subControl1 = (Control)MyCustomControl.Controls[0];
or
TextBox subControl1 = (TextBox)MyCustomControl.Controls[0];
Using any code you are able to access Text Property.
Always keep remember the hierarchy in your custom control and then try to access all control level-by-level.
If still you are facing any issue feel free to ask.

Categories

Resources