944,212 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1651
  • C# RSS
Nov 7th, 2009
1

Problem with 2 forms working together.

Expand Post »
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:
c# Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 7th, 2009
1
Re: Problem with 2 forms working together.
Hi danny.
You have to handle formclosing event along with singleton - single instance of Form.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Nov 7th, 2009
2
Re: Problem with 2 forms working together.
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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, 30 views)
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 8th, 2009
1
Re: Problem with 2 forms working together.
Thanks guys, these suggestions are very helpfull.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 16th, 2009
0
Re: Problem with 2 forms working together.
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 :

C# Syntax (Toggle Plain Text)
  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; Nov 16th, 2009 at 9:08 pm. Reason: Use [code] tags to wrap your code.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mjean is offline Offline
1 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: SHDocVw insert element in document.body?
Next Thread in C# Forum Timeline: How to send am Email using c# ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC