Problem with 2 forms working together.

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 2,024
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic

Problem with 2 forms working together.

 
1
  #1
Nov 7th, 2009
I have a main form which is going to use much input data typed in by the user. So I thought to make a separate form to handle the input. The closest to a solution was this thread http://www.daniweb.com/forums/thread231368.html
But I don't want the second form to be disposed off, or be closed by clicking the close icon. I like to store the input data in the second form(seems logical to me) and let the main form use them.
I don't want the input form to be visible all of the time. Just want to let it pop up when the user needs it(via menu or button, don't know yet)
This is my code so far a main form with a button and a click event and an inputdata form:
  1. public partial class Form1 : Form
  2. {
  3. private InputData InputForm = new InputData();
  4.  
  5. public Form1()
  6. {
  7. InitializeComponent();
  8. }
  9.  
  10. private void button1_Click(object sender, EventArgs e)
  11. {
  12. InputForm.Show();
  13. }
  14. }
Not very much, I know. But what would be the most elegant way to handle this?
As always, any help is greatly appreciated.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 500
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
1
  #2
Nov 7th, 2009
Hi danny.
You have to handle formclosing event along with singleton - single instance of Form.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,402
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 613
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
2
  #3
Nov 7th, 2009
I would treat the settings form more like a static class for this since thats more or less how you want to deal with it.

Main form:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb.twoforms
  11. {
  12. public partial class frmMain : Form
  13. {
  14. public frmMain()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void buttonGetValues_Click(object sender, EventArgs e)
  20. {
  21. StringBuilder sb = new StringBuilder();
  22. sb.AppendLine("Value1: " + frmSettings.Value1);
  23. sb.AppendLine("Value2: " + frmSettings.Value2);
  24. sb.AppendLine("Value3: " + frmSettings.Value3);
  25. MessageBox.Show(sb.ToString());
  26. }
  27.  
  28. private void buttonShowForm_Click(object sender, EventArgs e)
  29. {
  30. frmSettings.ShowForm();
  31. }
  32. }
  33. }

Settings:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb.twoforms
  11. {
  12. public partial class frmSettings : Form
  13. {
  14. static frmSettings instance;
  15.  
  16. static frmSettings()
  17. {
  18. instance = new frmSettings();
  19. }
  20.  
  21. public static string Value1
  22. {
  23. get { return instance.textBox1.Text; }
  24. set { instance.textBox1.Text = value; }
  25. }
  26.  
  27. public static string Value2
  28. {
  29. get { return instance.textBox2.Text; }
  30. set { instance.textBox2.Text = value; }
  31. }
  32.  
  33. public static string Value3
  34. {
  35. get { return instance.textBox3.Text; }
  36. set { instance.textBox3.Text = value; }
  37. }
  38.  
  39. public static void ShowForm()
  40. {
  41. if (instance.Visible)
  42. instance.BringToFront();
  43. else
  44. instance.Show();
  45. }
  46.  
  47. public frmSettings()
  48. {
  49. InitializeComponent();
  50.  
  51. //This smokes out a bug in code if you accidently create 2 instances
  52. if (instance != null)
  53. throw new InvalidOperationException("Only one instance allowed");
  54.  
  55. this.textBox1.Text = "default val 1";
  56. this.textBox2.Text = "default val 2";
  57. this.textBox3.Text = "default val 3";
  58. }
  59.  
  60. private void frmSettings_FormClosing(object sender, FormClosingEventArgs e)
  61. {
  62. e.Cancel = true;
  63. this.Hide();
  64. }
  65. }
  66. }
Attached Files
File Type: zip daniweb.twoforms.zip (16.0 KB, 4 views)
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,024
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 300
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Postaholic
 
1
  #4
Nov 8th, 2009
Thanks guys, these suggestions are very helpfull.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: mjean is an unknown quantity at this point 
Solved Threads: 0
mjean mjean is offline Offline
Newbie Poster
 
0
  #5
32 Days Ago
I ha an MDI parent Form which I want it to be in the to of othe form when I click a button and when I click the button back the form should go to center, I create this code :

  1. if (TopMost == false)
  2. {
  3. //Dis play the form to the top
  4. this.TopMost = true;
  5. }
  6. else
  7. {
  8. // Display the form the ceter.
  9. TopMost = false;
  10. }

it not working.
Last edited by adatapost; 32 Days Ago at 9:08 pm. Reason: Use [code] tags to wrap your code.
Reply With Quote Quick reply to this message  
Reply

Tags
c#, form

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



Tag cloud for c#, form
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC