This is a bit hard to explain in the title, but here's my situation. I have existing code and use the standard Textbox throughout. I have decided that I need to add functionality to the Textbox class so I created MyTextbox derived from Textbox and added what I needed. Now I want to permanently use MyTextbox instead of Textbox. I Should be able to force all instances of Textbox (present and future) to use MyTextbox.
I have done something similar before in C++ along the tune of
typedef Textbox MyTextbox;
If this makes sense, is it possible to do in C#?
Why not do a mass find/replace across your solution? This can be done quite easily in Visual Studio.
You could do the following at the top of each file that uses a Textbox:
using Textbox = Custom.Namespace.MyTextbox;
I don't recommend this because anyone seeing what looks like the standard text box in your code may not realize it is in fact your own custom text box. This may also cause ambiguity when compiling the related code.
Another way to make sure that your new functionality is available to everyone who uses TextBox instead of MyTextBox is to use extension methods. Then you wouldn't have to worry about the user using the wrong class.
MSDN Article on Extension Methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx
Why would you need to refer TextBox class again if you already have created MyTextBox. Remember, MyTextBox has all the functionality from TextBox and its own functionality as well.
In case you want to change all existing textboxes from your to comply to MyTextBox, you have to go manual. Edit them all because even if you change the type from .Net Framweork Base Class Library, exceptions would be thrown wherever a TextBox is called
Related
I’m trying to add a honey pot field to our contact us form. When I add hidden field to the form via the Kentico GUI (with conditions making it invisible) it’s not available in the source so I don’t think it will actually work. However I also tried adding the form via the GUI and trying modify the style on prerender in my form control won’t work either (Code below). It is odd as it will actually let me change the value of the field in my form control but not the styling. Is this typical of Kentico and is there a solution to trying to implement a honeypot field? I had suggested we just add more validation methods to the form, but I was told that they want the same behavior as the existing forms.
Here is the method I’m using.
((CMS.FormControls.EditingFormControl)viewBiz.BasicForm.FieldEditingControls["Pooh"]).Style.Add("display", "none");
Thanks
Under the advanced properties of the field input, you should be able to apply styles to the input. You could set display:none; there to hide it from the user, but still have it available in the source.
I'm also going to create a form control for this so that it can easily be applied to another with some validation functionality (i.e. a redirect to captcha)
I have an existing custom control library with controls which may contain properties: HeaderStyle, ModalStyle, Collapsable, etc...
In the user interface the program is currently displaying a categorized list of these properties. I am trying to update this code to hide properties they dont normally use. I have a list of properties to hide/show based on button click but I am not sure how I can hide these fields programmatically.
I would like to retain any values entered into the fields before hiding and re-display the values if the fields are shown again.
Here is a property that current exists but would like to be hidden/shown on toggle.
/// <summary>ModalStyle property for control</summary>
[XmlAttribute]
[DefaultValue(Utility.Common.Enumerations.ModalStyle.None)]
[Category(PropertyCategories.Rendering)]
[Description("Modal dialog style")]
public ModalStyle? ModalStyle
{
get { return control.ModalStyleActive; }
set { control.ModalStyle = value; }
}
My original though was to do some variant on #if DEBUG but use my own Conditional however I was unable to find a way to change my conditionals via button/toggle.
Can anyone please help with a solution to my problem? I have 20-30 controls with 20 to 30 properties that would like to be filtered.
I have two suggestions that, while they may not give you the exact functionality desired, will keep your solution much more straight forward.
First:
Since you are the library developer you should just decide what properties you want other developers to have access to though the IDE properties window. If a property is seldom used or not very useful through the IDE then just place the [Browsable(false)] attribute on it.
Second:
If you really want all properties to be visible in the IDE properties window, but want to give individuals a way of hiding the more advanced (or less used) ones, just throw them all in an 'Advanced' category. The user can then simply collapse that category and forget about them.
Also: Take a look at Oliver's answer to this question:
[how-to-show-or-hide-properties-dynamically-in-the-propertygrid]
I'm not sure to understand what you are trying to achieve.
When you use Attributes, those are static to the class. So, in your case, when you toggle a show/hide on an object, it's on an instance of the object. Also, you cannot change an attribute value at run-time.
Maybe you should try an alternate solution like creating a global
map<pair<type of object, property name>, is shown>
and update that accordingly from your editor.
And if you want to use something like a property grid, you will have a problem since it won't check your map, but it can be fixed. You could create a new class at run-time and make it a proxy to your current instance. (check on the net how to achieve that, but it's quite simple. There are 2 possibilities: compile from a string or use the ILGenerator.
Hope this help.
I have a situation where i have a very big project and i want to change max length property of every textbox that exist in application. How can i do this with small effort and without overhead on server.
Create a new class where you inherit from the standard TextBox.
Set/override the MaxLength property in the new class.
Use ASP.NET tag mapping to replace all instances of the standard TextBox with your new one. In web.config:
<pages>
. . .
<tagMapping>
<add tagType="System.Web.UI.WebControls.TextBox"
mappedTagType="YouNamespace.YourTextBox" />
</tagMapping>
</pages>
TagMapping is done by the compiler, so there's no added runtime overhead.
You could also use a control adapter, but in this case I think tag mapping may be cleaner and easier.
You can use a control adapter to customize the rendering of all the controls of a specified type, so this way you have control over the max length accepted by the text box.
From the ControlAdapter documentation (emphasis is mine):
An adapter modifies a control for a specific browser or class of
browsers or acts as an arbitrary filter on some capability.
In this case the arbitrary filter would be a common MaxLength for all TextBox controls.
Refactor. If all your TextBox controls on the project derives from System.Web.TextBox then make your own MyTextBox that derives from System.Web.TextBox and make all your textboxes derives from this instead.
So, in, the MyTextBox just set the preferred MaxLength
Use search-replace option of text editors.
alternatively if you do not want to hard code it, use a common Java script file on page load. In this javascript set the MaxLenght of each TextBox of the page.
Something like this in Jquery should work for you
$(document).ready(function()
{
// replace 6 with new length you want to set
$('input[type=text], textarea').attr('maxlength','6');
});
I already did some research and ended up with several autocomplete boxes which have one thing in common: they all match the whole expression which has been entered. Sometimes, they seem to be easily expandable, but at the end, they aren't.
However, I need a Textbox which allows the user to enter a word (e.g. "tag1"), displays a popup with some suggestions based on this single word, accept the suggestion with the return key and type in a new word (e.g "tag1 tag2") in the same textbox, with the popup popping up again. (I like the way CintaNotes handles this)
I need this for a tagging interface. It's often faster for the user to write the tags into a simple box, but sometimes, he needs assistance. This is what the autocomplete is for.
I've found some results, which don't work for my purpose (imho):
http://www.wpfpedia.com/item/details/743/wpf-autocomplete-textbox-control
http://www.codeproject.com/KB/WPF/WPF_Autocomplete.aspx
http://www.codeproject.com/KB/WPF/autocomplete_textbox.aspx
http://weblogs.thinktecture.com/cnagel/2011/01/autocomplete-textbox-with-wpf.html
Btw, I really like the way the Tag-Box for SO operates.
Does anyone have an idea? Is there a "out-of-the-box" - solution somewhere, which suits my needs but I didn't find? Or do I have to build one myself?
Thanks! :)
I think you mean a textbox which autocomplete for multiple words.
Like TokenizedTexbox on WPF Extended Toolkit.
this is the page: http://wpftoolkit.codeplex.com/wikipage?title=TokenizedTextBox&referringTitle=Home
Probably you would need to create your own Dictionary object of Key and Value pairs and hook that Dictionary to the textbox events and popup a suggestions dialog that displays the Value(s) from your Dictionary
Check this implementation out: http://code.google.com/p/kocontrols/downloads/list
You may inject your own Search algorithm and your own converter which converts the selected element to text, which you display in the TextBox. You will have to modify it slightly but I think that you might be able to solve your problem by basing your control on this implementation.
I never thought about this type of use case.
Can't you use different textboxes for the different tags? Something similar to how goole code does it?
If you have time, you can use the RichEditControl or TextBox and apply the same pattern used in Intellisense engine or Code Completation enabled editors: Hook the text changes events, Recogize context (last/current word) and display the options list on popup control. And optionally, on commit (detect acceptation or space key), apply the format to the word.
Instead of using all the base wpf controls such as Label, TextBox, Grid, ect. I want to create a sub class of all these base controls and use the sub class.
e.g.
public class MyTextBox : TextBox {}
They would be dummy classes for now, but it leaves room to be expandable in-case I need to in the future. Is this recommended or is it unnecessary?
This is a text-book definition of yagni
It sounds unnecessary, unproductive, and definitely not recommended!
If for some reason you need a subclass in the future it will be much easier to simply create it then than it to do lots of unnecessary work now.
This sort of approach tends to be overkill. Where it does have its uses is when you know you will need to change the base UI controls at some point in the future. So if you were using a third party control suite then you might look at this approach, but you wouldn't want to do it with the MS controls as they are baked into the framework - you aren't going to change those!
Another approach is to ensure you follow a pattern like MVVM - this ensures that you have a UI that is nicely separated from your working code, and you can change controls with the minimum impact.
I think this is unnecessary. We can easily override the look and feel of a control using styles. In case you want to add properties, you can use attached properties.