다음을 통해 공유


Windows Forms Controls

Posted February 18, 2003

Chat Date: January 14, 2003

Please note: This transcript has been edited for clarity.

Chat Participants:

  • Fred Balsiger, Software Design Engineer
  • Michael Harsh, Program Manager
  • Helen Allas, Software Design Engineer
  • Seth Grossman, Programmer/Writer
  • Scott Berry, Net Client Team Tester
  • Steve Hoag, Technical Writer
  • Nicholas (MVP), Microsoft MVP for .NET/C#
  • Michael Giagnocavo, Atrevido Software Corporation

Moderator: Ed_H (Microsoft)
Let's get started! Fire away with your questions for our hosts.

Host: Scott (Microsoft)
Q: is there a common dialog that I can use to prompt a user to select a Folder?

A: Michael's right -- FolderBrowserDialog will be available in v1.1. To get it before that, you'd need to interop to SHBrowseForFolder

Host: Samuel (Microsoft)
Q: The ListView control we used in VB6 allows an icon to be placed in every column (Details view). The .net ListView only allows icons in the first column. Will we get this functionality back in a future release of the framework?

A: Windows Forms uses a different list view control than VB6 and this functionallity is being concidered for a future release.

Host: Scott (Microsoft)
Q: I'd like to be able to have different column names <in a grid>: for example, some might be 2 separate words.

A: You can use TableStyles to do this: you add a TableStyle for your table, and add ColumnStyles with HeaderText set to whatever you want.

Host: Seth (Microsoft)
Q: Generally, when are WinForms controls not a good idea? Services? Can you explain further? Suggest any resources. Services? Can you explain further? Suggest any resources.

A: When you don't need an interface for what you are doing, for processes that don't have access to a user interface. Services are a perfect example of this. (answer provided by Nicholas (MVP))

A: There may be other times. Controls are a little heavy: you probably wouldn't want to have a grid where all the cells are controls. See the MSDN help topic "Introduction to Windows Service Applications" in the help at ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconintroductiontontserviceapplications.htm, or online at

https://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconIntroductionToNTServiceApplications.asp.

Host: Samuel (Microsoft)
Q: IE/Security/Controls : Say I derive from Control and override WndProc, then the resultant Control is unusable in IE (without Full Trust). Seems like a huge limitation in the Security Model. Any ideas to workaround?

A: There is no work around as any work around would be a security hole and allow a control to handle Windows Messages. You must give the control Full Trust to be able to override the WndProc.

Host: Scott (Microsoft)
Q: Can I set the DataGrid column names in Visual Studio? Is there a way to set up a table in VS so that it looks like a table in, say, MSWord? I want to see rows and columns in VS?

A: I'm not sure what you're looking for specifically. If you actually want to see the data in the designer, it's possible, but not necessarily easy.

Host: FredB (Microsoft)
Q: is there away to dynamically add implementation code behind the event at run time

A: At runtime, you could unhook an existing event handler and re-hook it back to a different delegate, hence run different code.

Host: Samuel (Microsoft)
Q: Samuel: Are there any guidelines on what can be done in a derived Control and preserve it's "safe" nature (so that it can be run with partial trust)?

A: I don't believe that there is any general document that outlines all the required security permissions for various tasks. The docs will state any security requirements to call a given method or property though. I would recommend looking there for security information for your specific task.

Host: Seth (Microsoft)
Q: I would expect the hook to comctl6 to be automatic ,we should have to do all this manifest business. If you were writing a normal c++ app you would have to it

A: A number of users have requested a less manually-intensive process for hooking to ComCtl 6; while we can't be specific about product plans, the product team is taking this suggestion for future releases. COMCTL 6.0 - with the Everett beta, you can use the EnableVisualStyles() method on the app object. Note: You need to call this before you call Application.Run()

Host: Seth (Microsoft)
Q: Is there a place I can find examples which use the DataGrid? Thanks.

A: A few resources:

The "Formatting the Windows Forms DataGrid Control in Visual Basic" whitepaper https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchformattingwindowsformsdatagridvisualbasicprimer.asp

SyncFusion Windows Forms FAQ (look in the DataGrid section) https://www.syncfusion.com/FAQ/Winforms/

The .NET Samples - Windows Forms: Data Binding - https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpqstart/html/cpsmpnetsamples-windowsformsdatabinding.asp

Host: Samuel (Microsoft)
Q: Samuel: What would be nice is somehow the "unsafe" overrides (like WndProc) could be skipped if the Control doesn't have enough permissions. Then the same Control could still be used in a partially trusted environment.

A: Unfortunately there is no easy way to do this. The best work around would be to create 2 controls, one safe one unsafe, and choose which one to load based on the security settings. For the IE Hosted case you will need to place the control on a user control to do this.

Host: FredB (Microsoft)
Q: dave_overall : nicholas: well i have written my custom control ( like the xp taskview control ) when i place the control on my form i want to write click and on the context menu display an item that will fire up the collectioneditor

A: What you'll need to do is: 1) Create a designer (class extending from ControlDesigner) for your custom control

2) Define a new DesignerVerb, add it to the DesignerVerb service. Have your DesignerVerb's click point back to a delegate on your custom control designer.

3) Hook your custom control to your designer via the Designer attribute

Host: Samuel (Microsoft)
Q: Any reason the IE ActiveX control has problems navigating when it's directly on a form, but when in a panel it works fine?

A: If you can get a solid repro I suggest posting it to the windows forms news group, microsoft.public.dotnet.framework.windowsforms, and someone should be able to take a look at it.

Host: Scott (Microsoft)
Q: If you have Form.KeyPreview=True, and a combobox has the focus, the form doesn't receive key events

A: This is a known issue that we'll be addressing in the future.

Host: Seth (Microsoft)
Tip: For all of you folks searching for MSFT-related answers via Google (esp. if you are frustrated with httsupport.microsoft.com), you can go to https://www.microsoft.com/en-us/default.aspx. This limits the search somewhat, so that you don't get back bogus hits.

Host: FredB (Microsoft)
Q: dave_overall : fred: i have already a designer for my control where i add my own verbs and handle the context menu. What i want to do is have an item on the context menu that’s says "Views" and when you select it , it will fire up the View Collection Ed

A: This is difficult. You need to get the UITypeEditor for this particular property - then call .EditValue() on the editor. Now, the tricky part is that you will have to implement your own IEditorService object to pass into your UITypeEditor instance.

Host: Scott (Microsoft)
Q: Any ideas on how to enable DataBinding in non-Control derived classes? I wonder why the framework provided built-in support for only Control derived classes?

A: The DataBinding makes use of the BindingContext, which is only available on a form. It makes sense to put it in a common base class, and Control is really the only base class where it could sensibly go. To write your own isn't trivial. At a minimum, you'd need to be able to grab your data (and, if for two way, to send it back), listen for ListChanged events on an IBindingList source, and CurrentChanged and ItemChanged on the corresponding CurrencyManager.

Host: Samuel (Microsoft)
Q: Shan : can we use Winform over the web? If so do we need .NET on the client side?

A: Yes, and Yes. Windows Forms controls can be either hosted in a web page in IE (see the Control In Browser Quickstarts) or run as HREF exes (downloaded and run upon accessing them via the web). To run a Windows Forms exe as an HREF exe simply place t on a web server in a virtual directory and access it as you would any other web page. It will run on the client under Internet security permissions.

Host: Seth (Microsoft)
Tip: For those of you who want to know when Knowledge Base articles are released, there is a free service that will send you mails (infrequently enough that it is not annoying) at https://kbalertz.com - you can even customize which technologies you’d like to know about.

Host: Steve Hoag (Microsoft)
Q: What is working on the .NET Framework like?

A: It's the second best job in the world... :-)

Host: Steve Hoag (Microsoft)
Q: What's the first best one?

A: Using the .NET Framework...

Host: Nicholas (MVP)
Q: I have a question regarding menu items... you can dynamically add menu items at run time...

A: Absolutely

Host: Nicholas (MVP)
Q: Can the same be done for the code defined by the event…I can define and event at run time for sure...is there away to dynamically add implementation code behind the event at run time. Here is a scenario...I have a windows forms application that contains a few modal (parent) forms, Each of the parent forms has a help menu. Through code I can build a consistent/uniform Help menu. I’m trying to encapsulate the code within a single procedure across multiple forms. If I have 3 forms each wioth a Help|About menu...I would need to have a helpAbout procedures within the respective windows forms. I'd like to have some way of just one procedure or an object that spits-out/dynamically binds code at runtime.

A: Hmm, so it sounds like you want to hook the same event handler up to every menu item that you generate, for help, that is. Basically, you have your one method and then for the menu item, assuming it is named pobjMenuItem, do this: pobjMenuItem.Click += new EventHandler(this.MenuItemEventHandler); And then you can differentiate in your event handler what menu item was clicked, and act accordingly. You can have the event handler anywhere you want, they don’t have to be in the form that contains the menu, or the event. Doesn't have to be static, just accessible to all of the MenuItems that you want to hook up the event handler for. You can also hook up event handlers to static methods as well, so that might very well be an option.

Host: Nicholas (MVP)
Q: I'm writing an app in C# which bascally consists of a table of data which is constantly updatingThe data in the table comes through a socket (it's not from a database), and I place it in a C# class. But, the column names are the same as the accessors. 'm using a datagrid bound to an arraylist of these objects. The data displays fine. I'd like to be able to have different column names: for example, some might be 2 separate words. It's a financial application which is receiving lots of real-time data. Putting the data in a db is not an option. Oh, ok. Thanks. By the way, can I set the DataGrid column names in Visual Studio? Is there a way to set up a table in VS so that it looks like a table in, say, MSWord? I want to see rows and columns in VS? Can I?

A: If this is the case, then you should probably use a datatable and populate the table from the socket. This way, you can dictate the header for the column. There ^might^ be an attribute you can add to your property though for the display name, and it might work like that. I dont know it offhand though. Not a DB, but rather, a DataTable, it doesn't have to be linked to a back end database. The data architecture is disconnected.

Host: Nicholas (MVP)
Q: IE/Security/Controls : Say I derive from Control and override WndProc, then the resultant Control is unusable in IE (without Full Trust). Seems like a huge limitation in the Security Model. Any ideas to workaround? I have a lot of derived Controls that I want to depoly in IE (without givinig full trust).

A: It's a limitation, but one I think that is necessary. What are you trying to do? Unfortunately, controls require a certain amount of trust. I imagine that since you are interacting with the message loop, this qualifies on some level as going through unmanaged code, which requires full trust.

Host: Nicholas (MVP)
Q: can i add x86 Assembly Language code to C#???

A: No, you can not.

Host: MichaelGiagnocavo
A: But you can call a C++ library with P/Invoke. Also, the C++ compiler lets you do some interesting mixes of managed/unmanaged code.

Host: Nicholas (MVP)
Q: Is there a place I can find examples which use the DataGrid? Thanks

A: Try this: https://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

Host: MichaelGiagnocavo
Q: Also it would have been nice if MS had developed an assembly/classes that would have wrapped up all of the camcat6 specific stuff and also one for doing themes properly. Maybe it could be another application block... But is it not true that anything written for comcat5 will work using comcat6 or has the compatibility been broken all together

A: They do it that way because some things have problems with the new visual styles. So they want you to explicitly say you want visual styles.

Host: Nicholas (MVP)
Q: Can anyone answer why the decision was taken not to automatically hook into comctl6 on winxp. I would expect the hook to comctl6 to be automatic ,we should have to do all this manifest business. If you were writing a normal c++ app you would have to it. Ok so i link explicitly to v6 in c++ and i can use all of the latest xp stuff. In dot net i have to use the manifest trick and to use anything new i have to resort to using pinvoke. Shouldn’t the clr be intelligent enough to at least recognize the app is running on xp and hook into comcat6 anyway?

A: Probably because there is SOOOO much that depends on comctl5 that they didn't want to risk breaking compatibility. So they just made that the default. If you were writing a normal C++ app, you still would have to do the manifest, or link directly to COMCTL6. It could, possibly, but considering that XP is the exception and not the norm right now, I wouldn't expect it anytime soon.

Moderator: Ed_H (Microsoft)
This has been a GREAT chat. Thank you to everyone. Unfortunately, it is time to go. Thanks for participating, and we'll see you next time!

Moderator: Ed_H (Microsoft)
And thanks to Nicholas(MVP) and Michael for helping out!

Top of PageTop of Page