I had inherited the ComboBox winforms control.
In the first trial, I added some properties, and the designable ones showed Ok in the Property grid and all went OK.
Today, I added some others, and from that point, it makes the designer bombs.
Initially, the (presumably) offending property was "new DisplayMember" which referenced in the set and get methods the base.DisplayMember. Suspecting that was the mistake, I changed it to "public string DisplayProperty", to avoid name clash, but the error continued.
Ultimately, I also set a private variable displayProperty, and set the base.DisplayMember in the OnCreateControl event.
Nothing works.
Any help will be appreciated.
If it´s required, I could put the code, but it´s very big.
TIA
EDIT: looking at the Application events, they show the VS failures, but don´t tells anything regarding the error.
I had a property that looked like
bool autoComplete = true;
[Category("Autocomplete")]
[Description("This is the only property of this group to set. All others will be set accordingly.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool AutoComplete
{
get { return AutoComplete; }
set { autoComplete = value; }
}
See the capital A in the get procedure... once in the designer, recursive calls ended in a stack overflow. But none of this were explicit in the event log.
Hours chasing a phantom.
Related
I am working with some custom controls and I am having some major issues with them. All but one of the controls is not able to be drawn in the designer. When I open the designer for a form which had the custom control manually added to it the designer shows the error "The property 'BackColor" could not be created from it's default value. Error message: SystemColors.ButonFace is not a valid value for Int32. I do not set the back ground to SystemColors.ButonFace so this is very confusing to me...
The stack trace has the return line from this method as the last position from within my code before going into System code, this is in the _ControlName.Designer.cs file:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("Red")]
public global::System.Drawing.Color BackColor {
get {
return ((global::System.Drawing.Color)(this["BackColor"]));
}
set {
this["BackColor"] = value;
}
}
The one control that works didn't work before and now it suddenly does but it has exactly the same thing for this function. I can't seem to find what changed that makes it work now. If I change "Red" or "BackColor" nothing seems to change for any of them. I also tried restoring my backup and the one that started working still worked... I had someone else load up my backup and it did not work for them. Is this information stored somewhere outside of the project or something? I can't find anywhere setting anything to SystemColors.ButtonFace.
Thanks for any suggestions.
In my code behind I have two method to enable or disable a group of controls depending upon the value of a field on a form. One of these methods works as expected, and the other doesn't ... quite. The one that works depends upon the value of a Checkbox, while the other depends upon the value of a drop-down list (I believe that this difference is irrelevant - I mention it only for completeness). I've simplified these methods for readability, but the only significant difference from the production system is that they set a number of controls, not just one.
private void SetControlsFromDropDown(int statusID)
{
// This method doesn't work
bool enable = (statusID == (int)ReqStatus.CompletedOK)
this.myTextBox.Enabled = enable;
}
private void SetControlsFromCheckBox(bool enable)
{
// This method works
cboMyDropDown.Enabled = enable;
}
Where the first method fails is that it sets the controls correctly when the form is loaded. However, when the drop-down list changes, the method is called and the value of the bool variable "enable" is correctly set, and the code runs through as expected (and a watch on the Enabled property of the controls that are being set shows that they are toggled as expected) - BUT BUT BUT the controls remain firmly unchanged in the interface. So, if they were initially set to Enabled = false they remain disabled even though the method might have set them to Enabled = true.
What is causing me conniptions is that if I put setting of this.myTextoxBox into the second method, it toggles correctly.
I get the impression I haven't explained myself very clearly. In essence, two more-or-less identical methods, called from similar events, operating in arguably indistinguishable ways, behave differently in real-time. One will toggle the Enabled property of a group of controls ad lib., while the other will toggle it once, never to be toggled again.
Any thoughts gratefully received.
Edward
The problem was caused by a failure of the brain. I was getting an incorrect value from the drop-down list. Apologies for any time wasted.
I'm not really sure about this, but try to remove the 'this'
Just put the following:
myTextBox.Enabled = enable;
I have a very annoying problem I'm trying to solve for couple of weeks. I have a WinForms C# project where I developed my custom control (ListView + ToolStrip with ToolStripButtons). This control is used in different forms inside solution - but in other projects. For different forms I need to make certain buttons visible or hidden, so I have added to my control corresponding properties like
public Boolean DeleteButtonVisible
{
get
{
return tsbDelete.Visible;
}
set
{
tsbDelete.Visible = value;
}
}
Some buttons are visible by default, some are hidden. In designer when editing a form with my control I'm able to change those properties, buttons on control become visible or hidden as they should. But every time I'm changing anything in my control source file in all forms those properties are reset to default values regardless of what I have set in designer and I have to restore those values manually. Well, I'm using a source control so this is not that hard, but performing "Undo" on a couple dozen of files every time I change a bit in another file is a damn disaster.
I have tried to use [DesignerSerializationVisibility] attribute to fix this issue. If I used it with value "Hidden" it didn't do any good at all - values were just not saved. "Content" made buttons randomly disappear even if by default they were visible. "Visible" lead to no effect, as this is default value...
I don't want to set every button visibility for every form in my code - this is just not the way it should be done.
Does anyone know something about this?
Yes, the Control.Visible property is special. The getter does not return the last assigned value, it only returns true when the control is actually visible. That can have side-effects, you've found one. In this case probably induced when the control switches out of design mode. To do this correctly, you must store the assigned state in a backing variable. Like this:
private bool tsbDeleteVisible;
public bool DeleteButtonVisible {
get { return tsbDeleteVisible; }
set { tsbDelete.Visible = tsbDeleteVisible = value; }
}
Be sure to initialize the default value of the backing variable to the default value of tsbDelete.Visible. Use the constructor to be sure.
Simplifying
I have a text box and a button
The button just create an messagebox with the text from the textbox.
But i change the value of the textbox, the new value apears (Ex: Type 123) but the message box does not show the value.
If i try to use the value in the programming (get the value by textbox1.text) the variable has nothing ( textbox1.text = "") but i can still see what i typed in the form.
Anyone has any clue?
Your button's click event handler should look something like this
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox.Text);
}
I suspect you already have code similar to this and that at some point the textbox is cleared or otherwise set to String.Emppty but without seeing actual code it is difficult to help you
When/where did you check the value of textBox1.Text? If you're checking it in the constructor, Form1_Load, or anything else that occurs before you'll have typed text, you will get an empty value.
To properly check the value of textBox1.Text, you should set what's called a breakpoint on the line that calls MessageBox.Show(textBox1.Text). To do this, click in the grey area of the source editor (it's on the far left) on the line containing MessageBox.Show(..). A red circle will appear and your code should be highlighted. When you run your application and click on your button, your application should pause and Visual Studio will highlight that line and from here you can hover over "textBox1.Text" in the MessageBox.Show() line and it should show you the current value.
If your application is as simple as a form, a textbox, and your button1_Clicked event handling code, this should work no problem. If it is not this simple, then you need to look for anything that sets the value of the textBox in your code and make sure it isn't passing any blank values by using breakpoints.
To solve this properly, though, we really need more information.
Thanks Eric and Crippledsmurf. As both of you said, its hard to help without the code.
The problem I found is that when calling the form, I send some objects by reference, so I can track them down and I found that when (don't ask me why it happens that way, I'm still working on it) the construtor is called he make an new component, so the component in the interface no longer represents the one pointed by the variable "textbox1" (Yes Crash893, I haven't mispelled the name).
I found that I was making some mess with the references, and probably that was causing the problem. I fixed the problem by changing the actions performed by references for delegates and events, but I couldn't track down the exactly source of the problem.
Thanks, again, everyone for the insights.
I am trying to disable a number of text boxes intended for displaying data (not edit) in one of my UserControls. However, for some reason I can not get the textBoxes to disable properly.
I've set "ApplyAuthorization on readWriteAuthorization" to true and the textBoxes are databound to the correct properties.
I've also added the following lines to the CanWriteProperty of my object:
if (propertyName == OpeningDateProperty.Name) return false;
if (propertyName == ChangeDateProperty.Name) return false;
if (propertyName == CloseDateProperty.Name) return false;
return base.CanWriteProperty(propertyName);
I can't figure out what I'm doing wrong here. I've implemented pretty much the same thing recently in other UserControls without any problems...
I am using Windows Forms in C# .NET (Visual Studio 2008)
EDIT: The code snippets and the properties are taken from my customer object. The date represent opening, last change and closure of the customer account. They are never supposed to be edited at all and in fact in the old sollution they are represented by textLabels, however we now want to use a text box and make the property's CanWriteProperty false.
I realise that the information might be sort of scarce, but I am looking for what I might have forgotten in this process.
EDIT: We are using CSLA as well and I guess (I'm new at this whole thing) this has something to do with why we want to do it like this.
EDIT (Sollution): As you can see in my answer below, the problem was that I had not set up the CurrentItemChanged event like I should have.
If you're trying to get them to be read only, then just set the .ReadOnly property to true.
Alternatively, if you're never ever using these textboxes for editing, then maybe just use a Label instead?
EDIT: Ahh it appears this more of a CSLA-framework question than a pure windows forms question. I've never even heard of CSLA before this question, but it looks interesting.
If you are databinding to properties of the control just bind the "ReadOnly" property of the textbox to the "CanWrite" property of your business object.
i think you mean ReadOnly property
To make this work you need to do the following:
Make sure the TextBox is databound to the right property in the correct way
Set up the needed checks for each textBox in the CanWriteProperty override in your root object
if (propertyName == OpeningDateProperty.Name) return false;
Make sure the rootBindingsource's CurrentItemChanged event is set up right
private void rootBindingSource_CurrentItemChanged(object sender, EventArgs e)
{
readWriteAuthorization1.ResetControlAuthorization();
}
Make sure the texBox's "ApplyAuthorization on ReadWriteAuthorization" is set to true
This solved the problem for me.