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. Changing data on a different form

Changing data on a different form

Scheduled Pinned Locked Moved C#
csharpvisual-studiographicstutorialquestion
3 Posts 2 Posters 10 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Stipulation - I'm doing this with notepad and the SDK.. no Visual Studio. How do you get a spawend dialog to interact with the main form? With VB, it was as easy as referring to the main form and it's properties. It doesn't seem to be as easy with c#.. example code..

    using System;
    using System.Windows.Forms;
    using System.Drawing;

    class MainForm : Form {

    Button btnGo = new Button();
    
         public MainForm() {
    
    	Text = "Main Form";
    	btnGo.Location = new Point(100,100);
    	btnGo.Name = "btnGo";
    	btnGo.Text = "Go";
    	btnGo.Click += new EventHandler(btnGo\_Click);	
    	Controls.Add(btnGo);
    	Opacity = 80;
    }
    
    public static void Main(string\[\] args) {
    	Application.Run(new MainForm());
    }
    
    private void btnGo\_Click( object sender, EventArgs e) {
    
    	Form2 x = new Form2();
    	x.ShowDialog();
    }
    

    }

    class Form2 : Form {

    Button btn1 = new Button();
    
    public Form2() {
    	Text = "This is the second form";
    	btn1.Text = "Do it";
    	btn1.Location = new Point(100,100);
    	btn1.Click += new EventHandler(btn1\_Click);
    	btn1.Name = "btn1";
    }
    
    private void btn1\_Click(object sender, EventArgs e) {
    	//Here is where I want to modify the main form.
    	// How would I go about changing, let's say, the Title?
    }
    

    }

    A 1 Reply Last reply
    0
    • L Lost User

      Stipulation - I'm doing this with notepad and the SDK.. no Visual Studio. How do you get a spawend dialog to interact with the main form? With VB, it was as easy as referring to the main form and it's properties. It doesn't seem to be as easy with c#.. example code..

      using System;
      using System.Windows.Forms;
      using System.Drawing;

      class MainForm : Form {

      Button btnGo = new Button();
      
           public MainForm() {
      
      	Text = "Main Form";
      	btnGo.Location = new Point(100,100);
      	btnGo.Name = "btnGo";
      	btnGo.Text = "Go";
      	btnGo.Click += new EventHandler(btnGo\_Click);	
      	Controls.Add(btnGo);
      	Opacity = 80;
      }
      
      public static void Main(string\[\] args) {
      	Application.Run(new MainForm());
      }
      
      private void btnGo\_Click( object sender, EventArgs e) {
      
      	Form2 x = new Form2();
      	x.ShowDialog();
      }
      

      }

      class Form2 : Form {

      Button btn1 = new Button();
      
      public Form2() {
      	Text = "This is the second form";
      	btn1.Text = "Do it";
      	btn1.Location = new Point(100,100);
      	btn1.Click += new EventHandler(btn1\_Click);
      	btn1.Name = "btn1";
      }
      
      private void btn1\_Click(object sender, EventArgs e) {
      	//Here is where I want to modify the main form.
      	// How would I go about changing, let's say, the Title?
      }
      

      }

      A Offline
      A Offline
      AndyG
      wrote on last edited by
      #2

      Try this. I added 2 lines of code, one in the "Owner" form (btnGo_Click), and one in the "Owned" form (btn1_Click). Good Luck! class MainForm : Form {  Button btnGo = new Button();  public MainForm()  {   Text = "Main Form";   btnGo.Location = new Point(100,100);   btnGo.Name = "btnGo";   btnGo.Text = "Go";   btnGo.Click += new EventHandler(btnGo_Click);   Controls.Add(btnGo);   Opacity = 80;  }  public static void Main(string[] args)  {   Application.Run(new MainForm());  }  private void btnGo_Click( object sender, EventArgs e)  {   Form2 x = new Form2();   x.Owner = this;   x.ShowDialog();  } } class Form2 : Form {  Button btn1 = new Button();  public Form2()  {   Text = "This is the second form";   btn1.Text = "Do it";   btn1.Location = new Point(100,100);   btn1.Click += new EventHandler(btn1_Click);   btn1.Name = "btn1";   Controls.Add(btn1);  }  private void btn1_Click(object sender, EventArgs e)  {   //Here is where I want to modify the main form.   // How would I go about changing, let's say, the Title?   this.Owner.Text = "HOWDY";  } } Andy Gaskell, MCSD

      L 1 Reply Last reply
      0
      • A AndyG

        Try this. I added 2 lines of code, one in the "Owner" form (btnGo_Click), and one in the "Owned" form (btn1_Click). Good Luck! class MainForm : Form {  Button btnGo = new Button();  public MainForm()  {   Text = "Main Form";   btnGo.Location = new Point(100,100);   btnGo.Name = "btnGo";   btnGo.Text = "Go";   btnGo.Click += new EventHandler(btnGo_Click);   Controls.Add(btnGo);   Opacity = 80;  }  public static void Main(string[] args)  {   Application.Run(new MainForm());  }  private void btnGo_Click( object sender, EventArgs e)  {   Form2 x = new Form2();   x.Owner = this;   x.ShowDialog();  } } class Form2 : Form {  Button btn1 = new Button();  public Form2()  {   Text = "This is the second form";   btn1.Text = "Do it";   btn1.Location = new Point(100,100);   btn1.Click += new EventHandler(btn1_Click);   btn1.Name = "btn1";   Controls.Add(btn1);  }  private void btn1_Click(object sender, EventArgs e)  {   //Here is where I want to modify the main form.   // How would I go about changing, let's say, the Title?   this.Owner.Text = "HOWDY";  } } Andy Gaskell, MCSD

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Perfect! Except for the fact I didn't add the control. Thanks...

        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