|
desmond5 wrote:
The base of this toolbar is a Control (or UserControl if you will).
That would be incredibly inefficient with too much overhead. A UserControl is a container control, used for hosting other controls like a Form hosts controls. This should not be used for single objects.
Even more advanced toolbars that seem to host other controls like a combo box are not container controls. They typically "site" (position) those controls at a particular location and move them when the control itself is moved.
desmond5 wrote:
Any ideas how is the mouse handling done in the Control class ?
Much of the mouse interaction is not handled by the Control class at all. Almost every Windows Forms control in the BCL encapsulates the related Windows Common Control - the very same native controls you see throughout Windows and Windows applications. This gives a consistent user interface whether your application is writtin in C/C++ using the Win32 APIs, MFC (C++), VB, or .NET (or a slew of others, excluding Java that wrote it's controls from the ground up for consistency across platforms, which is arguably less important that a single platform where most users spend their days).
If you want to see the implementation for yourself, learn to read Intermediate Language (IL) and use the ildasm.exe (IL Disassembler) application that ships with the .NET Framework SDK and is located in the Bin directory of the .NET Framework SDK installation. This is installed by default with VS.NET under the VS.NET installation root.
You could also use a decompiler like the .NET Reflector[^].
For your approach where you're defining everything in managed code, a more common approach is to have each UIElement implement a Click handler like so:
public class UIElement : IDisposable
{
public abstract void OnClick(MouseEventArgs e);
} Override OnMouseDown , for example, on your toolbar control and pass the MouseEventArgs passed to you to the UIElement at those client coordinates (i.e., 0,0 corresponds to the upper-left corner of the control that was clicked - not the screen). That will allow you to get any relative mouse click point for the element as well as any modifier keys.
A better design would be to encapsulate only the properties of the MouseEventArgs you want, which makes this easily callable for key events (like pressing the [Space] key when the UIElement has the focus, if you implement focus within the toolbar itself).
Regarding your naming conventions you should really read Naming Guidelines[^] in the .NET Framework SDK. Methods (not functions, mind you) should never be lower case, nor should properties. Fields may be, as it really doesn't matter how you name private fields (no one should see them anyway, but take into account how they're serialized, implementing ISerializable for custom serialization if needs be).
It's important to follow the guidelines as it makes developing applications using your library easier when it's consistent with the other libraries a developer would be using. Just think of all the classes, members, etc. that you use from the BCL? Have you ever seen a method that begins with a lower-case letter? A property, or a class? Not in the BCL (base class library) you won't.
It's good to run your assemblies through FxCop[^], which will not only catch naming violations (not typically bad violations, but following the guidelines is helpful for your clients) but many other errors as well.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
There is a method of the DataTable object that I require to access from various methods of my class basically to retrieve the column name as a specific index.
I have created a private property in my class to access this method and defined it similar to as follows:
<br />
private string ColumnName<br />
{<br />
get {return DataTable.Columns[ColumnNumber].ColumnName.ToString();}<br />
set {DataTable.Columns[ColumnNumber].ColumnName = value;}<br />
}<br />
Is this acceptable practice?
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
LearnVisualStudio.Net
|
|
|
|
|
This is certainly "acceptable", however, you might ask yourself "what is the benefit?". Considering that your DataTable and ColumnNumber fields appear to be instance variables, there's no reason when you need that specific column name you can't just call it directly as in DataTable.Columns[ColumnNumber].ColumnName.
If what you want is a short cut method, create some methods like this (that aren't properties).
private string GetColumnNameAt( int index )
{
if( DataTable.Columns[index] != null )
return DataTable.Columns[index].ColumnName;
}
private void SetColumnNameAt( int index, string name )
{
if( DataTable.Columns[index] != null )
DataTable.Columns[index].ColumnName = name;
}
Let me know if you need clarification.
Best Regards.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
confused:Hi everyone,
I´m trying to serialize some objects in C# using BinaryFormatter. The problem is such that multiples objects being serialized make reference to a same object and I don´t know how to map these references so that I can have only one copy of the object being referenced.
Does anyone know a way to get through it? Is there some facility in C# that handles it?
Thanks.
|
|
|
|
|
If you put all the objects in a Hashtable or a set and serialize the whole thing, only one copy of each object will be serialized.
Additionally, I like to write my code so that I can easily change the serialization to use a BinaryFormatter or the SoapFormatter. That makes it easier to verify your assumptions about how things are being serialized (to some extent).
Matt Gerrans
|
|
|
|
|
I'm writing a program that will populate a TreeView with a list of all the "programs" that can be run from within my program. These "programs" will actually just be MDI children. The name of the MDI child form will be loaded into the TreeMenu.SelectedNode.Tag property.
My problem is to be able to launch a form dynamically based on which item they double-click on.
Any ideas?
--In a world without fences, who needs Gates?
|
|
|
|
|
SignMan359 wrote:
My problem is to be able to launch a form dynamically based on which item they double-click on.
You can handle the AfterSelect event from the TreeView . This will allow you to interogate the TreeNode that issued the event. Something like the following will do it:
tv = new TreeView();
tv.AfterSelect += new TreeViewEventHandler(tv_AfterSelect);
private void tv_AfterSelect(object sender, TreeViewEventArgs tvea)
{
TreeNode node = tvea.Node;
if(node != null)
{
}
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
I'm sorry, maybe I confused the issue. I'm not having a problem with the TreeView. Here is where my problem is:
Let's say you have a form named HelloWorld, you would get that for to show up by typing something like:
HelloWorld newForm = new HelloWorld();
newForm.Show();
My problem is how do I get the same result of it was more like this:
string formName = "HelloWorld";
formName newForm = new formName;
Yes, I know this is totally invalid syntax, but I think it explains what I am trying to do. does anyody have any idea how to do this correctly?
--In a world without fences, who needs Gates?
|
|
|
|
|
In this example I am storing the Mdi's type in the node's tag. You could set the text of ur node to be the mdi form's classname instead if it were a valid solution.
Type type=Type.GetType((string)selectedTreeNode.Tag);
Form mdiChild=(Form)Activator.CreateInstance(type);
P.D. Activator.CreateInstance() can pass arguments to the object to instantiate constructor if needed. Just check the msdn information about the different overloads
|
|
|
|
|
I've tried the above. I have the name of the class (and therefore the form) in the .tag property. However, when I try to run this code, it leaves the type variable null.
I'm at a loss. I'm pretty new to C# and .Net, so please forgive my ignorance if this is a simple problem.
--In a world without fences, who needs Gates?
|
|
|
|
|
Put the whole name of your type, for example: MyApplication.MyObjects.MyType (Check the namespace of ur type.)
|
|
|
|
|
I have two datagrid controls and I want to syncronize the scrolling between them. I can find the HScrollBar for both datagrids by searching through their Controls collection and I can intercept the scroll events of the datagrids. In the scroll event I try and set the HScrollBar.Value of one scroll bar to be equal to the other. The problem is that while this does, in fact, move the scroll bars, the datagrid (the one I'm trying to force to scroll) does not get redrawn. I tried using Refresh() and Invalidate() methods of the datagrid, but neither of them seem to work. In the application if you click on the scroll bar that was moved programatically the datagrid will then update to reflect the new position of the scroll bar.
Does anybody have any idea on how I can make this work?
<br />
private void grid_Scroll(object sender, EventArgs e)<br />
{<br />
this.hsbLower.Value = this.hsbGrid.Value;<br />
}<br />
Thanks
|
|
|
|
|
I've been reading about virtual functions and I keep seeing code like this in the examples:
Dimensions c = new Circle(r);
The Circle class is derived from Dimensions class.
I understand the benefits of inheritance but I don't understand why you would declare a circle as a Dimensions class. This seems to be a common practice, too. The following three articles from MSDN all do it but I don't understand why. Are they just trying to demonstrate which method would be called under these special declaration circumstances?
Why not do this:
Circle c = new Circle(r);
1) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfvirtualpg.asp
2) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkversioningtutorial.asp
3) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_10_5_3.asp
|
|
|
|
|
public Circle(double r): base(r, 0)
{
}
This is the reason why it initialize the circle...
Its constructor , construct a new object by sending values to base class..So there is no difference between the two way
public virtual double Area()
{
return x*y;
}
and by declaring the virtual method , all children classes will implement their own Area method.. by overriding..
public override double Area()
{
return 2*pi*x*x + 2*pi*x*y;
}
In the first article , if you be more careful , each children has its unique area method , but with same commmon members
|
|
|
|
|
I suspect this is just one of those practices that is common in book examples demonstrating some aspect of inheritance, but has very little practical application. I agree, just say Circle c = new Circle(r); .
my blog
|
|
|
|
|
I'll have to disagree with that. Through inheritance u can get polymorphism which comes handy in thousands of code situations.
Another simplified example:
Imagine ur making an inventory of all kind of diferent InventoryObjects (big mall with all kind of different stuff on sale). Ok lets create a base class IventoryObject and a public property named InventoryId.
Then we would create all the kind of different objects derived form the base InventoryObject that implement all the diferent details of our real objects which could be very diferent in nature and thus require very different abstractions (for example Books, Hardware, VacuumCleaners, etc)
Now we want to iterate through our whole collection of objects looking for certain InventoryIds. We would do that iterating through our stronglytyped InventoryObjectCollection and getting the common InventoryId property. (The collection would obviously be stronglytyped to InventoryObject and as all our real objects are derived from this class, all casts would be succesful)
Otherwise u would have to keep stronglytyped collections of all our different object types and iterating through each of them.
Of course this can also be done also through Interfaces and nobody discusses their usefulness:
public Interface IIdentifiableObject
{
int InventoryId{get;}
}
But inheritance is already giving u this feature for free.
|
|
|
|
|
Hi all,
I am writing a server program for multi-client access in C#,
the server will update, delete, add object onto the database.
I am not quite sure the proper method to handle multi-client access.
There are object layer and database layer in the program, when two clients want to update the same object A to the database via the object layer at the same time, should the synchronization of object be
implemented at the object layer or the database layer ?
What are the considerations to choose which layer to synchronize ?
Are there any reference for these kind of programming ?
Thanks
Patrick
|
|
|
|
|
If you're using Sql Server for the database and two people update at the same time, the second person's update will happily over write the first person's update.
If you have two people working on the same record at the same time in your user interface and you want to ensure when the first person updates the record, that the second person gets notified that there has been a change made since starting to work with the record themselves, then you would probably want to put that into your business logic (object layer). You should put a last update date column into your database table that is being updated and check it before calling the update. You should also save the date/time that the record was opened and check that against the last update on the record. If it has been updated since the current person first accessed it, then you should prompt the user to determine if they want to see what has changed and/or if they want to overwrite it.
Hope that helps.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Hi and a happy new year!
I like to get the application path (startup path) of my console application (with a windows form it is no problem because I am having the Application object).
How can I do this?
Thank you for your help!
senfgurke.
life is very short and there is no time for fussing and fighting my friend.
|
|
|
|
|
Ah, ok, I found it:
using System.IO;
...
string AppPath = Directory.GetCurrentDirectory();
...
life is very short and there is no time for fussing and fighting my friend.
|
|
|
|
|
From MSDN:
"The current directory is distinct from the original directory, which is the one from which the process was started."
You can change the current directory by setting the CurrentDirectory of the Environment class. This would obviously give you false results if you're trying to get the path to the executing or entry assmebly.
Use Assembly.Location to get the physical path to the assembly in question. You can use Assembly.GetEntryAssembly to get the entry assembly or Assmebly.GetExecutingAssembly to get the assembly from which the current code is executing.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Thank you Charlie.
Yes, I think its the best way.
Just let me add which namespace has to be included:
using System.Reflection;
I also found code at
http://www.devtrain.de/artikel_908.aspx[^]
Senfgurke.
life is very short and there is no time for fussing and fighting my friend.
|
|
|
|
|
Hi all,
I often use "like" operator in VB.NET in my pattern comparision.I spent my time in finding the equivalent in C# but I don't know how.Could anyone tell me how.Thanks for your help!
|
|
|
|
|
You could use code this:
Console.WriteLine( ("TESTING".ToLower().IndexOf("sti") > 0) );
But when you work with patterns, you really should use Regular Expressions, if you are not familiar with Regex, then this should be a good reference: http://www.codeproject.com/useritems/RegexTutorial.asp[^]
"After all it's just text at the end of the day. - Colin Davies
"For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
|
|
|
|
|
Hi
Is there any problem if 2 threads use the same database SQL Server CE 2000 connection do transaction at the same time?
If 1 threads capture the right to do the transaction, then is the other thread will wait for access or what others happen?
Thanks
|
|
|
|