Can't find this.Controls - what am I missing? - c#

Sorry for a potentially dumb question, I am still new at this. I really appreciate your help.
Referring to Get a Windows Forms control by name in C#
But I don't have a "this.Controls" available. Is there something I am missing here?
In other words, when I type "this." and visual studio populates a list of option, there is no "Controls" option.

In WPF, you should try this.FindName(string name)
Button b = (Button)this.FindName("button1");

If you are looking to iterate through the controls for whatever reason, in your Window class, you can iterate through the LayoutRoot's children (e.g.)
foreach (object o in this.LayoutRoot.Children)
{
MessageBox.Show(o.GetType().Name);
}
Keep in mind that the children can also contain children, so you'll need to delve into each of those as needed.

The link you gave was for Winforms, you are looking for a WPF way to do it which is different.

I have to add whats LayoutRoot: because I am new in WPF I did not know whats that
After general research I found them: it's your Grid
if you write grid visual studio did not find them what you should do?
go to your window
next go to the Grid Start tag : <grid>
next add : x:Name="your desired name"
like this : <Grid x:Name="FRM">
now go back to code yourwindow.xaml.cs
not you see it works foreach (object o in this.FRM.Children)
I hope it was useful for a novice like me

Yes, this is very important issue when we want to make a change in similar controls in a particular Grid. So after looking up for a while I found out one solution which looks better.
I am performing a Null or Empty check on all the textboxes in my Grid when someone randomly presses the SUBMIT Button
Here is the C# Codeblock
foreach (TextBox tx in Grid1.Children.OfType<TextBox>())
{
if (string.IsNullOrEmpty(tx.Text))
{
MessageBox.Show("No empty boxes please!");
return;
}
}

Related

Copy Controls from a list to another without the same pointer reference

Today I would like to create a copy/paste function for a software I develop.
I have a Panel that contains Controls and I want to copy/paste.
I have a selection tool that permit the user to select different Controls and add this Controls to a List. I have called it "SelectionActuelle".
Then, when the user clicks on "Copy". I would like to add every controls that SelectionActuelle contains into a new List called "PressePapier".
But when I do, it copies the same pointer reference, and I dont want.
I throught that add a Control to another List should copy it and create a new instance but it doesn't.
I tried this example HERE but it doesn't work.
What I have now is only 6 lines (it doesn't work !!) to try to make a copy of the List :
private void bt_copier_Click(object sender, EventArgs e)
{
PressePapier.Clear();
foreach(Control ctr in SelectionActuelle)
{
PressePapier.Add(ctr);
}
bt_coller.Enabled = true;
}
How can I simply copy Control to make my Copy/Paste tool ? So is it possible (I think yes but we never know) ?
Have a good day,
Julien
Thanks to you, I found out that my structure was not great and I changed it to a simpler structure and more reutilisable.
I will apply this :
You should foreach the selected controls and check it's type, just generate a new instance of that control with it's properties.
But Dr. Coconut, my problem was that .NET does not appreciate some expressions in the code even if I have adapted it... I don't know why. Question of framework version ?

how to have an event when there is a change in list (collection editor)

this is the very first time i ask question at here. If i didn't follow the rules please let me know and please let me know if you unable to understand my english, i will try my best to let you know my problem.
public List<TeachPosUC> tp = new List<TeachPosUC>();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<TeachPosUC> _TeachPos
{
get { return tp;}
}
Basically the is a customer user control. What i want to do is just add all item in the tp to a table layout. My code like below:
tblLayout.Controls.Add(new TeachPosUC());
the main problem i faced is, how to detect we clicked the collection editor's "OK" button. By knowing the "OK" button was clicked, i can add the new User Control to the table layout.
Can anybody give me a hand on this, or is there any better way? I search through online, but can't get what I want. May be i searched the wrong keyword, if this question is duplicated, please kindly provide me a link direct to the answer. Thanks a lot!

How to hide textboxes, labels and buttons C# WPF

I would like to hide several textboxes, a label and a button as soon as a button is clicked... however, for some reason, my code doesn't seem to cause this effect. Nothing appears to happen. I'm using WPF.
Here is my code:
private void doSomething_Click(object sender, RoutedEventArgs e)
{
Name.Visibility = Visibility.Hidden;
}
This code doesn't seem to work.. any ideas?
I believe Visibility.Collapsed is what you need and not Visibility.Hidden.
EDIT: Did you try follow up this code with UpdateLayout() method of parent element/component?
Your code seems to work fine, the "Signing in..." label appears after everything else disappear. I suggest you to just copy all your code from the .xaml.cs file and the .xaml file into a new project, but make sure you don't copy the first line"<Window x:Class="..." because it could generate an error if the class name isn't the same in the new project.
For the xaml code I suggest you not think the same as you design windows forms applications. WPF has the layout system, which re-orientates or re-sizes its elements when re-sizing the window. So you should not specify exact numbers in the margin property as if they where coordinates. Create a grid, create rows or columns for each element and then just set the horizontal or vertical alignment or margins. Think different than the old windows forms way.
I've run your code... and it's working great for me. I've not changed anything (except the variable names) so I guess it's a bug from VS.
As said nikolamm94 try to add this.UpdateLayout(); at the end of connect_Click it might help. I tried and it is still working fine. Or maybe create a new VS projet, it already worked for me a few times.
Sorry my answer is not the most helpful, I wanted to put a comment instead but I don't have enough reputation :/
Please refer: https://msdn.microsoft.com/en-us/library/ms748821(v=vs.85).aspx
Set to Visible: tb1.Visibility = System.Windows.Visibility.Visible;
Set to Hide: tb1.Visibility = System.Windows.Visibility.Hidden;
You can hide a textbox by going to properties->appearance->visibility, then setting it to "hidden"

Interactive Design-Time User Control

I apologise if the title was confusing, it took me nearly 5 minutes to finally think of a title for this one...
Okay, you know how in Visual Studio Express when you add a TabControl to the Form, and you can click on the right-arrow on the top right of the TabControl and it will add a new TabPage, or remove one?
Well, I'm creating a User Control where I need people to be able to switch between Panels (my user control is made up of several Panels). I know this is possible as I've used a Ribbon Control in the past and you could add new buttons etc in the Designer View.
Can somebody please provide any suggestions/advice on how I might go about acheiving this?
Thank you
If I understand your question correctly, you're talking about smart tags.
The process is a little bit involved, so I'm not going to try to post a complete sample. Instead, I'll refer you to this tutorial on the subject. To make a long story short, you have to create a custom designer, and register one or more custom actions. You can use this to create a combo box listing the available panels and switch between them when the selected item is changed.
(Note - the term "smart tags" has two distinct meanings in Visual Studio - I'm specifically talking about the visual designer smart tags, not smart tags in the code editor).
When you make a control that is inherited from Control, you have to make use of a couple of properties such as IsDesignMode, you can then construct event handlers especially for within Design Mode:
if (IsDesignMode){
// Handle the interactivity in Design mode, such as changing a property on the
// Properties toolbox
}
Suppose the control has an event such as MouseClick, you can do this:
private void control_MouseClick(object sender, MouseEventArgs e){
if (IsDesignMode){
// Do something here depending on the Click event within the Designer
}else{
// This is at run-time...
}
}
Another I can think of is 'ShouldSerialize' followed by a publicly accessible property in order to persist the property to the designer-generated code, suppose for example a Control has a boolean property Foo
public bool Foo{
get{ return this._foo; }
set{ if (this._foo != value){
this._foo = value;
}
}
}
public bool ShouldSerializeFoo(){
return true; // The property will be persisted in the designer-generated code
// Check in Form.Designer.cs...
}
If ShouldSerializeFoo returned false, no property is persisted, its the opposite when true, it will be buried within the Form.Designer.cs code...
Hope this helps,
Best regards,
Tom.

Generic method to get a control name when moused over?

I am writing an application that I am working on a skinning feature to it. What I want to do is allow a user to create an XML document that my code will parse through and set properties of the controls on a form. This is the easy part. Where I am stuck is giving the user a way to find out the control names/types with minimal coding/documentation effort.
My idea was to have a tool tip that when they moused over a control, it got the name of the control and type to show. Anyone know of a way to do this? I was thinking something like how Spy++ can find control, but I want to get the .NET properties also.
If someone has another idea I'm open ears.
Thanks Much.
Figured it out. The issue was because the mouse location wasn't relative to the client location. Thus the code below will resolve this issue. I put it in a polling thread that I already had going, but it should work with a timer or other event. Didn't work in MouseMove for some reason though. Thanks everyone for the help.
Point p = this.PointToClient(MousePosition);
Control x = this.GetChildAtPoint(p);
if (x != null)
{
MessageBox.Show(x.GetType().ToString() + " - " + x.Name);
}
You'll need to set up some ToolTip objects (one for each field), looping through all of the controls in your form to obtain the text for each tooltip.
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx
Are you willing to register for each controls mouse over event? If so, the sender would be the type, etc...maybe I'm missing something here? Furthermore you can get the control out of the visual tree in WPF/SL, why not port over to WPF?
You could recursively install a OnMouseOver eventhandler. Then if an OnMouseOver event occures the control is in the sender argument of the event handler.
Why wouldn't this work? I'm putting it in the base form, don't really see what it wouldn't work next to GetChildAtPoint being buggy.
protected override void OnMouseMove(MouseEventArgs e)
{
Control c = this.GetChildAtPoint(e.Location);
if (c != null)
{
MessageBox.Show(String.Format("Your control name is: {0} and type is {1}.", c.Name, c.GetType().ToString()));
}
base.OnMouseMove(e);
}

Categories