Friday, May 6, 2011

Sorting gridviews and sortexpression

Hi I am using a gridview and have have to set a sort expression, I am using:

<asp:TemplateField HeaderText="Order Name" SortExpression="OrderId">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# OrderName(((DataLayer.Car)Container.DataItem).OrderId) %>' />
                </ItemTemplate>
                <ItemStyle Width="120px" />
            </asp:TemplateField>

But I rather want to sort by the name which I get from the OrderId than the actual OrderId - but I havent been able to find out if this is doable, and how?

From stackoverflow
  • You have to use the field name of the data results that you have in your grid. If you have the field in the results, use it.

  • If you are ever need to support paging (and you're not storing the entire result set in session or viewstate or something) you will not be able to page through the results sorted by the ordername.

    As long as your resultset is small (and you are storing it), you could create a new object that contains the OrderID and OrderName, fill it with the orderid's and ordername's, then bind to that.

    The best thing to do would be to have your ordername come from your datasource if possible.

Append text to input field

I need to append some text to an input field...

From stackoverflow
  • $('#input-field-id').val($('#input-field-id').val() + 'more text');
    
    Kevin Brown : Thanks a bunch!
  • You are probably looking for val()

  • If you are planning to use appending more then once, you might want to write a function:

    //Append text to input element
    function jQ_append(id_of_input, text){
        $(id_of_input).val($(id_of_input).val() + text);
    }
    

    After you can just call it:

    jQ_append('my_input_id', 'add this text');
    

    Sorry for not having 50 reputation when posting this, to add comment.

Run Visual Studio 2008 Macro on Pre Build Event

I have some Visual Studio Macros created in visual studio which run from the macro explorer and/or keyboard shortcuts. I would like to run these automatically on pre/post build events but see no option to do so in the project properties. Can anyone tell me if this is possible and, if so, how? Thank you Jason

From stackoverflow
  • I don't belive it's possible. That said, you can quickly create custom MSBuild tasks to do the job of the macros.

  • In your macro there are 4 build events you can hook into:

    • OnBuildBegin -- Will fire when any build operation is fired from the IDE. It fires only once for a full solution or multiproject build operation.

    • OnBuildDone -- Will fire when a build operation completes. This event fires only once for a full solution or multiproject build operation.

    • OnBuildProjConfigBegin -- Will fire when a project build begins. This event is used to catch each project build event within a solution or multiproject build operation.

    • OnBuildProjConfigDone -- Will fire when a project build completes. This event is used to catch the completion of each project build within a solution or multiproject build operation.

    Common Environment Object Model Events

How do I load a MOV file into Flash 9

I am having issues loading an MOV file into Flash 9. When I google the question, I get alot of people say sure you can do it, but nobody provides working sample code.

From stackoverflow
  • The following is from http://en.wikipedia.org/wiki/Adobe_Flash#Flash_Video

    Flash Video

    Flash Video (.flv files) is a container format, meaning that it is not a video format in itself, but can contain other formats. The video in Flash is encoded in H.263, and starting with Flash player 8, it may alternatively be encoded in VP6. The audio is in MP3. The use of VP6 is common in many companies, because of the large adoption rates of Flash Player 8 and Flash Player 9.[16]

    On August 20, 2007, Adobe announced on its blog that with Update 3 of Flash Player 9, Flash Video will also support the MPEG-4 international standard.[31] Specifically, Flash Player will have support for video compressed in H.264 (MPEG-4 Part 10), audio compressed using AAC (MPEG-4 Part 3), the MP4, M4V, M4A, 3GP and MOV multimedia container formats (MPEG-4 Part 14), 3GPP Timed Text specification (MPEG-4 Part 17) which is a standardized subtitle format and partial parsing support for the 'ilst' atom which is the ID3 equivalent iTunes uses to store metadata. Adobe also announced that they will be gradually moving away from the proprietary FLV format to the standard MP4 format owing to functional limits with the FLV structure when streaming H.264. The final release of the Flash Player supporting MPEG-4 had become available in Fall 2007.[32]

    package com.yourdomain.packageName
    {
        import flash.display.Sprite;
        import flash.media.Video;
        import flash.net.NetConnection;
        import flash.net.NetStream;
    
        public class DocumentClass extends Sprite
        {
            private var _video:Video;
            private var _nc:NetConnection;
            private var _ns:NetStream;
    
            public function DocumentClass():void
            {
                _video = new Video(800, 600);
                addChild(_video);
    
                _nc = new NetConnection();
                _nc.connect(null);
    
                _ns = new NetStream(nc);
                _video.attachNetStream(_ns);
               _ns.play("path/to/file.extension");
            }
        }
    }
    

    -This is a very basic implementation and does not include handling of meta-data.

    First, we instantiate a Video object that is 800 wide and 600 tall and add it to the display list.

    Second, we instantiate a NetConnection object and call it's connect method passing null as a parameter. (This is required)

    Third, we create a NetStream object, passing the previously made NetConnection object to it's constructor.

    Fourth, we set attachNetStream of the Video object to _ns, the NetStream we instantiated.

    Finally, we call the play method of the NetStream object, _ns, and pass it a string to where our media is located. (The FLV/F4V/MOV/etc.)

    -Hope this helps. Brian Hodge
    blog.hodgedev.com

  • I'm assuming it's an Action Script - sorry I'm really not that familiar with it. How would you call it to initiate the video to be played?

  • Same as with any FLV, using NetStream (I don't recommend it, it's very hard to handle) or any AS3 video playback component you can find, like FLVPlayback, etc... as long as you target FP 9.0.115.0, and the file is encoded in h264 and in a MPEG-4 container like mov, mp4, f4v, etc...

  • Personally I usually just convert the video so some more compatible format. Much less hassle than trying to do some work around code etc. Video converters are free and easy to use, and it is once off mostly.

Rails doesn't like this anymore? "script/generate scaffold Album" for Album model

script/generate scaffold Album

used to generate the scaffolding for a model called Album (pre-Rails 2).

Is it correct that these days (post-Rails 2), you need to specify all of the required fields:

script/generate scaffold Album artist:String title:String

I've been following along with a tutorial on Rails 1.1.6 and have been puzzled about why

script/generate scaffold Album

doesn't work the way the tutorial describes but my suspicion is that the new format of the command requires that you explicitly spell out all of the fields that you want in the scaffold.

It's disappointing if Rails has lost the ability to concisely generate scaffold based on the model name alone since it "knows" through schema.db what all of the fields for the model are without you explicitly declaring them to the scaffold command.

From stackoverflow
  • If you don't specify the fields in the command then Rails won't build any of the scaffolding with the fields information pre-populated, like in the views.

    script/generate scaffold Album
    

    should still be valid, but you'll have to go make all the form and display views as well as define all the fields in the migration file.

    Steve Weet : If you want to have scaffolding that creates view for already existing models then check out Ryan Bates nifty_scaffold http://github.com/ryanb/nifty-generators
  • can anyone tell me how to create references for the attributes in this...

UserControl as an interface, but visible in the Designer

So we have a C# WinForms project with a Form that contains a bazillion UserControls. Each UserControl naturally exposes all the UserControl methods, properties, etc. in addition to its own specific members.

I've been thinking that one way to reduce the complexity of dealing with these UserControls is to access them through an interface. So instead of drag-and-drop to put the UserControl on the form, something like this in the constructor:

public class MyGiantForm
{
    ICustomerName cName;

    public MyForm()
    {
        InitializeComponent();

        var uc = new SomeCustomerNameUserControl();
        this.Controls.Add(uc);
        cName = uc;
    }
}

SomeCustomerNameUserControl implements ICustomerName, naturally, and ICustomerName contains the specific properties I really care about (say, FirstName and LastName). In this way I can refer to the UserControl through the cName member and, instead of being bowled over by all the UserControl members, I get only those in ICustomerName.

All well and good, but the problem is that if I do it this way, I can't see SomeCustomerNameUserControl in the Designer. Does anybody know I way I can do this but still see the UserControl on the form's design surface?

EDIT: One way to do this, which isn't overly complicated, is to put the controls on a base form. By default (in C#) the control members are private. Then I create a property for each control exposing it through the interface.

However, I'd be interested in some other way to do this, even if it's more complex. There seems to be some way to do it with IDesignerHost, but I can't find any applicable examples.

From stackoverflow
  • If SomeCustomerNameUserControl is defined like this:

    class SomeCustomerNameUserControl : UserControl, ICustomerName
    {
    }
    

    You can still drop this control in the designer (which creates someCustomerNameUserControl1) and do this whenever you need to:

    ICustomerName cName = someCustomerNameUserControl1;
    

    Maybe I'm missing something, but I think it's that simple.

    Brian ONeil : Unless there is a detail that we don't have I think that it is this simple... This is what I was going to say.
    Kyralessa : This doesn't answer the question. I want ICustomerName to be the *only* option for accessing the UserControl's variable. The idea is that a developer doesn't have to "just remember" to cast it. I've edited the question to indicate one way to do this, but I'm looking for other ways that don't require a base form.
  • you could as well do as Bob said but assign all your member variables in the constructor, then you have it in one place.

    Kyralessa : That still leaves the programmer the option to absentmindedly use the UserControl reference instead of the interface reference. I want the interface reference to be the only member referring to the UserControl, if at all possible.
  • After you add the UserControl using the designer, you can set GenerateMember to false in the Properties window to suppress generation of a member.

    You could then use some other technique in the constructor to assign your cName reference, e.g.:

    foreach(Control control in this.Controls)
    {
        cName = control as ICustomerName;
        if (cName != null) break;
    }
    

    cName would then be the only reference to the UserControl.

    Kyralessa : This is the first *good* possibility I've seen. GenerateMember didn't even occur to me. To my mind, a better way to do it than the way you describe is to use: cName = this.Controls["controlName"] as ICustomerName; It has the definite weakness of breaking if the control is renamed, though, so it's still not a perfect solution. The reason I don't like the casting solution you mention is that I might have more than one of the same customer UserControl on the same form. In fact, in the project I'm working on, I know of at least one definite case like that.
    Joe : Yes this was meant to be just one example of a tehnique to obtain the desired reference from the Controls collection. If you have more than one instance of a control, you're right that you'll need to use some other characteristic of the control, e.g. the name, to distinguish them. The "best" solution will depend on your app.
  • There's a way to accomplish what you want -- hiding the members you don't want to see -- but make it apply automatically, without requiring others' cooperation in terms of them using a custom interface. You can do it by reintroducing all the members you don't want to see, and tagging them with attributes.

    This is what Windows Forms does when, for example, a base-class property doesn't mean anything for a particular descendant. For example, Control has a Text property, but a Text property is meaningless on, say, a TabControl. So TabControl overrides the Text property, and adds attributes to its override saying "By the way, don't show my Text property in the Property Grid or in Intellisense." The property still exists, but since you never see it, it doesn't get in your way.

    If you add an [EditorBrowsable(EditorBrowsableState.Never)] attribute to a member (property or method), then Intellisense will no longer show that member in its code-completion lists. If I'm understanding your question correctly, this is the big thing you're trying to achieve: make it hard for application code to use the member by accident.

    For properties, you probably also want to add [Browsable(false)] to hide the property from the Property Grid, and [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] to prevent the designer from writing the property's value to the .designer.cs file.

    These will make it very difficult to accidentally use the method/property. They're still not a guarantee, though. If you do need a guarantee, then throw in an [Obsolete] attribute too, and build with "Treat warnings as errors" -- then you're taken care of.

    If the base member is virtual, you probably want to override it, and have your override simply call base. Don't throw an exception, since the overridden member will probably be called by the base class during the normal course of events. On the other hand, if the base member isn't virtual, then you want to use "new" instead of "override", and you can decide whether your implementation should call base, or just throw an exception -- nobody should be using your reintroduced member anyway, so it shouldn't matter.

    public class Widget : UserControl
    {
        // The Text property is virtual in the base Control class.
        // Override and call base.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Obsolete("The Text property does not apply to the Widget class.")]
        public override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }
    
        // The CanFocus property is non-virtual in the base Control class.
        // Reintroduce with new, and throw if anyone dares to call it.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Obsolete("The CanFocus property does not apply to the Widget class.")]
        public new bool CanFocus
        {
            get { throw new NotSupportedException(); }
        }
    
        // The Hide method is non-virtual in the base Control class.
        // Note that Browsable and DesignerSerializationVisibility are
        // not needed for methods, only properties.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("The Hide method does not apply to the Widget class.")]
        public new void Hide()
        {
            throw new NotSupportedException();
        }
    }
    

    Yes, this is a fair bit of work, but you only have to do it once... per member, per class... umm, yeah. But if those base-class members really don't apply to your class, and having them there will cause confusion, then it may be worth going to the effort.

    Kyralessa : Actually, it seems like I'd only have to do it once for any UserControl, and then interject this new member-hiding class in the inheritance tree between UserControl and my various derived controls. If that were the case, it wouldn't be an unbearable amount of work. On the other hand, any amount of work is too much if you don't really have to do it.
    hmcclungiii : That is some very good information.
  • It almost seems like you want to implement a mediator pattern. Instead of having to deal with each of the bazillion UserControls directly, you'd interact with them through the mediator. Each mediator would define the slim interface you want to see from each control. This would reduce the overall complexity by making your design more explicit and concise. For example, you wouldn't need the 20 properties and 50 methods available on one of your controls. Instead you'd deal with the mediator for that control which defines the 2 properties and 5 methods you really care about. Everything would still show up in the designer, but other parts of your app would not be interacting with those controls -- they'd interact with the mediators.

    One of the big advantages to this approach is it greatly simplifies your maintenance. If you decide the MyCrappyUserControl needs to be rewritten because the implementation is bad, you just need to update the mediator class for that control. All the other classes that interact with the control do so through the mediator and would be unchanged.

    Ultimately it comes down to discipline: you and your team need to be disciplined enough to use the mediators/interfaces/whatever instead of the directly hitting the controls. Institute an over the shoulder code review by a leader programmer if your team is on the low end of the discipline scale.

    Kyralessa : I'm looking for a solution that won't require that much discipline, because I know what happens when a solution requires discipline from the average programmer. We have too many things to think about and remember already; a "you just have to remember" solution is no solution.
    Mike Post : This would just become part of your standard design for the code your shop produces. If you're turning programmers loose into your code without first briefing them on your design, this problem of UserControls exposing too many properties is going to be the least of your concerns.
    Kyralessa : Dude, I'm not the guy in charge. I can't enforce coding standards on others. I'm trying to find a subtle but effective way to improve design without forcing people to do things a certain way. To lead by example rather than by edict, perhaps.
    Mike Post : Not every programming problem has a technical solution. Sometimes the right answer is a people solution.
    Kyralessa : I agree in general. But this programming problem has a lot of technical solutions. Some are better than others.
  • You could write an extension method that would allow you to return any controls on the form that implement an interface.

    public static class FormExtensions
    {
        public static IDictionary<string, T> GetControlsOf<T>(this Form form) 
               where T: class
        {
            var result = new Dictionary<string, T>();
            foreach (var control in form.Controls)
            {
                if ((control as T) != null)
                    result.Add((control as T).Tag, control as T);
            }
            return result;
        }
    }
    

    Then in your form you could call it whereever you want by:

    this.GetControlsOf<ICustomerName>()["NameOfControlHere"];
    

    In the event that it returns more than one user control you would need to handle that some how, perhaps by adding Tag property to the interface to uniquely keep track of each user control or something, like so

    public partial class UserControl1 : UserControl, ICustomerName
    {
         public string Tag { get { return this.Name; } }
    }
    

    You can then drag and drop the user controls onto your form from the designer. Tag will always return the name of your control, which will allow you to directly access the control through the IDictionary's interface. You're developers could put whatever unique identifier they want in the name of the control, and it would carry through to the interface.

    Also, it should be noted that this approach will ALSO allow you to use this on ALL forms in your solution.

    The only other thing you would need to do is set your GenerateMember to false.

    Kyralessa : Very interesting. It's similar to Joe's solution, but a bit more elegant. It has the same weakness as his (multiple controls that have the same interface), but I think passing in the UserControl name would alleviate that, though it's still a problem if the control name is changed.
    Joseph : @Kyralessa actually there is no problem with multiple controls, you can use a dictionary instead, and index it based off the name of the control. Then you can remove the extra tag all together. I'll modify my sample to illustrate.
    Kyralessa : If you're going to have Tag return the Name property, why not just use Name?
    Joseph : Because the Name property is not part of the interface. If, however, you only want to use it for the dictionary purposes, then you could drop the Tag property all together. Note that if you use it as a read only property as I've illustrated above, it won't show up in the designer, so you can instructor your developers to use the Name property only and everything will work out correctly.
  • 'I want ICustomerName to be the only option for accessing the UserControl's variable. The idea is that a developer doesn't have to "just remember" to cast it.'

    The problem you are having is that you have two completely divergent uses for your form and the controls it hosts. There is no trick built into Visual Studio or winforms which solves this neatly for you. It may be possible, but there is a much cleaner and object oriented way to separate the two methods of interacting with the controls.

    If you want to hide the fact that these objects inherit from UserControl, and just want to treat them as IDoSomeThingYouShouldDealWith, you need to separate the logic that deals with the presentation concerns (designer + UI logic) from your business logic.

    Your form class, should rightly deal with the controls as UserControls, docking, anchoring etc etc, nothing special here. You should put all the logic that needs to deal with ICustomerName.FirstName = etc into a completely separate class. This class doesn't care or know about fonts and layout, it just knows that there is another instance that can present a customer name; or a DateTime as a 'date of birth choosing' control properly etc.

    This is a really lame example, but I have to go right now. You should be able to get the idea covered here in more detail:

    public interface ICustomerName
        {
            void ShowName(string theName);
        }
    
        public partial class Form1 : Form, ICustomerName
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            #region ICustomerName Members
    
            public void ShowName(string theName)
            {
                //Gets all controls that show customer names and sets the Text propert  
                //totheName
            }
    
            #endregion
        }
    
        //developers program logic into this class 
        public class Form1Controller
        {
            public Form1Controller(ICustomerName theForm) //only sees ICustomerName methods
            {
                //Look, i can't even see the Form object from here
                theForm.ShowName("Amazing Name");
            }
        }
    
    Kyralessa : Noel, I know all about Model-View-Controller, Model-View-Presenter, and the rest. But I'm not at liberty to change the app that radically any time soon. I'm looking for something that will work with the app as it is. There are already several possibilities listed here that can do what I want, to varying degrees of completeness and ease of use. If what I'm looking for here were *easy* to do, I'd have already figured out how to do it, and I wouldn't have bothered to post a question about it on StackOverflow.
    Noel Kennedy : Hi Kyralessa, thanks for the feedback. Not sure why you are looking for a more complex solution, which I'm sure you would agree, are hacks or workarounds to greater or lesser extent. If you are able to (and I accept you might not be at liberty), I would try to find a way to refactor to a UI pattern, even if you can't go to it straight away. For example, you could create the controller in the constructor of the view, which is not so far awat from what Joe is suggesting. You can also move accross functionality and logic into the controller bit by bit so there would be no big bang.
    Kyralessa : It's a work project, not a personal project, and people aren't always free in work projects to refactor to our heart's content. And I'm not specifically looking for a complex solution. I'm looking for a good solution, and it's OK if it's complex as long as it's good. I'm also looking for a solution to a very specific problem. My question isn't "How do I fix this app?" My question is "How do I access UserControls only through an interface, but still have them show up in the Designer?"
  • Assume that MyUserControl is defined like this:

    class MyUserControl : UserControl, IMyInterface
    {
        // ...
    }
    

    Then in your form, you should have something like this:

    public class MyForm : Form
    {
        IMyInterface cName;
    
        public MyForm()
        {
            InitializeComponent();
    
            cName = new MyUserControl();
            Controls.Add((UserControl)cName);
        }
    }
    

    This way, cName is the only way to access this instance of our usercontrol.

    Mike Post : This doesn't let you see MyUserControl in the designer though.
    M. Jahedbozorgan : If you put this code in the designer file, you will be able to see the control. The problem is that your changes will be lost if it is regenerated. This can be solved using a macro.
    Kyralessa : This is exactly the solution I proposed in my original question. As such, it's no help at all.

WinXP button-style with wxPython

Hi, guys.

I noticed that my programs written with wxPython have Win98 button style. But Boa Constructor (that is written using wxPython too) got pretty buttons.

How to make buttons look like current Windows buttons style?

From stackoverflow
  • Have you tried running your scripts with pythonw.exe instead of python.exe?

    Kirill Titov : I will try, but I think - no matter, 'cause the same app written using PyQt works perfectly, I think that's problem of wxPython.
  • Are you packaging the app with py2exe?

    If so you may need to specify a manifest file to make Python use the WinXP(Vista?) theme/Common Controls:

    http://wiki.wxpython.org/DistributingYourApplication

    Kirill Titov : OK, but how about Windows Vista or Windows 7 styles? Does this manifest support that styles?
    John Montgomery : Sorry Mac user, by WinXP i just mean "modern windows". I think it'll work for Vista too, but I only have an XP laptop for testing at home so YMMV.
  • Expanding on John's answer, you may also be able to create manifest files for python.exe and pythonw.exe to see the new styles without first packaging using py2exe.

  • The answers so far handle distributing the package as an executable (eg. py2exe), where the answer has already been given.

    But since (i think) python 2.6 you have the same problem when just starting the .py file from the commandline (Vista and Windows7). Robin Dunn suggested using update_manifest.py which he distributes with wxPython and puts it in the same directory as python.exe.

    After applying update_manifest.py using a copied version of python.exe wxPython apps have the correct themed look and yes it also works using windows7 RC1.