7 Replies - 1555 Views - Last Post: 13 December 2012 - 06:07 AM Rate Topic: -----

#1 PiscesHunter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-December 12

C# how to hide form using the button from the current active form

Posted 11 December 2012 - 05:52 AM

I need to hide the a form using a button from my current active form. let see for example there is two form open and I am using form2 and then form1 was inactive. i want to hide the form1 using the button from form2..could give me a hint on how to do it guyz//..ty

here is my code

namespace windowsAppication1
{
 public partial class Form2:Form
 
 public Form2()
 {
  initializeComponent();
 }
 private void button1.click(object sender, EventArgs e)
 {
  Form1 f1 = new Form1();
  f1.hide();
 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: C# how to hide form using the button from the current active form

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: C# how to hide form using the button from the current active form

Posted 11 December 2012 - 05:59 AM

Write all that code yourself? Seriously, if you don't even have the forms yet, let alone the button to close it, how have you even tried to accomplish this yourself?

You'll have to set the form's visibility attribute in the button's click event handler. Something like:


protected void MyButton_Click(object sender, EventArgs e)
{
    myForm2.Visible = false;
}




EDIT: First comment was before any code showed up in the OP beyond namespace windowsApplication1{}.

This post has been edited by h4nnib4l: 11 December 2012 - 03:28 PM

Was This Post Helpful? 1
  • +
  • -

#3 PiscesHunter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-December 12

Re: C# how to hide form using the button from the current active form

Posted 11 December 2012 - 06:09 AM

here is my code sir.. i misenter the button so that's why i didnt finish my post..
namespace windowsAppication1
{
public partial class Form2:Form

public Form2()
{
initializeComponent();
}
private void button1_click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.hide();
}
}


This post has been edited by Skydiver: 11 December 2012 - 06:26 AM
Reason for edit:: Put code in codetags. Please remember to do this in the future.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: C# how to hide form using the button from the current active form

Posted 11 December 2012 - 06:29 AM

Your post #3 still looks very much like post #1. Both of which will fail to compile.

Once you do get your code to compile, you will still run into an issue because your line 11 creates a new instance of Form1. You won't be affecting the existing instance of Form1 that you want to make invisible.

h4nnib4l has the correct solution.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5583
  • View blog
  • Posts: 11,943
  • Joined: 02-June 10

Re: C# how to hide form using the button from the current active form

Posted 11 December 2012 - 08:47 AM

Quote

 private void button1.click(object sender, EventArgs e)
 {
  Form1 f1 = new Form1();
  f1.hide();
 }
}


Your code does exactly what you are telling it to do.
You make a NEW INSTANCE of a form, of type Form1.
Then you hide the new instance.

The real problem here is not the code as much as it is your understanding of the fundimentals of the language and objects. It seems clear that you do not yet understand that you make instances of objects. You can make 100 new instances of the Form1 type for example. They are all unique and unrelated. Like making 100 Dodge Ram pickups that are all identical in model/color and features. But they are still 100 unique instances of the blueprint.

My first suggestion would be to stop and re-read the intro chapters of whatever book you are working through because you missed some important stuff.

Secondly: From my FAQ list...

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'

NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:
Was This Post Helpful? 2
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: C# how to hide form using the button from the current active form

Posted 11 December 2012 - 03:27 PM

I honestly deleted my next reply before I posted, because I knew that whatever I was going to try to explain was most likely explained better in one of tlhIn`toq's tutorials. I got distracted while looking for it, and never found/posted it. Knew I'd seen one about two forms communicating.
Was This Post Helpful? 0
  • +
  • -

#7 PiscesHunter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-December 12

Re: C# how to hide form using the button from the current active form

Posted 13 December 2012 - 06:02 AM

View Posth4nnib4l, on 11 December 2012 - 03:27 PM, said:

I honestly deleted my next reply before I posted, because I knew that whatever I was going to try to explain was most likely explained better in one of tlhIn`toq's tutorials. I got distracted while looking for it, and never found/posted it. Knew I'd seen one about two forms communicating.

sorry sir for my senseless post, i am just a newbie in c# i just got interested on it so i don't know much more about it...thanks for you replies
Was This Post Helpful? 0
  • +
  • -

#8 PiscesHunter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-December 12

Re: C# how to hide form using the button from the current active form

Posted 13 December 2012 - 06:07 AM

View PosttlhIn`toq, on 11 December 2012 - 08:47 AM, said:

Quote

 private void button1.click(object sender, EventArgs e)
 {
  Form1 f1 = new Form1();
  f1.hide();
 }
}


Your code does exactly what you are telling it to do.
You make a NEW INSTANCE of a form, of type Form1.
Then you hide the new instance.

The real problem here is not the code as much as it is your understanding of the fundimentals of the language and objects. It seems clear that you do not yet understand that you make instances of objects. You can make 100 new instances of the Form1 type for example. They are all unique and unrelated. Like making 100 Dodge Ram pickups that are all identical in model/color and features. But they are still 100 unique instances of the blueprint.

My first suggestion would be to stop and re-read the intro chapters of whatever book you are working through because you missed some important stuff.

Secondly: From my FAQ list...

[*]Q: ...get Form 'A' to make a change or talk to Form 'B'

NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:



thanks sir for this link you've posted i'll read all about it and learn how to properly understand more about c#.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1