C# event

Associate
Joined
30 Dec 2005
Posts
415
Just started learning C# yesterday and have already overcome many problems, but its slow work atm! Just wondering if there's an event in C# which is called upon when other form dialogs are closed and it is the active form...I suppose its on focus really...

I've tried this, but it doesn't seem to work. :(
Code:
        private void Main_Activated(object sender, EventArgs e)
        {  
            message.Text = message.Text + msgtext + " ";
            msgtext = null;
            this.message.Focus();
        }

Can anyone point me in the right direction? I basically need to run the code above each time all the other form dialogs are closed..

Cheers,
Rich
 
Doing what lol?
Sorry i'm completely new to this language.

I'll explain the scenario.

Basically I have a form called main and a form called responses.
main opens the responses form, which opens in front of main and you cannot access main until responses is closed (modal I think its called)...

The user picks a message in the responses form, which I want to then appear in the text box 'message' on the main form. When I tried to access main.message.text from the responses form, it says it didn't have permission to do so...

So what I was going to do was save the message from the responses form in a global variable called msgdump, which is then put in the main.message form box each time main is focused.

Obviously the first method/attempt was best, but I couldn't get round the permission bit..
 
Ok, 2 things:

1) You can't just define a method and expect it to magically be associated with the event. You need to go into the forms designer, into the properties window and click the little lightening symbol. This opens up the events list for that form. Here, you should be able to see every event that you can make use of. Find the event you need and if you've already created the method, you should be able to select it from the drop down box. If not, just pressing enter in the blank space should create the event method for you. Once this is done, that method will be run when the event is called.

2) There's a better way to do this. I don't know how much you know about OO, but there's such a concept of a protection level. Basically, this controls what objects/properties/methods can be controlled from other classes. If an object, in your case a textbox, is defined as 'private', it can only be accessed from within that form's class. So you can only do stuff to it from code within the 'main' form class. You need to change it's protection level to allow it to be used from other classes. The opposite of private is 'public' which allows to be accessed from any other class, even if it's not in the same namespace. Other levels are 'protected' and 'internal', but you can read about them on google. The best thing to do for the minute would be to set the textbox to be 'public' ('internal' would work as well) and then it can be accessed from the responses form. As a rule, using global vars to pass data between objects isn't a good idea, as bad things can happen if more than one thing accesses it at once.

The problem you will run into is that while the control is now public, the responses form doesn't know which instance of the 'main' class to access. For all it knows, there could be 1000 'main' objects floating out there, so when you say "Go do something with this textbox", it doesn't know which you mean. A way round this is to send the main object off to the responses object when you create it. I imagine you have a constructor in the responses form:
Code:
public responses() {
}
All you need to do is to create a variable within the responses form of type 'main' and then throw the main value over to it when it's created:
Code:
private main mainfrm;
public responses(main thisinst) {
	mainfrm = thisinst;
}
Then, when you need to update the textbox on the mainform from the responses form, just use the 'mainfrm' object, as that points back to your original object.
Code:
mainfrm.myTextBox.Text = "Hi there";

let me know if you have any issues. :)
 
can you use panels ? basically divs you can show and hide

so you can show respnses and hide main then when youve got whatever answer u want in responses just hide responses and use main again ?

toastyman said:
Doing what lol?
Sorry i'm completely new to this language.

I'll explain the scenario.

Basically I have a form called main and a form called responses.
main opens the responses form, which opens in front of main and you cannot access main until responses is closed (modal I think its called)...

The user picks a message in the responses form, which I want to then appear in the text box 'message' on the main form. When I tried to access main.message.text from the responses form, it says it didn't have permission to do so...

So what I was going to do was save the message from the responses form in a global variable called msgdump, which is then put in the main.message form box each time main is focused.

Obviously the first method/attempt was best, but I couldn't get round the permission bit..
 
Ah, I see. What you should be doing is accessing the data the user entered in response from the main form, not trying to access the main form from the response form.

edit: unless you want the changes to be visible in the main form immediately, in which case growse's code is the way to go (I would normally consider it a little messy however, as modal dialogues should typically be self contained).

For example:
[Main.cs]
Code:
// ...

            Response responseForm = new Response();
            DialogResult result = responseForm.ShowDialog();

            // Do whatever you want with the message...
            string message = responseForm.Message;

// ...

[Response.cs]
Code:
// ...

    public partial class Response : Form
    {
        public string Message
        {
            get
            {
                return messageTextBox.Text;
            }
            set
            {
                messageTextBox.Text = value;
            }
        }

        // ...
    }

When you show the dialogue, execution pauses hangs at the responseForm.ShowDialog() method call, waiting for control to return from the method (happens when the dialog is closed/accepted/whatever). After that, you just get the Message property and do whatever you want with it :)

edit: bummer, beaten by 2 posts :(
 
Last edited:
Wow thanks for those informative posts. I've actually rebuilt the project from scratch as i've learnt a lot over the last few days and decided to restructure and rename items.

The form originally called 'Main' is now called 'messenger'. 'responses' is still called 'responses'.

As I don't need the text to update in the text box till the dialog box has been closed, I decided to use Inquisitors example. However, it threw up a few errors...


The code on responses.cs
Code:
   public partial class responses : Form
    {
        public responses()
        {
            InitializeComponent();
        }
        public string Message
        {
            get
            {
                return message.Text;
            }
            set
            {
                message.Text = value;
            }
        }
  etc...


The code on messenger.cs
Code:
        private void btn_responses_Click(object sender, EventArgs e)
        {
            Response responseForm = new responses();
            DialogResult result = responseForm.ShowDialog();
            string message = responseForm.message;
        }


This threw up the following errors:
Error 1 The type or namespace name 'Response' could not be found (are you missing a using directive or an assembly reference?) D:\Visual Studio 2005\Projects\Routy Helper\Routy Helper\messenger.cs 67 13 Routy Helper

Which is this line:
Response responseForm = new responses();


Error 2 The name 'message' does not exist in the current context D:\Visual Studio 2005\Projects\Routy Helper\Routy Helper\responses.cs 22 24 Routy Helper

Which is this line:
return message.Text;


Error 3 The name 'message' does not exist in the current context D:\Visual Studio 2005\Projects\Routy Helper\Routy Helper\responses.cs 26 17 Routy Helper

Which is this line:
message.Text = value;
 
Last edited:
Your first error is caused by you trying to define your responseform as type "Response", which doesn't exist. You're creating it as a "responses" object, so define the variable to be a responses object as well.

Code:
responses responseForm = new responses();

Your second and third error are caused by you accessing the wrong thing. Within responses, "message" is (I'm guessing) a private textbox, which is therefore invisible to anything else. "Message" is a public string property which you've nicely defined to be the text value of message. Try accessing responseForm.Message instead.

Intellisense (that box thta pops up when you're typing) pretty useful here. If it doesn't pop up when you're filling out a method or property name and you think it should, chances are that something's wrong.
 
The code I provided was just an example of how you'd do things, so you'll have to adapt it to use your own type and member names.

To address the errors individually:
  1. While you changed new Response() to new responses(), you didn't change the first occurance of Response in the variable's declaration. It should read responses responseForm = new responses();.
  2. My code assumed that there was a TextBox in the form called messageTextBox (message in your case), so you'll have to create one for this to work.
  3. See above.

Hope this helps :)
 
Last edited:
Back
Top Bottom