|
I got a question:
Q: You use Visual Studio.net to create a windows form named Form1, you add a custom control named BarGraph, which displays numerical data. You create a second custom control named DataBar. Each instance of DataBar represent one data value in BarGraph.
BarGraph retrieves its data from a Microsoft SQL Server database. for each data value that it retrieves a new instance of DataBar is added to BarGraph. BarGrahp also includes a Label control named DataBarCount, which displays the number of DataBar controls currently contained by BarGraph.
You must add code to one of yor custome controls to ensure that DataBarCount is always updated with the correct value.
What are two possible ways to achieve this goal?(Each correct answer presents a complete solutins.Choose two).
And I got the so-called two correct answers:
A1:Add the following code segment to the ControlAdded event handler for BarGraph:
DataBarCount.Text=this.Controls.Count;
A2:Add the following code segment to the constructor for DataBar:
this.Parent.DataBarCount.Text=this.Controls.Count;
I don't understant why?
who can explain why they are right????
|
|
|
|
|
The only difference between the two really is from where they are called and what you're setting them on. The second code fragment won't work, however, since Parent returns a Control , which won't have a reference to the DataBarCount . You would nee dot cast it to the class you defined and then set the DataBarCount.Text .
The other thing you might consider is a property binding. Bind the DataBarCount.Text property to Controls.Count property like so:
DataBarCount.DataBindings.Add("Text", Controls, "Count"); Something like that should work and would keep everything up to date. You also only need to set this once, then. From where depends on your implementation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how to prohibit user enter the key char on key board except num key.
thanks
Mr Duc Linh Nguyen
|
|
|
|
|
Depends on what you want to do this. If you want to provide a numeric-only TextBox or something similar, extend the class and override IsInputKey (or a number of other methods that would do the same thing in effect):
public class NumericTextBox : TextBox
{
protected override bool IsInputKey(Keys keyData)
{
return ((keyData >= Keys.NumPad0 && keyData <= Keys.NumPad9) ||
(keyData >= Keys.D0 && keyData <= Keys.D9));
}
} Read the class member documentation for the TextBox for more information and other methods you can override if this isn't (for some odd reason) sufficient.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
When Windows shut down, my application running couldn't shut down. My application is the style staying on the
system tray.
So I tried to get Windows shut down event in my application, but I couldn't.
I don't know how to get it. If there are other ways,
please tell me.
regards,
yu-yu
|
|
|
|
|
Add an IMessageFilter implementation (such as on your main application form) and add that instance using Application.AddMessageFilter , or simply override WndProc in your main application form and watch for the WM_QUERYENDSESSION (0x0011) or WM_ENDSESSION (0x0016) depending on whether or not you want to cancel the Windows shutdown. In your handler, simply call Application.Exit or Environment.Exit (warning: the latter will unload the CLR - which unloads your application - without calling any further code for a clean shutdown).
The problem with the application not shutting down, however, is usually when an exception is thrown from the UI thread that goes uncaught while forms are loading, so that the message pump (the loop that translates and dispatches messages to the appropriate windows/controls (which are windows, technically)). Be sure to use effective exception handling in your application.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Mr.Stewart
Thank you for your advices.
So I used WndProc(), and I have another questions.
I don't know how to recognize whether action is shutdown or not(reboot).
I checked member variable (Msg and LParam and WParam) Message class,
so I understood logoff pattern.
But I could not recognize the difference of shutdown and reboot.
If I understand, I could set value in ExitWindowsEx().
regards,
yu-yu
|
|
|
|
|
Mr.Stewart
I'm sorry,
I resolve it.
thank you,
yu-yu
|
|
|
|
|
just a quick one i'm having a little difficulty with alt+f4 closing indevidual forms and not the entire app. so can i disable alt+f4 or redirect it to the correct actions?
|
|
|
|
|
Alt+F4 is a key sequence reserved by Windows for closing applications. Without using a system hook, I doubt very highly that you can override the default behavior.
IIRC, Alt+F4 will only close windows if they are owned by your main application window instead of the application itself. For more information on this, see the documentation for the Form.AddOwnedForm method and Form.Owner property in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
HI Michael,
1.
You can disable the Alt + F4 action by overriding WndProc.
Below is the code for the same.
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg != 0x0010)
{
base.WndProc(ref m);
}
else
{
}
}
2.
Below solution refers to how to disable only X button on the Windows form
In the using section type the following....
using System.Runtime.InteropServices;
declare the following functions and constants...
private const int MF_BYPOSITION = 0x400;
private const int MF_REMOVE = 0x1000;
private const int MF_DISABLED = 0x2;
[DllImport("user32.Dll")]
public static extern IntPtr RemoveMenu(int hMenu, int nPosition,long wFlags);
[DllImport("User32.Dll")]
public static extern IntPtr GetSystemMenu(int hWnd, bool bRevert);
[DllImport("User32.Dll")]
public static extern IntPtr GetMenuItemCount(int hMenu);
[DllImport("User32.Dll")]
public static extern IntPtr DrawMenuBar(int hwnd);
Write a method as mentioned below..
public void DisableCloseButton(int hWnd)
{
IntPtr hMenu;
IntPtr menuItemCount;
hMenu = GetSystemMenu(hWnd, false);
menuItemCount = GetMenuItemCount(hMenu.ToInt32());
RemoveMenu(hMenu.ToInt32(), menuItemCount.ToInt32() - 1,MF_DISABLED | MF_BYPOSITION);
RemoveMenu(hMenu.ToInt32(), menuItemCount.ToInt32() - 2,MF_DISABLED | MF_BYPOSITION);
DrawMenuBar(hWnd);
}
call the above method in the Form load event handler as follows
DisableCloseButton(this.Handle.ToInt32());
For further query do revert back.
Regards,
Jay
|
|
|
|
|
i haven't tried it yet but if it works thats perfect thanks
the only other question is how is the code activated and/or where does it go?
|
|
|
|
|
Hi Michael,
1. It does work. If you are unable to solve the problem. Feel free to revert back.
2. I will prepare a sample application with comments, and post it, when ever I get time . this will solve your other queries as well.
Regards,
Jay.
|
|
|
|
|
Hello,
i have a problem with the char typecast's or the string.toCharArray() function. when i have an extended ascii char in my string like "‹" c# converts this into the char with the integer 8249. BUT the integer i need is 0139, which is the same in HTML (http://www.netstrider.com/tutorials/HTMLRef/ASCII/[^]).
how can i solve this problem most simply? i don't wanna check every char if it's greater than 255.
|
|
|
|
|
I'm working on a fairly large program that is a graded part of my university course
all was going well until i tried to do the remoting and now i'm totaly stuck the book's i've looked at demonstrate only simple problems and don't even mention the problems i'm having
heres the main one i'm having difficulty understanding
for(int i=0;i
|
|
|
|
|
try{}catch{} wont catch any exceptions as I learnt only a few days back. Although this is acceptable to the compiler, it wont catch any exceptions unless specified.
Hope its the problem you having. Your code screwd up the formatting.
|
|
|
|
|
there is nothing to try catch as far as i'm aware, the code seems to be working but i'd don't understand why i can do X = Y and then have X.col diferent to Y.col hope that clears up any misunderstandings
and as i've got you email i'll send a copy of the code if you can't help for what ever reason i thank you for your time
|
|
|
|
|
I suggest u make Galaxy also inherit from MarshalByRefObject.
|
|
|
|
|
That was the first thing i tried but that causes a
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed.
and i don't know how to get round it.
|
|
|
|
|
this
for(int i=0;itry{
GetPlayer(i).Gal.Systems = this.Gal.Systems;
}
catch{
players.RemoveAt(i);
i--;
}
}
used to be
for(int i=0;itry{
GetPlayer(i).Gal = this.Gal;
}
catch{
players.RemoveAt(i);
i--;
}
}
but i kept geting security exceptions saying the objref was unavalable and the cahnge seemed to be the only thing that fixed it, though i don't see why there was a prob in the first place as gal is an instance of
[Serializable]
public class Galaxy{
...
}
which i thought dealt with reading it for transmission
|
|
|
|
|
Hi,
I created a new class(AdvListViewItem) that inherits from the ListViewItem. This class replaces certain base class methods/properties with identical methods/properties except that the new methods/properties fire an event when they are run. However, when accessing the new method/property through the listview (ie. listview1.SelectedItems[0].Font), the methods/properties of the base listviewitem are run.
Do I have to replace the SelectedItemCollection also for my code to work?
|
|
|
|
|
You should probably override CreateControlCOllection or something like that. Look at the protected methods.
|
|
|
|
|
I realized that when I was adding the AdvListViewItem to the listview through the Items property, it was casting the AdvListViewItem to the standard ListViewItem. So I want to replace the ListViewItemCollection class represented by the Items property with a derived one that replaces the Add method with one that accepts an AdvListViewItem. The problem is I don't know how the collection stores the internal ListViewItem objects.
How do I add to the internal collection, or do I have to recreate the class completely (including my own collection)? The ControlCollection does not allow me to add listviewitems to it.
|
|
|
|
|
Anonymous wrote:
The ControlCollection does not allow me to add listviewitems to it.
Of course it won't - that's only for Control instances, which ListViewItem s are not.
You could always add your derivative ListViewItem s to the base class's Items properties, using base.Items and calling the appropriate actions. Since you hide that member, your Items collection property will be called only when referencing your type by its actual Type (and not ListView ), but nothing is preventing you from using the base class's collection property to store the items. After all, your derivative class is still a ListViewItem a la polymorphism.
Search CodeProject. There are some articles that discuss how to do this, as well as some examples I remember seeing on MSDN[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi
Is there any way (C# or API) to get the icon associated with a given file type?
I've looked to SHGetFileInfo but this only works if I give a path for an existing file.
Thanks
|
|
|
|