Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is for a queuing system. When the next button is clicked which is in frmCounter1 and frmCounter2 the increment number must be processed in their preferred textbox which is in main form.

What I have tried:

//counter1
namespace QueuingSystem
{
    public partial class Counter1 : Form
    {
        int count = 0;
        Main view = new Main();
        public Counter1()
        {
            InitializeComponent();
        }
        
        private void btnNext1_Click(object sender, EventArgs e)
        {
            count++;
            label1.Text = count.ToString();
            view.label4.Text = count.ToString();
        }

        private void Counter1_Load(object sender, EventArgs e)
        {
            view.Show();
        }
    }
}



//counter2
namespace QueuingSystem
{
    public partial class Counter2 : Form
    {
        int count = 0;
        Main view = new Main();
        public Counter2()
        {
            InitializeComponent();
        }
        private void btnNext2_Click(object sender, EventArgs e)
        {
            count++;
            label1.Text = count.ToString();
            view.label5.Text = count.ToString();
        }

        private void Counter2_Load(object sender, EventArgs e)
        {
            view.Show();
        }
    }
}
Posted
Updated 17-Feb-18 21:54pm
Comments
BillWoodruff 17-Feb-18 23:44pm    
There are many ways you can implement this, but, first, please clarify:

1. this is a WinForm app ? What is the 'Main Form here ? Where are the instances of the 'Counter Forms created ?

2. why are you using multiple Forms ?

3. "the increment number must be processed in their preferred textbox which is in main form." please say more about what happens in this Form/

4. have you implements the Queue logic ?

Instead of using events, which is the normal way, you can also simply put your count variable in Program as a Static variable.
Then your can simply refer to it as: Program.count in your forms, example:
using System;
using System.Windows.Forms;

namespace QueuingSystem
{
    static class Program
    {
        public static int count = 123;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

And in your Form:
using System.Windows.Forms;

namespace QueuingSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = Program.count.ToString();
        }
    }
}
 
Share this answer
 
v2
Comments
Member 13680689 22-Feb-18 12:07pm    
i get it but i don't know how. can i have some codes for it?
RickZeeland 22-Feb-18 12:25pm    
Updated the solution with an example !
Get rid of the reference to your main form, that's a bad idea - and it won't work as that's a different instance of the form, not the one you are displaying.

Instead, use Events to "tell" the Main form that the child form has done something and needs attention: Transferring information between two forms, Part 2: Child to Parent[^]
It may look complicated, but it isn't really - it's exactly the same mechanism, you use all the time to process the buttons, text boxes, and everything else on your forms.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900