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
  1. Home
  2. General Programming
  3. C#
  4. Problem with Delegates

Problem with Delegates

Scheduled Pinned Locked Moved C#
help
3 Posts 3 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.
  • N Offline
    N Offline
    Nine_
    wrote on last edited by
    #1

    Hi all I'm having a problem with delegate. In my project I have 2 forms, form1 and form2. From form1 I have a button there just for opening form2. And from form2 I have a text box there to enter an Int value. After getting a value from the text box I want to send it back to form1 and appear in a text box on form1. I do this with a delegate but I don't know if my way is the way you use to solve this problem or not. Here is my code:

    Form1

    public delegate void TestDelegate(int i); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frmOpen = new Form2(); frmOpen.ShowDialog(); } public void GetValueA(int i) { textBox1.Text = i.ToString(); } }

    Form2
    public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int b; b = Convert.ToInt32(textBox1.Text); Form1 frm1=new Form1(); TestDelegate td = new TestDelegate(frm1.GetValueA); td(b); Close(); } }

    D K 2 Replies Last reply
    0
    • N Nine_

      Hi all I'm having a problem with delegate. In my project I have 2 forms, form1 and form2. From form1 I have a button there just for opening form2. And from form2 I have a text box there to enter an Int value. After getting a value from the text box I want to send it back to form1 and appear in a text box on form1. I do this with a delegate but I don't know if my way is the way you use to solve this problem or not. Here is my code:

      Form1

      public delegate void TestDelegate(int i); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frmOpen = new Form2(); frmOpen.ShowDialog(); } public void GetValueA(int i) { textBox1.Text = i.ToString(); } }

      Form2
      public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int b; b = Convert.ToInt32(textBox1.Text); Form1 frm1=new Form1(); TestDelegate td = new TestDelegate(frm1.GetValueA); td(b); Close(); } }

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

      The way I do this is to create a public event and delegate in Form2. After I create the new instance of Form2 but before showing it, I subscribe to the event.

      Dave

      1 Reply Last reply
      0
      • N Nine_

        Hi all I'm having a problem with delegate. In my project I have 2 forms, form1 and form2. From form1 I have a button there just for opening form2. And from form2 I have a text box there to enter an Int value. After getting a value from the text box I want to send it back to form1 and appear in a text box on form1. I do this with a delegate but I don't know if my way is the way you use to solve this problem or not. Here is my code:

        Form1

        public delegate void TestDelegate(int i); public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frmOpen = new Form2(); frmOpen.ShowDialog(); } public void GetValueA(int i) { textBox1.Text = i.ToString(); } }

        Form2
        public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int b; b = Convert.ToInt32(textBox1.Text); Form1 frm1=new Form1(); TestDelegate td = new TestDelegate(frm1.GetValueA); td(b); Close(); } }

        K Offline
        K Offline
        Kjetil Svendsen
        wrote on last edited by
        #3

        Hi. Try to create this in Form2:

        public int GetNumericValue()
        {
        this.ShowDialog();

        //This code will run after form is closed
        int iOut = 0;
        int.TryParse(textBox1.Text, out iOut);
        return iOut;
        

        }

        and change the button_click event to

        private void button1_Click(object sender, EventArgs e)
        {
        Form2 frmOpen = new Form2();
        textBox1.Text = frmOpen.GetNumericValue().ToString();
        }

        This is similar to the good old InputBox in VB. Kjetil

        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