Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Working with multiple forms

Working with multiple forms

Scheduled Pinned Locked Moved C#
helpquestion
20 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B bwood2020

    Hello, I have two forms; one is the main form and the other the Delimiter form. The first form gets the file extension and location from the user. The second form is used when the user selects .txt files. The second form captures the delimiter of the .txt file then is closed when the "OK" button is clicked. Here is where I am having the problem. I need the delimiter info the user inputs, which is a text box on the second form, to be called in the connection string. I have tried a couple of things but the only delimiter I can get to work is when I hard code this into the connection. Can someone please help me? Here is what I have: First form: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited(TAB)"""; Second form: string GD; GD = textBox1.Text; What I would like to do is something like: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited["+ GD +")"; --This doesn't work Thank you!

    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #3

    If I understand you correctly, you want to get a value from the second Form in the first? If so, create a public property or method in the second Form, and call that from the first using the instance of the second Form.

    // form2 is the Form2 instance and TextIWant is a string property getter in Form2
    string specialText = form2.TextIWant

    Dave
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
    Why are you using VB6? Do you hate yourself? (Christian Graus)

    B 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      bwood2020 wrote:

      =Delimited["+ GD +")";

      Try replacing '[' with '(' - it may help!

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      B Offline
      B Offline
      bwood2020
      wrote on last edited by
      #4

      Thank you for your reply! Sorry, that was a typo on my part. I had ["+ GD +"]. GD doesn't show up in the popup but the name of the second form does (GetDelimiter). I used this as well but it throws the error: GetDelimiter is a 'type' but is used like a 'variable'. When I use GD it throws the error: GD does not exist in the current context. What am I doing worng here? Thank you!

      1 Reply Last reply
      0
      • D DaveyM69

        If I understand you correctly, you want to get a value from the second Form in the first? If so, create a public property or method in the second Form, and call that from the first using the instance of the second Form.

        // form2 is the Form2 instance and TextIWant is a string property getter in Form2
        string specialText = form2.TextIWant

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        B Offline
        B Offline
        bwood2020
        wrote on last edited by
        #5

        Yes, that is correct. I want to capture the value from the second form and use it in the first. in the second form I have this: public void textBox1_TextChanged(object sender, EventArgs e) { string GD; GD = textBox1.Text; } then I am calling it in the first form with this: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited("+ GD +")"; Both methods were made public before posting. Should have mentioned that. When I declare another string in the first it only allows me to write: string GDS = GetDelimiter.GD This throws an error: Doesn't contain a definition for GD.

        D 1 Reply Last reply
        0
        • B bwood2020

          Yes, that is correct. I want to capture the value from the second form and use it in the first. in the second form I have this: public void textBox1_TextChanged(object sender, EventArgs e) { string GD; GD = textBox1.Text; } then I am calling it in the first form with this: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited("+ GD +")"; Both methods were made public before posting. Should have mentioned that. When I declare another string in the first it only allows me to write: string GDS = GetDelimiter.GD This throws an error: Doesn't contain a definition for GD.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #6

          Keep a reference to the second form when you instanciate it and use that reference. A simple example. Button on Form1, TextBox on Form2:

          // Form1
          using System;
          using System.Windows.Forms;

          namespace WindowsFormsApplication1
          {
          public partial class Form1 : Form
          {
          private Form2 form2;
          public Form1()
          {
          InitializeComponent();
          form2 = new Form2();
          form2.Show();
          }

              private void button1\_Click(object sender, EventArgs e)
              {
                  if (form2 != null)
                      MessageBox.Show(form2.GD);
              }
          }
          

          }

          // Form2
          using System;
          using System.Windows.Forms;

          namespace WindowsFormsApplication1
          {
          public partial class Form2 : Form
          {
          public Form2()
          {
          InitializeComponent();
          }

              private void textBox1\_TextChanged(object sender, EventArgs e)
              {
                  GD = textBox1.Text;
              }
          
              public string GD
              {
                  get;
                  set;
              }
          }
          

          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          B 1 Reply Last reply
          0
          • D DaveyM69

            Keep a reference to the second form when you instanciate it and use that reference. A simple example. Button on Form1, TextBox on Form2:

            // Form1
            using System;
            using System.Windows.Forms;

            namespace WindowsFormsApplication1
            {
            public partial class Form1 : Form
            {
            private Form2 form2;
            public Form1()
            {
            InitializeComponent();
            form2 = new Form2();
            form2.Show();
            }

                private void button1\_Click(object sender, EventArgs e)
                {
                    if (form2 != null)
                        MessageBox.Show(form2.GD);
                }
            }
            

            }

            // Form2
            using System;
            using System.Windows.Forms;

            namespace WindowsFormsApplication1
            {
            public partial class Form2 : Form
            {
            public Form2()
            {
            InitializeComponent();
            }

                private void textBox1\_TextChanged(object sender, EventArgs e)
                {
                    GD = textBox1.Text;
                }
            
                public string GD
                {
                    get;
                    set;
                }
            }
            

            }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            B Offline
            B Offline
            bwood2020
            wrote on last edited by
            #7

            Having a problem with get and set. The error I receive for: public void textBox1_TextChanged(object sender, EventArgs e) { GD = textBox1.Text; } public string GD { get; set; } Error: get and set must declare a body because it is not marked abstract or extern. I tried adding some code to get and set and I received this error: public void textBox1_TextChanged(object sender, EventArgs e) { //GD = textBox1.Text; } public string GD { get { return GD; } set { GD = value; } } Error:Make sure you do not have an infinite loop or infinite recursion. Any ideas?

            H D 2 Replies Last reply
            0
            • B bwood2020

              Having a problem with get and set. The error I receive for: public void textBox1_TextChanged(object sender, EventArgs e) { GD = textBox1.Text; } public string GD { get; set; } Error: get and set must declare a body because it is not marked abstract or extern. I tried adding some code to get and set and I received this error: public void textBox1_TextChanged(object sender, EventArgs e) { //GD = textBox1.Text; } public string GD { get { return GD; } set { GD = value; } } Error:Make sure you do not have an infinite loop or infinite recursion. Any ideas?

              H Offline
              H Offline
              Henry Minute
              wrote on last edited by
              #8

              The problem is caused by your new public property. Change the case of the field references. Change:

              public string GD
              {
              get { return GD; } // this is calling this property and so goes round in circles
              set { GD = value; } // same here
              }

              to:

              public string GD
              {
              get { return gd; } // now referencing your private field, assuming that you called it 'gd'
              set { gd = value; } // same here
              }

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              B 1 Reply Last reply
              0
              • B bwood2020

                Having a problem with get and set. The error I receive for: public void textBox1_TextChanged(object sender, EventArgs e) { GD = textBox1.Text; } public string GD { get; set; } Error: get and set must declare a body because it is not marked abstract or extern. I tried adding some code to get and set and I received this error: public void textBox1_TextChanged(object sender, EventArgs e) { //GD = textBox1.Text; } public string GD { get { return GD; } set { GD = value; } } Error:Make sure you do not have an infinite loop or infinite recursion. Any ideas?

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #9

                Sorry, I was using automatic properties which may not be available in the version that you're working with. Follow Henry's advice and use a private member variable that the property getter and setter accesses.

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                • H Henry Minute

                  The problem is caused by your new public property. Change the case of the field references. Change:

                  public string GD
                  {
                  get { return GD; } // this is calling this property and so goes round in circles
                  set { GD = value; } // same here
                  }

                  to:

                  public string GD
                  {
                  get { return gd; } // now referencing your private field, assuming that you called it 'gd'
                  set { gd = value; } // same here
                  }

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  B Offline
                  B Offline
                  bwood2020
                  wrote on last edited by
                  #10

                  Sorry, I'm not following. As you can guess I am not a very good programmer. Just starting out and I thank you for your patience. I do not have any private fields in the second form. Do I need to declare gd as private? Something like: public void textBox1_TextChanged(object sender, EventArgs e) { GD = textBox1.Text; gd = GD; } private string gd; public string GD { get { return gd; } set { gd = value; } }

                  H 1 Reply Last reply
                  0
                  • B bwood2020

                    Sorry, I'm not following. As you can guess I am not a very good programmer. Just starting out and I thank you for your patience. I do not have any private fields in the second form. Do I need to declare gd as private? Something like: public void textBox1_TextChanged(object sender, EventArgs e) { GD = textBox1.Text; gd = GD; } private string gd; public string GD { get { return gd; } set { gd = value; } }

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #11

                    Almost right. Well worked out! just a small change

                    public void textBox1_TextChanged(object sender, EventArgs e)
                    {

                    GD = textBox1.Text;
                    gd = GD; // this line is not needed. When you say GD = textBox1.Text, the lines in the set part of GD
                    // (called the setter, guess what the get part is called) get executed; gd = value.
                    // The value is whatever is on the right of the = in GD = textBox1.Text

                    }
                    private string gd;
                    public string GD
                    {
                    get { return gd; }
                    set { gd = value; }
                    }

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    B 1 Reply Last reply
                    0
                    • H Henry Minute

                      Almost right. Well worked out! just a small change

                      public void textBox1_TextChanged(object sender, EventArgs e)
                      {

                      GD = textBox1.Text;
                      gd = GD; // this line is not needed. When you say GD = textBox1.Text, the lines in the set part of GD
                      // (called the setter, guess what the get part is called) get executed; gd = value.
                      // The value is whatever is on the right of the = in GD = textBox1.Text

                      }
                      private string gd;
                      public string GD
                      {
                      get { return gd; }
                      set { gd = value; }
                      }

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      B Offline
                      B Offline
                      bwood2020
                      wrote on last edited by
                      #12

                      All right, it works!!! One more issue though. I placed a messageBox in the code on the first form and I am getting the correct string delimiter. So I know GD is storing this, however, when I go to call GetDelimiter.GD for the in the connection I get an ArgumentException was unhandled error: Format of the initialization string does not conform to specification starting at index 85. Here is the connection string: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited["+ GetDelimiterForm.GD +"]"; The error occures at: OleDbConnection conTXT = new OleDbConnection(comString); I know it has something to do with FMT=Delimited["+ GetDelimiterForm.GD +"]"; because if I hard code it it has no problems (FMT=Delimiter(TAB)""";). I have tried replacing "[" with "(" and that doesn't work. Any ideas on this one?

                      H 1 Reply Last reply
                      0
                      • B bwood2020

                        All right, it works!!! One more issue though. I placed a messageBox in the code on the first form and I am getting the correct string delimiter. So I know GD is storing this, however, when I go to call GetDelimiter.GD for the in the connection I get an ArgumentException was unhandled error: Format of the initialization string does not conform to specification starting at index 85. Here is the connection string: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=""text;HDR=YES;FMT=Delimited["+ GetDelimiterForm.GD +"]"; The error occures at: OleDbConnection conTXT = new OleDbConnection(comString); I know it has something to do with FMT=Delimited["+ GetDelimiterForm.GD +"]"; because if I hard code it it has no problems (FMT=Delimiter(TAB)""";). I have tried replacing "[" with "(" and that doesn't work. Any ideas on this one?

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #13

                        When you hard code it you are using slightly different syntax Hard coded: FMT=Delimiter(TAB) Using GetDelimiterForm: FMT=Delimited[...... Delimiter and Delimited, or is that just a typo?

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        B 1 Reply Last reply
                        0
                        • H Henry Minute

                          When you hard code it you are using slightly different syntax Hard coded: FMT=Delimiter(TAB) Using GetDelimiterForm: FMT=Delimited[...... Delimiter and Delimited, or is that just a typo?

                          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                          B Offline
                          B Offline
                          bwood2020
                          wrote on last edited by
                          #14

                          I apologize, it was a typo. What I meant was Hard Coded: FMT=Delimited(TAB) \\This works Using GetDelimiterForm: FMT=Delimited["+ GetDelimiterForm.GD +"]\\This throws the error

                          H 1 Reply Last reply
                          0
                          • B bwood2020

                            I apologize, it was a typo. What I meant was Hard Coded: FMT=Delimited(TAB) \\This works Using GetDelimiterForm: FMT=Delimited["+ GetDelimiterForm.GD +"]\\This throws the error

                            H Offline
                            H Offline
                            Henry Minute
                            wrote on last edited by
                            #15

                            It is the double quote thing for Extended Properties that is causing the problem. In order to get a quotation mark within a literal string you have to 'escape them' This means that the double quotes in the middle have to look like \"\", for them to be recognised as quotes within a string. When this is all over look up Escape Characters for a better explanation. In the mean time take a look at ADO Connection Strings[^], for some examples. Search in the article for 'Extended Properties' and you'll see what I mean. Have a play. Good Luck! :)

                            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                            B 2 Replies Last reply
                            0
                            • H Henry Minute

                              It is the double quote thing for Extended Properties that is causing the problem. In order to get a quotation mark within a literal string you have to 'escape them' This means that the double quotes in the middle have to look like \"\", for them to be recognised as quotes within a string. When this is all over look up Escape Characters for a better explanation. In the mean time take a look at ADO Connection Strings[^], for some examples. Search in the article for 'Extended Properties' and you'll see what I mean. Have a play. Good Luck! :)

                              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                              B Offline
                              B Offline
                              bwood2020
                              wrote on last edited by
                              #16

                              Thank you Henry for all your help. I will look into this today.

                              1 Reply Last reply
                              0
                              • H Henry Minute

                                It is the double quote thing for Extended Properties that is causing the problem. In order to get a quotation mark within a literal string you have to 'escape them' This means that the double quotes in the middle have to look like \"\", for them to be recognised as quotes within a string. When this is all over look up Escape Characters for a better explanation. In the mean time take a look at ADO Connection Strings[^], for some examples. Search in the article for 'Extended Properties' and you'll see what I mean. Have a play. Good Luck! :)

                                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                B Offline
                                B Offline
                                bwood2020
                                wrote on last edited by
                                #17

                                Hi Henry, I have read a couple articles on escape sequences and have tried to include the same line of code(just modified to fit what I need) and I still get errors. Maybe I am looking in the wrong place but when I try and use Extended Properties=\"\"text;HDR=Yes;FMT=Delimited(" + gds + ");\"\";"; it says I need to add ";". But when I add one it tells me I need to add another. However, if I were to plug the line of code above into the code found in the Code Project page it seems fine. I think what is happening is that there is something wrong with the first part of the connection string, maybe the Source. "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @"; Could you look at this one more time for me and point me in the right direction when you have time please? Here is the entire connection string: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=\"\"text;HDR=Yes;FMT=Delimited(" + gds + ");\"\";"; Thank you, Brenton

                                H 1 Reply Last reply
                                0
                                • B bwood2020

                                  Hi Henry, I have read a couple articles on escape sequences and have tried to include the same line of code(just modified to fit what I need) and I still get errors. Maybe I am looking in the wrong place but when I try and use Extended Properties=\"\"text;HDR=Yes;FMT=Delimited(" + gds + ");\"\";"; it says I need to add ";". But when I add one it tells me I need to add another. However, if I were to plug the line of code above into the code found in the Code Project page it seems fine. I think what is happening is that there is something wrong with the first part of the connection string, maybe the Source. "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @"; Could you look at this one more time for me and point me in the right direction when you have time please? Here is the entire connection string: String comString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(FilePath) + @";Extended Properties=\"\"text;HDR=Yes;FMT=Delimited(" + gds + ");\"\";"; Thank you, Brenton

                                  H Offline
                                  H Offline
                                  Henry Minute
                                  wrote on last edited by
                                  #18

                                  I have found an article that is doing what I think you are trying to do. Read text file (txt, csv, log, tab, fixed length)[^], it only uses one \" instead of two. Have a look and see if it helps.

                                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                  B 1 Reply Last reply
                                  0
                                  • H Henry Minute

                                    I have found an article that is doing what I think you are trying to do. Read text file (txt, csv, log, tab, fixed length)[^], it only uses one \" instead of two. Have a look and see if it helps.

                                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                    B Offline
                                    B Offline
                                    bwood2020
                                    wrote on last edited by
                                    #19

                                    That did it Henry. Thank you so much. Is there something I need to do is far as closing this out or giving you a rating? Let me know and I will come back.

                                    H 1 Reply Last reply
                                    0
                                    • B bwood2020

                                      That did it Henry. Thank you so much. Is there something I need to do is far as closing this out or giving you a rating? Let me know and I will come back.

                                      H Offline
                                      H Offline
                                      Henry Minute
                                      wrote on last edited by
                                      #20

                                      Thanks for the thanks! If you would just vote one of my posts as a 5, then people will know that your question has been answered. Good luck! :)

                                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups