I am working with Visual Studio 2005 (C#) and in one of my windows forms I have a couple of textboxes. I have placed the textboxes inside instances of 'System.Windows.Forms.GroupBox' because I want to use the GroupBox member 'Text' to tell what the textboxes are for (it shows up as a label near the top left corner of the fine line that encircles the GroupBox).
My problem is that suddenly one of these labels just disappeared. It is present when I work with the form in the designer but when I run the program and the form appears the label is gone.
Thank you for any information on this issue!
Check to be sure you're not setting the .Text property of the GroupBox to an empty string.
If you do not need to access the groupbox in your code you can always set the property GenerateMember on the groupbox to false and what you see in the editor "should" always be what you get when you run the app.
This is useful with any controls that you do not wish to modify during runtime thus keeping your memory allocation down as well as mistakenly modifying one of it's properties as well.
Related
I have an MdiContainer with 4 children inside.
Each individual child has a number of label which are styled as squares.
There are 3 rows featuring 3 squares per Child (so 3x3).
The labels are dragable from one child to another.
Each MdiChild has a StackPanel which is used to contain the rows.
When I perform a drag or a drop, i'd like to know which MdiChild were the ones involved in the drag(the origin) and the drop(the destination child).
I'm just having trouble figuring out the code to do so.
I've tried using visualtreehelpers and getparent but they haven't done the trick. Most pages I could find through googling deal with forms whereas i'm working in WPF.
Edit 1:
When testing to see if I was getting the answer I wanted, I used this code
string TheActiveMdi = Container.ActiveMdiChild.Name;
where "Container" is the name given to my MdiContainer.
For some reason the value put into the variable is just whichever is MdiChild is the last one written in my Xaml code, so it's not returning an accurate value. I have that code placed in my Drop event method.
Edit 2:
Within the drop event method, this code will allow me to access the label which was dragged:
Label sourceLbl = e.Data.GetData("System.Windows.Controls.Label") as Label;
If I use this code, I can access it's parent, which is a Stackpanel:
string LabelsParent = myObject.Parent.GetType().ToString();
MessageBox.Show(LabelsParent);
This tells me that it's a Stackpanel.
However what confuses me is that if I try to print it's name in a Messagebox:
MessageBox.Show(LabelsParent.Name);
then it simply returns "Stackpanel" whereas if I do the same with sourceLbl:
MessageBox.Show(sourceLbl.Name);
then it will in fact print it's name, which in this case is "testSquare"
Can anybody explain this or help me with my initial problem?
I am working on a XAML page in VS2012.
The page has a couple of empty labels (which programatically get filled in later). Because the labels are empty, they do not appear on the page unless moused over.
Is there any way to have VS show me the outline of these labels only in the design pane (so in release it would be invisible).
First and foremost, the comments are correct - you should fill the controls in with dummy values and then clear them on startup (either automatically with databinding, or otherwise).
Now, to actually answer the question, you can check if Visual Studio is in "Design Mode", and then programmatically set the border of a control... or better, set the text to something easily noticeable in the UI:
// Check for design mode.
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue))
{
myTextBlock.Text = "*** DESIGN ***";
}
This code should be run in the constructor of your WPF window/control class (or something invoked from the constructor) so that the designer executes it.
The code to check for design time was taken from this post.
Context :
I created an User Control. For some reason, I want to use this control in different size. To keep the initial "Template" of my User Control when re-sizing, I use the property Anchor on my different element inside the control.
So when I create my control at design time, it is possible to me to hand re-size the control and keep the original "Template" of it.
When the control is created, it look like this :
And after re-size :
As you can see, the property Anchor work well.
The label and the picture stay in the middle.
The "?" stay to the left corner.
The problem :
The problem I have is, when the control is reloaded, created with a different size as the initial one, all the elements inside return to their initial position :
I don't know if this is the better way to do what I try to achieve. Keep in mind that I add and re-sizing the control's during the design time.
Thank you.
EDIT :
I think my problem is caused by the designer. Ex : I add my control in the designer, I re-size it, I run the solution. All is working good. But when I go to the code of the page, and then, return to the designer, the element inside the control returned to their initial position.
EDIT 2 :
Ok I have found a solution, I simply moved all the element of the User control inside a Panel. For some reason that I can't explain, it work perfectly. The control's stay at the same location.
The solution is ta add a Panel to the User Control and dock it to "Fill", then place the element inside of this panel. For some reason that I can't explain, the designer keep the location of the re-sized control's elements.
The anchoring, docking and auto-sizing of a UserControl seem to be terribly confusing. I found UserControl does not auto resize with the Form which suggests that you set the AutoSize property to False, which I did, and it still didn't correct my problem. But when I tried your solution, I also noticed there are two copies of the AutoSize property! I had set the AutoSize in the UserControl designer to False, but the Form designer where the UserControl instance was added also had an AutoSize on the instance, and that one had a different value (it was still True). When I set that to False also, then everything worked (with the panel in place). Then I removed the panel you suggested, and everything still worked. So I guess the trick is to make sure you check all the properties of the UserControl in the UserControl designer and in the form designer where the control is used. Then you shouldn't need a panel.
I've had similar problem in VS2015 project, and unfortunately - none of your answers helped. Clean and working solution was found here, in Jignesh Thakker answer.
For quicker navigation here it is how it was done in my project (c++/cli, not c#, but idea is the same):
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
/* some code */
myUserControl = gcnew MyUserControl();
myUserControl->Dock = DockStyle::Fill;
tabPage1->Dock = DockStyle::Fill;
tabPage1->Controls->Add(myUserControl);
/* some code */
}
Set the Localizable property of the parent form at VS designer to false. This solves the problem at design time. (Save, close and reopen the form after switching the property)
If you need a localized application switch the Localizable property to true after finish up working at the layout and don't care about the wired representation in the VS designer. At run time it's shown correctly.
Tested in VS2013
I'm working a WPF application which has a number of groupboxes, each containing 3-4 controls a piece. Each of these groupboxes contains a reset button.
Instead of explicitly writing a line for each item the groupbox reset buton should reset is there a way to have the reset button only reset all items for the groupbox it's contained upon?
When researching this issue I found the following post which seems relevant but I'm unsure how to extend Nathan's answer to do this: What is the best way to clear all controls on a form C#?
You can use this question as an source of methods you need.
First of all you need to find groupbox that contains button. To do that use sender argument form click event it is reference to button that was clicked and call FindVisualParent method from one of answers in that question. This will give you groupbox.
Then Find all ui elements by FindVisualChildren<DependencyObject> method. This will give you super set of controls that are to be cleared.
Then it is just a loop over this as in question that you referenced. Note that that question was intended for winforms not wpf so you will have to revise the controldefaults dictionary which handles grouping controls ~(Panel GroupBox) which is not needed here (and would not work in wpf).
You might add a TAG to each control telling which property is beeing edited in the control, and then on reset press, you travel up the button to find containing GroupBox, then down to find all controls contained in this GroupBox, and then you find, with the TAGs, the name of the properties that needs reset. You then reset the properties using Reflexion.
Maybe an issue is that the default value might not be obvious. But you could store the default values for all your bindings in a static object, and copy the value of the property for this reset object into the current status object.
Rq : you could also use reflexion to get the bindings for all control of current GroupBox, and then get the bounded properties from the binding, so no need for a TAG. BUT if a control has several bindings, you won't know which one to reset. In my case, controls within GroupBoxes have also a color binding to indicate wether they have default value (green) or non-default (red), hence the need for a TAG. (BUT the color indicator are read-only so reflexion could, in fact, also be used with a little more use of Reflexion... was just too lazy to change a working logic when i saw that :-) )
I don't know about your bindings so obviously i cannot tell what's best, i hope this thoughs can help.
I need to create a list in c# with click-able and customizable child elements.
Check attached image (red square) to get a feeling of what I would like to end up with. I tested on a control called "list view". I can add an icon and a label, it is not enough, I need more customization.
So question: Which c# control should I use? Maybe share a link if you know something useful.
Note: I don't really know or enjoy C# (I am Java guy), as IDE I use Visual Studio C#, express edition and I would love to minimize the coding part as much as possible
First, create a user control (says ContactRow.cs)
Draw what ever UI that you need on it. (Image, text, etc)
Secondly, Create a container form (says ContactList.cs)
Add a TableLayoutPanel (Or FlowLayoutPanel Or equivalent) into container form.
Set AutoScroll property = true so that it scrolls.
In run time, you can new an instance of your ContactRow.cs and set all the values.
Then add it into the TableLayoutPanel using the TableLayoutPanel.Controls.Add(contactrow)