How may I prevent the Designer changing control member modifiers to private? - visual-studio-2010

When in the Designer I change a property of a DataGridViewColumn on which I previously manually changed the modifier to public in the .Designer.cs file, the modifier gets reverted to private.
Is there any way to prevent this?

I would recommend not changing the designer.
If you really need to have your controls public, I would recommend adding a property to expose them in your code file (not the designer file):
public TextBox MyTextBox { get { return this.textBox1; } }
This will provide public access to the designer generated types without worry of the designer overwriting your changes.
It also makes it much more clear, in the long run, since your public API is defined in your main code file, and not in a second, designer generated file.
That being said, in general, I'd avoid this. Instead of exposing the control itself, I would actually recommend exposing the data that you want to set. Take the text box above - If this text box was a title, I would expose that directly:
public string Title
{
get { return this.textBoxTitle.Text; }
set { this.textBoxTitle.Text = value; }
}

Related

Styling an extended TextBox control in Windows Phone 7

Totally new to custom control creation in Silverlight.
I'm wanting a custom control that inherits from a TextBox control. I've found plenty of tutorials but they all do something like watermarked text or other attached properties. My goal is only to manipulate text at time of entry using the KeyUp event, so visually my TextBox is no different from a standard TextBox.
I created a class file and inherited from TextBox, but at run-time the textbox doesn't display. From what I can gather I need a themes/generic.xaml file, but all of samples I've seen include styles for the additional properties, and in my ignorance I don't know what to change and/or remove.
I'm hoping someone can point me to a generic plain-jane TextBox style definition or a tutorial of such.
What you described should work, I just tried the following and the TextBoxEx renders just fine:
public class TextBoxEx : TextBox
{
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
}
}
You do not need to add a generic.xaml file. This file is used to provide a template which defines the look of your control. You specify the default look of your control by setting the following property:
DefaultStyleKey = typeof(MyControl);
However, as the above TextBoxEx does not set this property, it uses the value inherited from TextBox and hence it inherits the same template (i.e look).

Inject selected view path as HTML comment to start and end of action's output

I'm currently putting together a multi-tenancy web app using MVC 3. At least 30 different Web sites will share a common codebase, and while also sharing similar under-the-hood functionality, they are need to look significantly different. As a consequence I'm using the URL to internally separate out clients, and an overridden Razor view engine (at least in terms of finding the view) to automatically select either a customised view for a given action, or the default view.
To help 'compartmentalise' a complex page, any particular view will make use of several partials, usually rendered as self-contained actions, so that generally a custom view or partial view will only have small HTML differences, helping to minimise any code specific to a client's site.
I hope you followed that background!
So one HTML page might be made up of lots of little smatterings of HTML partial views, which could come from specific folders for the client, or a general-purpose version. I'm hoping to make it easier for our designer to make minor changes to a page by easily seeing where in the folder structure the bit of HTML he wants to change are.
My proposal then is that each partial will be 'bracketed' with HTML comments such as:
{ Content of partial }
Obviously I could put these in manually, but that's just asking for trouble, for typos, for copied and then modified client versions not being updated with the correct URL. It should be possible to get this from some context and inject it, I think.
At the same time, I need to be able to not do this for certain Actions. Eg, a partial might be generating text inside a textarea, say, so the comments wouldn't be appropriate there. On the whole I'm happy to put these comments in unless I specify that it's not appropriate.
For me this suggests an ActionFilter on an Action, which I can apply site wide and then turn off for certain Actions. I'd hope that one of the overridable events would let me ascertain this path, but I can't seem to find anywhere it's stored. Furthermore, OnResultExecuting seems to fire before the Partial has been selected, and OnResultExecuted seems to have already written out the contents of the Partial, so I can't insert the starting comment here. I also can't find any reference to the path of the selected partial.
Just for completeness, it's my intention that this attribute would only write these comments when compiled in Debug mode.
So, does anyone know how I might be able to get the path to the selected View without any kind of hack between FindPartialView and the Attribute? Is my Attribute method the best choice or is there an easier way to do this? Perhaps something's built in already!
Many thanks for your help.
Well, I've never forgotten about wanting this, and always hoped I'd solve it one day, and thankfully I have.
I've overridden the default WebViewPage (I use the Razor engine), and in particular ExecutePageHierarchy to inject the comments:
public abstract class PaladinWebViewPage : PaladinWebViewPage<dynamic>
{
}
public abstract class PaladinWebViewPage<TModel> : WebViewPage<TModel>
{
public bool DisplaySourceCodeComments
{
get { return ((bool?) ViewBag.__DisplaySourceCodeComments) ?? false; }
set { ViewBag.__DisplaySourceCodeComments = value; }
}
public override void ExecutePageHierarchy()
{
base.ExecutePageHierarchy();
// Filters can be used to set and clear this value so we can decide when to show this comment
if (!DisplaySourceCodeComments) return;
var sw = Output as StringWriter;
if (sw == null) return;
var sb = sw.GetStringBuilder();
sb.Insert(0, string.Format("<!-- Start of {0} -->", VirtualPath));
sb.AppendFormat("<!-- End of {0} -->", VirtualPath);
}
VirtualPath tells us the exact file used to build the HTML, so we can inject the filename before and after. This isn't doing anything at the moment, since the default is to not show comments (the "?? false" in DisplaySourceCodeComments).
Also to use this view page you need to edit Views/Web.config and change the pageBaseType to this type.
I want to selectively turn these comments on and off so I've created an ActionFilter:
public class DisplaySourceCodeCommentsAttribute : ActionFilterAttribute
{
private readonly bool _displaceSourceCodeComments;
public DisplaySourceCodeCommentsAttribute(bool displaceSourceCodeComments)
{
_displaceSourceCodeComments = displaceSourceCodeComments;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var viewResult = filterContext.Result as ViewResultBase;
if (viewResult == null) return;
viewResult.ViewBag.__DisplaySourceCodeComments = _displaceSourceCodeComments;
}
}
I'm slightly unhappy that I've had to use the ViewBag here and also separately in the view page override, as they aren't tightly linked, but I can't find a way for the filter to directly interact with the view page, so this is something of a necessary fudge. It does have the benefit that displaying source code for a view or partial also automatically displays it for any child partials until you turn it off again, since the ViewBag is passed down the chain.
With this in place, any action can turn on the source code comments with
[DisplaySourceCodeComments(true)]
or, obviously turn it off again with false
The Attribute checks that the context result is a ViewResultBase, which means just Views and Partials, so Json or Content or redirects aren't affected, which is very handy too.
Finally, I make this action filter a global when running in debug mode so that every view, and partial has the source comment included, by adding the following line to global.asax.cs:
[#]if DEBUG
// When in debug mode include HTML comments showing where a view or partial has come from
GlobalFilters.Filters.Add(new DisplaySourceCodeCommentsAttribute(true));
[#]endif
I'm really happy I've finally got it sorted so I hope this is useful for someone else.

Eclipse RCP: How can I update a view when the selected editor changes?

This should be quite a common problem, but I couldn't find anything helpful on the topic.
We are developing an application with Eclipse RCP. The application shows data in an editor of which usually multiple instances are open. In an additional view you can edit the editor-values. When the values are changed in the view they are updated in the editor and it's dirty flag is set.
So far it works fine. What we're missing is: When another editor instance gets the focus, our view should show the data of this editor.
I managed to do that for two views. The second view is sucessfully updated using a TableViewer as selection Provider and registering a SelectionListener in the other view. I tried the same thing for the editor using a Viewer I subclassed from ContentViewer, but it didn't work.
Can this approach be working?
Or do I need a different approach on the problem?
May be you can subclass your view from PageBookView and then provide special adapter for your editor. Outline View is implemented using this approach.
Thank you cerealk, that was exactly what I needed. :-)
Update the View when another Editor is selected
public class myView {
// Create an IPartListener2
IPartListener2 pl = new IPartListener2() {
// If the Editor I'm interested in was updated ...
public void partActivated(IWorkbenchPartReference ref) {
IWorkbenchPart part = ref.getPart(true);
if (part instanceof DetailEditor) {
// ... update the view
Contact contactFromSelectedEditor = ((DetailEditor) part).detailComposite.contact;
detailComposite.update(contactFromSelectedEditor);
}
}
...
}
// Add the IPartListener2 to the page
IWorkbenchPage page = this.getSite().getPage();
page.addPartListener(pl);
}
Why use an IPartListener2 instead of an IPartListener
The IPartListener2 replaces IPartListener with 3.5.
As explained in this this answer:
You should always use IPartListener2 as it can handle part-change events on parts that have not yet been
created because they are hidden in a stack behind another part.
This
listener will also tell you when a part is made visible or hidden or
when an editor's input is changed.

WatiN and typing capital letters

I'm trying to automate a password entry but the website in question does not allow you to type your password with the caps lock key on. WatiN appears to use the caps lock key in order to type capital letters thus not allow this to work.
Does anyone know a workaround or a way to force WatiN to use the shift key?
You could write your own extension to the TextField class like this...
public static class WatinHelper
{
public static void TypeTextFast(this TextField textField, string text)
{
textField.SetAttributeValue("value", text);
}
}
and then use TypeTextFast instead of TypeText. This would furthermore improve typing speed considerably (particularly in IE) when running the WatiN test. See this for further details.
The TextField class has the Value property you can use to set text of the TextBox directly, without mimicking a manual input, like the TypeText() method.
As a side note, because the value is set behind the scene, it may not raise an event that the value has change, which could be necessary if action append as you type. The TypeText() was taking care of that for you. In those case you can use the Change() method after setting the Value.
It has been a while since I created anything with WatiN, but you can assign the text directly with something like this:
TextBox.Text = "PaSsWoRd";
I logged into a website without any problems using the above.

Name property of UserControl or Control

What is special about the Name property of a Control or UserControl that causes it to be displayed as "(Name)" inside the property grid within Visual Studio?
Check out this article about design-time attributes in .NET. Specifically, I think you are looking for the Browsable attribute, which enables properties in Visual Studio's design-time properties dialogue.
If you have a property called Name, you'd declare it like this:
[Browsable(true)]
public string Name { /*...*/ }
You can set a lot more attributes, like Description, DefaultValue and Category, which will come in handy if you're planning on presenting your controls to other developers.
EDIT: To get the effect that you want, use both the Browsable and ParenthesizePropertyName attributes:
[Browsable(true)]
[ParenthesizePropertyName(true)]
public string Name { /*...*/ }
(Thanks to Ksempac from the comments for this.)
Since you didn't specify if you're using VB or C#, here's the same thing in VB:
<Browsable(true)> _
<ParenthesizePropertyName(true)> _
Public Property Name(Value As String) As String
' ...
End Property
EDIT 2:
I think you're wondering about why you would want to surround your property with parentheses in the first place, or perhaps what it means for a property's name to have parentheses around it.
You can find the answer to that here:
Parenthesized properties are shown at the top of the window — or at the top of their category if the list is grouped by category
Basically, if a property is important, you want it to appear at the top of a sorted list, so you surround it with parentheses to indicate this.

Resources