|
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
|
|
|
|
|
How can i package a project (included SQL database)?
Thanks!
|
|
|
|
|
Do you mean how can you create an installer that will install database objects?
Here is a pretty good overview of how to create an installer with a custom action assembly: http://www.c-sharpcorner.com/Code/2003/Dec/CustomInstallMG.asp[^] . It is in the custom action assembly that you will add your code to run a script to generate database objects (I'm assuming you're using Sql Server). You can just include a .sql script in your installation project and open that file and run it with ADO .NET during the installation process.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
I can't open your link. Can't you check it for me.
Thanks a lot!
|
|
|
|
|
I tried to just change the link in the original message, but it wouldn't work so here it is again: http://www.c-sharpcorner.com/Code/2003/Dec/CustomInstallMG.asp[^]
There was a trailing slash for some reason.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
The link works now
Thanks a lot!
|
|
|
|