none
How do you display text on a windows form? RRS feed

  • Question

  • Howdy! I'm making an app in Visual Studio which generates language, then displays it onscreen. I want to use a windows form, but can't get it to work. I tried Form1.Text = x, which gives me an error, but doing the same thing for a button's text property doesn't. Thanks in advance for any help!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    namespace Pome_Generator_App
    {
        public partial class Intro : Form
        {
            bool formload = false;
            string test = "hi";
           
            public Intro()
            {
                InitializeComponent();
            }
            public void Intro_Load(object sender, EventArgs e)
            {
                Adjectives newAdjectives = new Adjectives();
                Verbs newVerbs = new Verbs();
          
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
            public void button1_Click_1(object sender, EventArgs e)
            {
                if (!formload)
                {
                    var form = new Pomescreen();
                    form.Show();
                    formload = true;
                }
                Pomescreen.Text = test;
                button1.Text = test;

            }
        }
    }

     
    Thursday, May 16, 2019 8:44 PM

Answers

  • Hello,

    The following takes a different approach, in this case the main form is Form1 and the child form is Form2.

    We want to know if Form2 has been created and displayed using 

    var fc = Application.OpenForms.OfType<Form>().FirstOrDefault(form => form.Name == "Form2");

    If fc is null that means the form is not being displayed currently so create it, otherwise set the text from a TextBox named childFormTitleTextBox using it's Text property if there is text, otherwise use a pre-determine text to set the child form's title. If the form is already shown place it to the right of Form1.

    Code form Form1, no code in Form2.

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace ForForumQuestion
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void openFormButton_Click(object sender, EventArgs e)
            {
                // see if the child form is open
                var fc = Application.OpenForms.OfType<Form>()
                    .FirstOrDefault(form => form.Name == "Form2");
    
                if (fc == null)
                {
                    // not open so create, set text 
                    var f = new Form2
                    {
                        Text = !string.IsNullOrWhiteSpace(childFormTitleTextBox.Text) ? 
                            childFormTitleTextBox.Text : "Not set"
                    };
    
                    // display the form just created.
                    f.Show();
    
                }
                else
                {
                    // form is already created, bring it to front and 
                    // position it to the right of this form.
                    fc.BringToFront();
                    fc.Top = Top;
                    fc.Left = (Left + Width) + 10;
                }
            }
        }
    }
    
    Hopefully the above is clear and easy to follow.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 16, 2019 11:13 PM
    Moderator

All replies

  • What line is the error on and what exactly is the error message.

    If this is the error line Pomescreen.Text = test; then change it to 

    var form = new Pomescreen();

    form.Text = test;

    Currently you can simply do that so you need to move it as follows

    if (!formload)
    {
        var form = new Pomescreen();
        form.Text = test;
        form.Show();
        
        formload = true;
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 16, 2019 8:51 PM
    Moderator
  • Line 49 "An object reference is required for the non-static field, method, or property 'Form.Text'
    Thursday, May 16, 2019 8:56 PM
  • the Pomesreen.Text line
    Thursday, May 16, 2019 8:56 PM
  • It's not supposed to run the if (formload) brackets every time, though, just the first time the user tries it. This is loads the display form, but only once. Updating the text should be outside these.
    Thursday, May 16, 2019 9:05 PM
  • I just noticed the text in the form's properties is not showing up. Your way seems to work, though.
    Thursday, May 16, 2019 9:33 PM
  • I'm a newbie to Visual Studio. I have no idea how to display text on a windows form.
    Thursday, May 16, 2019 9:56 PM
  • Hello,

    The following takes a different approach, in this case the main form is Form1 and the child form is Form2.

    We want to know if Form2 has been created and displayed using 

    var fc = Application.OpenForms.OfType<Form>().FirstOrDefault(form => form.Name == "Form2");

    If fc is null that means the form is not being displayed currently so create it, otherwise set the text from a TextBox named childFormTitleTextBox using it's Text property if there is text, otherwise use a pre-determine text to set the child form's title. If the form is already shown place it to the right of Form1.

    Code form Form1, no code in Form2.

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace ForForumQuestion
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void openFormButton_Click(object sender, EventArgs e)
            {
                // see if the child form is open
                var fc = Application.OpenForms.OfType<Form>()
                    .FirstOrDefault(form => form.Name == "Form2");
    
                if (fc == null)
                {
                    // not open so create, set text 
                    var f = new Form2
                    {
                        Text = !string.IsNullOrWhiteSpace(childFormTitleTextBox.Text) ? 
                            childFormTitleTextBox.Text : "Not set"
                    };
    
                    // display the form just created.
                    f.Show();
    
                }
                else
                {
                    // form is already created, bring it to front and 
                    // position it to the right of this form.
                    fc.BringToFront();
                    fc.Top = Top;
                    fc.Left = (Left + Width) + 10;
                }
            }
        }
    }
    
    Hopefully the above is clear and easy to follow.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 16, 2019 11:13 PM
    Moderator