How to access the control of a form from diffrent form?
-
In Form2() ========= In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}Thanks:thumbsup:
-
In Form2() ========= In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}Thanks:thumbsup:
ShowDialog()
is a modal operation. Did you mean to callShow()
? /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
In Form2() ========= In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}Thanks:thumbsup:
Have you subscribed to the MyAppForm_MouseLeave event in the some part of the code? If not you need to subscribe to it and then you can use a event handler to handle that event.
-
In Form2() ========= In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}Thanks:thumbsup:
The
ShowDialog
is the problem. While the form is modal and displayed, focus cannot be returned to the parent form.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
In Form2() ========= In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}Thanks:thumbsup:
1. This code as-is will not compile: the new Form 'MyAppFrm' is created inside the Load event: outside of the scope of the Load event it will not exist: thus you cannot 'refer' to it inside the button1 Click EventHandler. The "fix" for that is easy enough: make MyAppFrm a class-scoped variable defined outside the Load Event:
private Form MyAppForm;
//
private void Form1_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.Closing += new CancelEventHandler(MyAppFrm_Closing);
}2. In your original code you can "get away with" accessing MyAppFrm in the MouseLeave event handler: because that EventHandler had a valid reference to the instance of MyAppFrm <i>when it was added in the Load event of Form2</i>. And, after all, you reference it only indirectly by calling 'GetType() on the 'sender parameter of the EventHandler. 3. Note that in the example above I changed the Event in MyAppForm to 'FormClosing, not 'MouseLeave: if you use 'MouseLeave you'll get an initial trigger of that Event just by the call to ShowDialog ... and, as explained below: showing modally excludes "leaving." There are good reasons, at times, to show a Form using 'ShowDialog: but in your code I can't detect any reason why are you using 'ShowDialog. When you use 'ShowDialog: the reason to use it is to prevent any "moving away from it," to block the user from changing focus until the user does something that closes/dismisses the modally shown Form. Suggest you sit down and try and think through and outline your design goals here: what's going to be inside the new Form created (which is just a blank, "vanilla" Form, now). If this is a homework question that really does ask "How to access the control of a form from diffrent form?:" then you should be pre-defining ... at Design-Time probably ... the new Form that is created in Form2, and put some Control on it, and focus on the issue of how you get access to that Control from Form2 ? good luck, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
-
1. This code as-is will not compile: the new Form 'MyAppFrm' is created inside the Load event: outside of the scope of the Load event it will not exist: thus you cannot 'refer' to it inside the button1 Click EventHandler. The "fix" for that is easy enough: make MyAppFrm a class-scoped variable defined outside the Load Event:
private Form MyAppForm;
//
private void Form1_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.Closing += new CancelEventHandler(MyAppFrm_Closing);
}2. In your original code you can "get away with" accessing MyAppFrm in the MouseLeave event handler: because that EventHandler had a valid reference to the instance of MyAppFrm <i>when it was added in the Load event of Form2</i>. And, after all, you reference it only indirectly by calling 'GetType() on the 'sender parameter of the EventHandler. 3. Note that in the example above I changed the Event in MyAppForm to 'FormClosing, not 'MouseLeave: if you use 'MouseLeave you'll get an initial trigger of that Event just by the call to ShowDialog ... and, as explained below: showing modally excludes "leaving." There are good reasons, at times, to show a Form using 'ShowDialog: but in your code I can't detect any reason why are you using 'ShowDialog. When you use 'ShowDialog: the reason to use it is to prevent any "moving away from it," to block the user from changing focus until the user does something that closes/dismisses the modally shown Form. Suggest you sit down and try and think through and outline your design goals here: what's going to be inside the new Form created (which is just a blank, "vanilla" Form, now). If this is a homework question that really does ask "How to access the control of a form from diffrent form?:" then you should be pre-defining ... at Design-Time probably ... the new Form that is created in Form2, and put some Control on it, and focus on the issue of how you get access to that Control from Form2 ? good luck, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
Thankyou Bill, I get clear...very nice reply...:cool: