473,790 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

In C#, how do I code 'this = that'?

I want to make a MenuItem that I can 'click' programmaticall y. I
started by coding this:

class ClickableMenuIt em : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuIt em( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuIt em to take
all of its fields and events from MenuItem upon construction.

How would you do this?

Aug 21 '06
14 1941
On 2006-08-21 12:49:18 -0400, Andreas Mueller <me@privacy.net said:
Are your custom controls and the containing controls in the same
project? The VS2003 designer had problems with that, so the VS2005
designer might have them, too. It typically happened when I had a user
control and one that contains it open inside the designer at the same
time.
The solution was to factor the user controls into another library.

Also, whenever you encounter such an behavior, don't save anything,
close all open windows then and close VS. Then delete the bin and obj
folder. Reopen VS and recompile the solution. Should work fine again.

Its not a perfect solution, but I can live with this workaround since
VS2002 :-(
They are in a separate project within the same solution. I made a
mistake when I said the designer would not display the control or forms
that contain it. Typically, the control will display just fine in the
designer when I look at it and it will comple and run without a
problem. It is when I put the control on a form that the form becomes
unusable in the designer.

Thanks!

Aug 21 '06 #11
Charles Jenkins a écrit :
On 2006-08-21 10:38:52 -0400, Mathieu Cartoixa
<mathieu.cartoi xa@_NO_hotmail_ SPAM_.comsaid:
> Have you checked the method MenuItem.Perfor mClick in MSDN ?...

I am using the .NET Compact Framework, which seems to be designed for
frustration, becaue the method you NEED is never available. For
instance, MenuItem.Perfor mClick() does not exist in the Compact Framework.
I must admit I never used the Compact Framework. PerformClick seems to
be available for buttons, but not for menu items in this world :-(
<snip>

Because the VS 2005 designer is not reliable for custom controls, I am
trying to put plain old controls (with no visual inheritance) onto my
forms, then add the behaviors I need at runtime. I need
ClickableMenuIt em to inherit from MenuItem so that it can call
OnClick(); but I need each new ClickableMenuIt em to get its state from
the MenuItem that is actually available. I want to be able to do
something like this:

// Use what we have to create what we actually need
ClickableMenuIt em cmi = new ClickableMenuIt em( menuItem );
cmi.DoClick();
Well, except with Delphi, I have always been disappointed with
designers. The code they ususally produce is such a mess that they end
up being unable to deal with it after a while...

> * isolate your ClickableMenuIt em in a library and add it to the
designer controls. Use inheritance. The constructor would have no
parameters, and you could use it exactly like a MenuItem.

I think this would require the kind of inheritance that would lead me to
have problems with the designer. In order to add ClickableMenuIt ems
using the designer, I would have to create a ClickableContex tMenu, right?
I do not think so, as with inheritance a ClickableMenuIt em would be a
MenuItem. That reminds me of a hack I have used with VS2003 that should
work with VS2005 : add a MenuItem to your menu vie the designer, save
your files, close the designer, and replace MenuItem with
ClickableMenuIt em directly in the source code for your instance (ther
should be only two occurrences : one in the variable declaration, one in
the method InitializeCompo nents). Reopen the designer : you are done !
>
> * create your ClickableMenuIt em elements "by hand" in your form
constructor, after the call to InitializeCompo nents(). Use
inheritance. You would not want any parameter in your constructor either.
* let the designer create your MenuItem. Then create your
ClickableMenuI tem elements "by hand" in your form constructor, after
the call to InitializeCompo nents(). Use composition (which in this
case seems to be a rather long and fastidious if you do it properly...).

These workarounds still require me to make the ClickableMenuIt em to get
its state (fields and event handlers) from the MenuItem I want to
impersonate. That leads me back to this question. Given that I cannot
say "this = that," how do I copy all the MenuItem's fields and events
from the MenuItem I have into the ClickableMenuIt em that I want?
The first method uses inheritance : you are just not using the designer
for your specific components.
>
Oddly enough, I really *did* attempt to combine inheritance and
composition, just like you guessed, in the hope that an inherited
control could gain access to the protected methods of the member
control's instance---

class ClickableMenuIt em : MenuItem {

private MenuItem m_mi;

public ClickableMenuIt em( MenuItem mi ) {
m_mi = mi;
}

public void DoClick( EventArgs e ) {
m_mi.OnClick( e );
}
}
I have been there before : no matter what you are trying to achieve,
inheritance cannot live with composition (wit the same class involved,
of course ;-). It is always wrong.
But here is your real point : how to simulate the click ? Be careful
that OnClick just triggers the handlers registered to the Click event,
it does not perform any MenuItem specific action associated with the
click... The best solution if you want to call OnClick anyway is to use
inheritance and just :

public void PerformClick() {
this.OnClick(ne w EventArgs());
}

What if OnClick is not enough ?... I would try to use a reflector to
look deep into the MenuItem class (you can find one there :
http://www.aisto.com/roeder/dotnet/), and look for a private method that
would look like a PerformClick. Then I would call it using reflection,
which is a secret way to bypass the laws of method accessibility
(beuaark)...
Last thougt : I think that if you really had separated UI code from the
rest, you would not have felt this need to call a method that does not
exist... Let me explain that : what I think you did is something that is
very usual thanks to designers, I mean add a handler to your MenuItem,
and perform your actual actions in there

public void MyForm_myMenuIt em_Click(object sender, EventArgs e) {
// related actions...
}

So now you will want to programmaticall y click the myMenuItem to
perform the actions... Try instead to separate the code related to your
actions in another layer (most likely in another class)

public void RelatedActions( )
{
// some code here
}

and just replace the preceding with :

public void MyForm_myMenuIt em_Click(object sender, EventArgs e) {
RelatedActions( );
}

Now, can you not try to call RelatedActions directly instead of trying
to click the menu ?
>
Of course, that does not work. I can call OnClick() in my own instance
(where it would do no good) but not in m_mi (which has the handlers I
want to fire). That approach would have been wrong and very wasteful,
but if it had worked, I could have lived with it.

Have a great day, and thanks!
Aug 22 '06 #12
Patterns are fun, but watch out for the observer. That one can be dangerous.
:)

I find that I can "re-align" my designer if I re-inherit from a standard
windows component, rebuild, view in designer, close windows, then switch
back the inheritance and rebuild.
Failing that, I reset the toolbox, remove the reference, build (watch it
whinge about the missing reference) then re-add the assembly into the
toolbox and then it works.
Failing ALL of that, I create the real thing with windows control, detach
the windows control at runtime and use reflection to copy all the designer
settings and inner controls across to my inherited version.
You can't be bothered with that you could start up a brand new .sln and
possibly even .csproj and copy the files back into the new projects and
solutions (but the speed of that depends on how much stuff you have).
Just to double check, you are aware that all designer components must have
default parameterless, constructors, right?

God the designer can suck sometimes..... but it can work (e.g. it's not
completely broken), you just have to be persistent with it.

I concur that the .NET Compact Framework sucks in comparison, but apparently
older devs tell me it's better than what they previously had (so WTF was
that like????).
My favourite is the great exceptions you get:

System.Exceptio n
An exception occured

NO STACKTRACE. Arggghhhh!

Anyway, good luck.

Simon

"Charles Jenkins" <no************ @not.here.comwr ote in message
news:2006082112 423043658-notmyaddress@no therecom...
On 2006-08-21 12:13:55 -0400, "Greg Young"
<dr************ *******@hotmail .comsaid:
Aside from Mathieu's great point on "PerformCli ck" ...

It seems that what you are trying to do is a decorator pattern (perhaps
a look at the pattern will clear thing sup a bit?)

I have a book on C# Design Patterns, so I will do just as you say and
read up on the Decorator pattern.

Thanks!

Aug 22 '06 #13
Hee hee! Thanks for the advice. Maybe I oughtta seriously think about
changing careers

On 2006-08-22 16:31:11 -0400, "Simon Tamman"
<i_************ *************** *******@NOSPAMh otmail.comsaid:
Patterns are fun, but watch out for the observer. That one can be dangerous.
:)

I find that I can "re-align" my designer if I re-inherit from a standard
windows component, rebuild, view in designer, close windows, then switch
back the inheritance and rebuild.
Failing that, I reset the toolbox, remove the reference, build (watch it
whinge about the missing reference) then re-add the assembly into the
toolbox and then it works.
Failing ALL of that, I create the real thing with windows control, detach
the windows control at runtime and use reflection to copy all the designer
settings and inner controls across to my inherited version.
You can't be bothered with that you could start up a brand new .sln and
possibly even .csproj and copy the files back into the new projects and
solutions (but the speed of that depends on how much stuff you have).
Just to double check, you are aware that all designer components must have
default parameterless, constructors, right?

God the designer can suck sometimes..... but it can work (e.g. it's not
completely broken), you just have to be persistent with it.

I concur that the .NET Compact Framework sucks in comparison, but apparently
older devs tell me it's better than what they previously had (so WTF was
that like????).
My favourite is the great exceptions you get:

System.Exceptio n
An exception occured

NO STACKTRACE. Arggghhhh!

Anyway, good luck.

Simon

"Charles Jenkins" <no************ @not.here.comwr ote in message
news:2006082112 423043658-notmyaddress@no therecom...
>On 2006-08-21 12:13:55 -0400, "Greg Young"
<dr*********** ********@hotmai l.comsaid:
>>Aside from Mathieu's great point on "PerformCli ck" ...

It seems that what you are trying to do is a decorator pattern (perhaps
a look at the pattern will clear thing sup a bit?)

I have a book on C# Design Patterns, so I will do just as you say and
read up on the Decorator pattern.

Thanks!

Aug 24 '06 #14
You put a lot of time and thought into that, Matheiu, and I really
appreciate it. Thanks for helping me.

I will have to print out your reply and go over it very carefully.
Intuitively, I think your clues about better separating my actions from
user interface stuff are great points that may really help me as I
solve this problem and others in the future! :-D

Aug 24 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2212
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes is: > def f(this:that=other): > print this This means that f() has a 'this' parameter, of type 'that'. And 'other' is the default value.
8
1430
by: Bernd Fuhrmann | last post by:
Hi! I just tried to write a little program, but GCC (Mingw 3.3.1) refused to compile it. So I'd like to know if there's something wrong with my code or if it is a bug in GCC. ---snip--- namespace Namespace {} template <class TSomeType>
0
1636
by: Chua Wen Ching | last post by:
Hi there. I had couple of Java codes with me that i not sure how would i code this in C#. I always use jcreator without ide. So a bit confuse over c#. Okay!
2
8464
by: Chua Wen Ching | last post by:
Hi there. I had 3 questions to ask. 1) Public Event ButtonClicked(MessageBoxNumber As Integer, OKButton As Boolean, CancelButton As Boolean, ExitButton As Boolean)
3
2066
by: joshblair | last post by:
Hello, Has anyone ever seen or created such a code generator? I'm looking for a sample of a code generator that will generate code (preferably one that uses C# and the XMLTextWriter) to create an XML document structure based on an XML file as input. I have to build some classes that allow me to generate some very complex/large/nasty XML documents for use in B2B exchange of data (like
5
1438
thatos
by: thatos | last post by:
The following program was supposed to read a number of years from a file called "friday.in" then print out how many of the thirteenth were on each day from monday to sunday on to a file called "friday.out". Here is the code which I used . import java.io.*; import java.util.*; public class friday{ public static void main(String args) throws IOException{ BufferedReader in = new BufferedReader(new FileReader("friday.in"));...
9
1223
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, please reference: http://pastebin.com/mef78b3d As shown in the reference I am dynamically building my controls in my class. I'd like to be able to attach javascript calls as attributes to each of my controls if applies. for example, String varString = "alert('i'm a textbox')";
3
2595
by: Al Dykes | last post by:
Can some post a Python code fragment that will to make a PC with Skpye installed to make a Skype call, given a valid phone # string. I'm not asking for code that handles the audio once the connection is made.
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.