|
In addition, if you want to use coordinates relative to your control, see the PointToScreen method inheritted from the Control class that will return screen coordinates for the given client coordinates.
Also, they re-define the VK enumeration. You don't need to do this. The Keys enumeration in the System.Windows.Forms namespace contains the same defs.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am currently using a listbox to list strings in my app. This is convenient for handling a single double click event and processing the event depending on which item was clicked. The problem i am having is that only the listbox forecolor is allowed to be modified, not the items forecolor. If an item is currently selected (in this case the media file is playing) i would like the color to be different. I want the user to be able to double click an item to play. Originally I populated a blank panel with labels so that I could change the colors, however, creating event handlers for each one is not the right approach. Is there a way to change the color of individual items in a list box; OR is there a better way to manage a single event for multiple controls and still get information from the control that was clicked.
thanks in advance
John C
|
|
|
|
|
A couple of things...hopefully they can help:
1) You can do what you want to do with a listbox. You just need to make it into a user-drawn listbox. In a nutshell: set the DrawMode property to one of the OwnerDrawn options:
<br />
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;<br />
Then handle the DrawItem event. I've attached a simple program below that draws the selected list item in CadetBlue. It's a little more involved than just setting a color but it's also very powerfull. There are a number of interesting examples in the articles here at codeproject...just snoop around.
2) You can manage a single event on multiple objects very easily: just create event handlers that call the same function. For example:
label1.Click += new System.EventHandler(this.GenericClickHandler);
label2.Click += new System.EventHandler(this.GenericClickHandler);
...
private void GenericClickHandler(object sender, System.EventArgs e){
}
Here's the code I promised:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ListBoxText
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.Container components = null;
int i=-1;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.listBox1.Items.AddRange(new object[] {
"one",
"two",
"three",
"four",
"five"});
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 199);
this.listBox1.TabIndex = 0;
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(120, 197);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == this.i)
{
e.Graphics.FillRectangle(Brushes.CadetBlue,e.Bounds);
}
else
{
e.Graphics.FillRectangle(Brushes.White ,e.Bounds);
}
e.Graphics.DrawString (this.listBox1.Items[e.Index].ToString(),new System.Drawing.Font("Comic Sans", 8),Brushes.Black, e.Bounds);
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.i = this.listBox1.SelectedIndex;
this.listBox1.Invalidate ();
}
private void label1_Click(object sender, System.EventArgs e)
{
}
}
}
|
|
|
|
|
Didn't we have this conversation a long time ago, and I told you that players like Windows Media Player and RealOne actually use the List-View common control, which is encapsulated by the ListView control in the .NET FCL? It already supports all this behavior. Read the class documentation in the .NET Framework SDK to learn more.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I don't think we've had this conversation... This is the first time I've asked this since I finally got around to coding. Thanks for the reply.
|
|
|
|
|
My apologies, then. I did have this conversation with someone quite some time ago.
Anyway, the ListView is probably what you want. If you're thinking of something like Windows Media Player or RealOne uses (keeps it's selection after loosing focus), then you definitely want the ListView . Hooking up events to either is a cinche, but the ListView does have an ItemClick event making it a little easier. Also, you can store sub-items (like you'd see for the file size, modified date, and file type in Windows Explorer which also uses the List-View common control) which might be a good place to keep the filename, or you could always use the ListViewItem.Tag property which could be anything you want (but won't display).
If you set ListView.HideSelection to false , then when the control loses focus the item is still highlighted (though in a different color defined by the user's display preferences). All the highlighting is done for you already, too. Like I said, WMP, RealOne, Windows Explorer, and many, many other applications use this control probably more than any other complex control.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Did you notice the toolbar below the posting window? Why don't you try using it, and you'll see the supported HTML tags you can use, as well as emoticons below the formatting toolbar. If you don't see it, try using Internet Explorer.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I would just like to say, that this is not good. Why on earth is it so hard to implement support for mozilla?
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
No one said it's hard, but Chris and his staff have a lot of other things to do besides scripting for both browser camps, and this site is primarily a Microsoft development site (and Chris has said that many times in the past). If you'd like to see it changed, go to the Suggestions forum, although I bet Chris probably already has this on his very long TODO list.
If you want to use Mozilla or Firefox, use IE once and test out the toolbar to see what tags are supported. The basic formatting tags like B, I, and U are supported, as well as PRE (formatted text block) and CODE (include code) and a few others. What tags you use, of course, is not browser-dependent.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I cant handle an enter key press in the DataGridTextBoxColumn.TextBox unlike other keys (a,b,c...)?
|
|
|
|
|
Hi,
Here is what I am looking to do and tell me if this will work. I have programmed an app that runs on PDA's that connects to Sql server. If the connection is down I want to create a queue on the pda to hold messages/data that when connected to the network will download the messages to Sql to insert data into database. Is this possible to do using web services with MSMQ? If so what do I need to setup on the server. Server = Win XPPro and database is really MSDE. So how do I setup the web services and MSMQ on the server? I assume I would need to program a Queue on the client side and then have a web service residing on the server side that has a Queue. Correct? What else do I need? I am using C# to code in and the PDA is Pocket PC 2003.
Thanks,
JJ
|
|
|
|
|
First of all, WinXP is not a server platform. It won't scale and as of WinXP SP2 your connections will be even more limited.
Second, why not just use SQLCE (support is in the .NET CF) and replicate the data between SQLCE and the MSDE/SQL Server? This functionality is easy to set up and replication can be controlled.
I mean, if MSMQ does exist on the particular Windows CE .NET platform (presumably, Microsoft Windows for Pocket PCs have it but I don't have "PocketPC 2003" yet - and OEM's don't have to include it in the platform builder), you're still stuck with writing the listener on the server and queuing messages on the client. Replication between the SQLCE and MSDE/SQL database is easy and there's almost nothing to write.
See Designing "Sometimes Off-line" Microsoft .NET Compact Framework Applications[^] for an article on database replication.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need some direction on how to code a class to view computers on a workgroup from a wireless device. This will be for a pocket PC. What im trying to do is have the pocket PC search each computer (or have the user pick a computer) and the device will connected to it, so i dont have to keep typing in the ip address. The host (computer) will e running a program what will send information on a port, and if its the info the client (pocket PC) need then a connection is setup between the two, so the client can send commands to the host.
modified 16-May-21 21:01pm.
|
|
|
|
|
Unfortunately it has proven otherwise. I have a simple C# form with a label control(prompt), an OK button, and a Cancel button. When the form loads, the OK button is disabled and the form spawns a worker thread that does some work (runs a pin-pad in this case) and calls a callback function when it is done. In the callback function I re-enable the OK button, and I want to set the focus to the OK button but I can't seem to do it. I used the Focus() method and it fails even though the CanFocus property is true. I also set the ActiveControl property of the form but this failed also. Does anyone have any sage advice for me?
Neil Van Eps
"Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
|
|
|
|
|
You will need to Invoke everything coming from the other thread.
|
|
|
|
|
When getting/setting properties or calling methods on a control from another thread, you need to use Control.Invoke . If you're not sure if you need to invoke the method (properties are just getter and setter methods), you can use Control.InvokeRequired . More information and examples can be found in the .NET Framework SDK regarding these members.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
So just because you declare a callback function doesn't mean it runs in the parent thread. Got it - thanks.
Neil Van Eps
"Sweet liquor eases the pain" - Lionel Hutz from the Simpsons
|
|
|
|
|
It runs in the thread context in which it was invoked.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i am working on some reports to export to Excel - in the code I am using the ExportReport() method.
Previously, this all worked fine - the user would click on a button which would kick off this code. A dialog would appear prompting the user to name and save the file. Upon completion of the export, the user would get an "export complete" message box - the file would then be in the correct location.
Now, for some reason, this functionality is not working - the dialog still comes up to prompt the user for the name/location. But after specifying this and clicking OK, nothing happens. No file is created.
Have no idea what is causing this...but is it related to the code not being able to find the u2ddisk.dll??
|
|
|
|
|
Probably, but you give absolutely no details. What reporting engine are you using (presumably Crystal Reports since you mentioned u2ddisk.dll)? Is this on another machine? Has the machine been altered (i.e., new hardware or software installed)? If you deployed your solution to a new machine, did you include all the required files (like the 4 or 5 Crystal Reports merge modules for a Windows Installer package)?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I apologize for the lack of details...I am using my own machine but it was recently altered. There was a dll problem and the sysadmin tried to get it back to its orignial state. He reinstalled VS and supposedly brought everything back to its original state.
I am using the Crystal Reports reporting engine as part of VS.NET 2002. I am having these problems in debug mode - haven't yet deployed these reports onto another machine. In the code I create a CrystalReportViewer to call ExportReport() which was working before my machine was "rebuilt" and now does not seems to. As an aside, I had to manually add a registry entry this morning for the crpe32.dll as there was not value for it and VS could not load that dll when it tried to run the code generator tool. I should have the 4 merge modules included - the big thing is that all this worked fine before but having problems since VS.NET was reinstalled.
Thanks for any help you can provide. Let me know if there are other details you need.
|
|
|
|
|
Sounds like lots of registry values and environment variables weren't restored. The Crystal Reports assemblies should be, for example, installed into the Global Assembly Cache (GAC). The Crystal Reports unmanaged runtime also needs to be re-registered or added to the PATH environment variable (I honestly don't remember which, though you should find more information on their web site).
This is definitely a breaking change, something you should've mentioned before. Re-installing VS.NET is hardly a solution if the admin isn't sure what's wrong (a common problem it seems). I would recommend uninstalling VS.NET completely - including the .NET Framework - and reinstall it all, including the Crystal Reports stuff (runtime, designer, assemblies, etc.).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm about to write a C# application to, er, do stuff (not important). I'm going to provide request handlers that can take input from database tables, MSMQ and JMS queues (thank god for J#), Web service requests, HTTP requests, and maybe socket connections using a simple proprietary protocol or even a drop directory. Its admin interface will be ASP.NET pages.
I know pretty well how to write the logic in a nice way to satisfy all the requests from different input. My main question right now is how to arrange the components. I think I'm going to write the main body of the application as a Windows service, which'll give the benefits of startup, shutdown and even power-management hooks, as well as easy administration. I obviously need IIS for the Web services and HTTP support, which is great because it allows me to delegate security responsibilities for that stuff to IIS and the Web applications, as well as the opportunity to spread the load.
I figure the best thing to do is use a TCP/IP remoting channel with a binary formatter between the IIS applications and the Windows service, yes? (I've done some reading and practicing with remoting, but never used it in the real world.) Once I've done this, should I think about just making remoting another method of direct input into the Windows service? I mean, how valuable is it in general to provide access to something via remoting? I mostly see mention of it as a way to provide internal communication between components.
-Jeff
here, bloggy bloggy
|
|
|
|
|
Using .NET Remoting to talk to a Windows Service is very common, and the whole concept of communication between some server object (in this case, an ASP.NET Web Service) is not uncommon at all. Before (and still) server objects talk to COM+ services, which provide a lot of nice features for (almost) free.
As far as how ASP.NET talks to the back end, if you're not sure about Remoting perhaps consider using a provider pattern to make abstract calls to implementations: one could talk to .NET Remoting while another talks to another Web Service or directly to a database...whatever. Every since .NET 1.0 beta days when I realized how great the CTS was, I've been using a provider pattern for most of my bigger apps (including the solution I've architected here - though in a rush-to-market start-up I didn't have as much time as I needed, but will someday get around to revamping). Much of the .NET Framework already uses a provider pattern (.NET Remoting channels and sinks, System.Net authentication modules, etc., and ASP.NET stuff), and 2.0 will use it even more heavily (with great improvements).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
provider pattern
That's a good name for it. That's what I'm used to doing, now. I create an interface for something that takes input, then make one implementation for every type. I do the same for the methods of output. Then, for things that need a synchronous response that depends on processing, I give the ability to associate a particular input method to a particular output method ("sink"). Each request carries context information, etc. Next I write the threading code, so that one thread is attached to each method of input. These threads read in requests (sometimes assigning them priorities; sometimes that's down the processing chain).
Each request is dropped into an in-memory queue at this point, and winds up getting processed eventually. Any sensed admin requests are sent to the front of the queue. Sometimes I make the same thread that processes the request write the output, but when performance is important I usually dedicate a separate thread to each sink too, especially if the same response or result has to be written to more than one output.
Since you bring it up, I have to say that the word "sink" to refer to output is awesome. I formed most of my personal naming preferences doing Java work, where everything in the API is "reader" and "writer" organized, with an occasional "sink" in there usually as the contribution of an old C coder! The word "sink" is now one of my favorite things about .NET. I really am a naming freak.
So you're saying that I could make the ASP app front end another method of input besides remoting, an interesting suggestion. Whatever I do with this thing, though, I'm going to have to provide synchronous responses for both the ASP and Web service requests, so I'm concerned about performance; like, I wouldn't want to funnel HTTP requests through the Web service.
I'll just have to try wading into the remoting stuff, I guess. I have zippo experience with COM+, although I've done some reading on it. I'm sure I'll have plenty of annoying questions in a week or so.
-Jeff
here, bloggy bloggy
|
|
|
|
|