|
Hi there,
I'm a newbie in C# and comming from C++/MFC.
I'm building my first application in C#.
I have created a class derived from Object
public class Block : Object
{
Block()
{ Initialize(); }
public bool Initialize()
{ Debug.WriteLine("Block.Initialize"); ... }
...
}
As the array is a 2D array of Block, I have instanciated the arry like that:
Block[,] board=new Block[m_nHeight, m_nWidth];
while default values for m_nHeight is 20 and m_nWidth is 10.
When I look at the trace, I see only 0 calls for the method Block.Initialize.
How can it be possible? Are all the Block object created when I do "m_board=new Block[m_nHeight, m_nWidth];"??? or must I browse the array and call m_board[i, j]=new Block()? If all blocks are created, I should get 200 lines of "Block.Initialize"...
Can anybody explain what's wrong?
Best regards.
Thanks.
|
|
|
|
|
First of all, you don't need to derive from Object . If you don't specify a base class, Object is assumed. This just looks bad.
Second, you're not creating a new instance of Block , only a new Block[] array (which is of type Array ). You won't create a new instance of Block until you use new :
Block[,] board = new Block[m_nHeight, m_nWidth];
board[0, 0] = new Block(); Find more information about arrays at http://msdn.microsoft.com/library/en-us/csref/html/vclrfArraysPG.asp[^].
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi,
Thanks for the answer.
So, if I undertand well, in C#, I need to browse my array and instanciate a "new Block" for each cells of the array right?
|
|
|
|
|
Yes, only it's "iterate" not "browse".
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
so, I do a the following:
<br />
...<br />
m_nHeight=20;<br />
m_nWidth=10;<br />
...<br />
m_board=new Block[m_nHeight, m_nWidth];<br />
for (int i=0; i < m_nHeight; i ++ )<br />
for (int j=0; j < m_nWidth; j ++ )<br />
m_board[i, j]=new Block();<br />
but a strange thing, I have inserted a trace inside the constructor of the Block class, and when I look at the trace, instead of having 200 calls of Block.Block, I have 400 calls 20x10=200...
|
|
|
|
|
You had a trace in your Initialize method before. Are you sure you're not getting 200 lines with "Block.Block" and 200 lines with "Block.Initialize"? If not, I'd really need to see the rest of the Block class code and the full creation block in which you gave a fragment of above.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
I have rebuild the code, and indeed, there are 200 calls now
Thanks
|
|
|
|
|
Hi!
I use RemotingConfiguration.RegisterWellKnownClientType(typeof(type),url) to register a type as a wellknown client type. Is it possible to unregister this type to register it again with another url (or to change the url for the registered type) without restarting my application?
|
|
|
|
|
First, let me advocate that a well-known client Type can't be well-known if you change its URI.
That being said, you can use RemotingServices.Disconnect . If you have a reference to the registered object, just pass it to the static Disconnect method. If you don't have an instance of it available, you can get one using other methods of the RemotingServices class.
Registering it with another URI is as simple as what you've already done.
Another way is to simply shut down the .NET Remoting host, such as a Windows Service, IIS, or some other program, but that's most likely not what you want in this case.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Hi!
Thanks for your effort!
Unfortunately doesn't work it...
I want to explain the situation more detailed:
When the client starts his application, he get's a login-dialog, where he has to type in the servername (and username, password ....) he wants to connect to. When the client types in a servername, that does not exist or the server is not available, he must get a new chance to login, without restarting his application.
When he clicks the Login-Button I do something like this:
<br />
Dispather MyDispatcher;<br />
RemotingConfiguration.RegisterWellKnownClient(typeof(Dispatcher),URL);<br />
MyDispatcher=new Dispatcher();<br />
MyDispatcher.Login(...);<br />
...<br />
If an error occures (severname incorrect or server not available) I have to unregister this Dispather-object first, before I can register it again with another URL. RemotingServices.Disconnect(Dispatcher) does not work, because it's for Serverobjects. And Reregistering (with RegisterWellKnownClient ) throws an exception ("System.Runtime.Remoting.RemotingException: Es wurde versucht, die bereits umgeleitete Aktivierung des Typs 'Dispatcher, ccShared' erneut umzuleiten.").
What can I do?
Thanks in advance!
|
|
|
|
|
I found a solution for this problem by myself!
My Server marshals the Serverobject (RemotingServices.Marshal ) and my Client connects that object (RemotingServices.Connect ).
|
|
|
|
|
Hi, I'm seeking a way to emulate "Send To" function.
I just need to pass a file to .MAPIMail (it can pass a file to the default mua). .MAPIMail is normally located in C:\Documents and Settings\Default User\SendTo\ but doesn't have to use them. Simply prepare a file named like foo@test.local.MAPIMail (zero-byte) and drop a file with explorer. It'll attache the file to the default mua.
If anyone knows about this, please let me know. I'm willing run the default e-mail client and a file attached.
Thanks in advance.
|
|
|
|
|
I want Send To function for one of my C# projects too and in that time I couldn't find a .NET way for it, so I wrote a Shell Extension which do some task for my SEND TO function and register it with my application. But as I'm thinking now , those steps are doable in .NET too(not sure). This[^] article show you the VC6 way. If you couldn't translate it to .NET at least you can make that dll in VC6 which handle your job.
Mazy
No sig. available now.
|
|
|
|
|
Thanks Magy. So, it sounds like there is no way in C# and I at least need to import a dll.
|
|
|
|
|
For more help on Shell in C# you could search this site too.
Mazy
No sig. available now.
|
|
|
|
|
Hi,
I have the problem to get the selected text of a TreeNode. I want to put the selected text of a TreeNode into the clipboard. But I found no solution to get the selected text.
Is there anybody who has an idee?
Regards
Frank
|
|
|
|
|
When you say selected text you mean the user has "highlighted" a portion of the node label with the mouse and you want that higlighted text only ?
|
|
|
|
|
Yes, I mean (like in TextBoxes), the part of the text which is selected (with the mouse or with the keyboard).
Only when the TreeNode is in edit mode (SelectedNode.IsEditing).
|
|
|
|
|
Have your tried casting the selected node as TextBox and using the SelectionStart and SelectionLength properties ?
|
|
|
|
|
Another not-so-simple resolution would be to create your own TextBox and handle the edits yourself.
private void tvTestTree_BeforeLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
e.Node.EndEdit(true);
TextBox MyEdit= new TextBox();
Rectangle IRect = e.Node.Bounds;
MyEdit.Location= new Point(IRect.X,IRect.Y);
MyEdit.Width=IRect.Width+20;
MyEdit.Height=IRect.Height;
MyEdit.Text=e.Node.Text;
tvTestTree.Controls.Add(MyEdit);
}
You will probably have to add an event handler for the TextBox to close it and get the selected text...
|
|
|
|
|
Sorry, I had to work.....
Thank you both for your ideas. I'll try both of your solutions. I thought already at this. But I hoped to get an easier solution.
Whatever thanks for your efforts...
Frank
|
|
|
|
|
You can't cast a TreeNode to a TextBox .
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Sorry replied to myself....
Another not-so-simple resolution would be to create your own TextBox and handle the edits yourself.
private void tvTestTree_BeforeLabelEdit(object sender,
System.Windows.Forms.NodeLabelEditEventArgs e)
{
e.Node.EndEdit(true);
TextBox MyEdit= new TextBox();
Rectangle IRect = e.Node.Bounds;
MyEdit.Location= new Point(IRect.X,IRect.Y);
MyEdit.Width=IRect.Width+20;
MyEdit.Height=IRect.Height;
MyEdit.Text=e.Node.Text;
tvTestTree.Controls.Add(MyEdit);
}
You will probably have to add an event handler for the TextBox to close it and
get the selected text...
|
|
|
|
|
This should work, but you can also get the edit control (a TextBox in .NET) that the Tree-View common control uses during label edits by handling the TVN_BEGINLABELEDIT notification message in TreeView.WndProc and then call a P/Invoked SendMessage with the TVM_GETEDITCONTROL :
public class MyTreeView : TreeView
{
private TextBox editBox;
protected override void WndProc(ref Message m)
{
if (m.Msg == TVN_BEGINLABELEDIT)
{
IntPtr hWnd = SendMessage(this.Handle, TVN_BEGINLABELEDIT, 0, 0);
if (hWnd != IntPtr.Zero)
this.editBox = (TextBox)Control.FromHandle(hWnd);
}
else if (m.Msg == TVN_ENDLABELEDIT)
this.editBox = null;
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
int lParam);
private const TVN_BEGINLABELEDIT = 0x10c5;
private const TVN_ENDLABELEDIT = 0x10c4;
private const TVM_GETEDITCONTROL = 0x110f;
} This way, you're not overlapping controls which can lead to problems, and you'll have to handle all the getting and setting of text.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
This method is what I was referring to when I suggested casting the TreeNode edit control to a TextBox, except you said it correctly, I didn't. My Appologies....
|
|
|
|