Click here to Skip to main content
16,011,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been having this issue for quite a time.

I'm using a LoginForm that when I click the textbox, a second Form called KeyboardForm appears. When I click a button on the KeyboardForm it's supposed to send it's text property and increment it to the text property of the textbox in the LoginForm.

I've been using a very unpractical way, that is using a public string that saves the input, and the KeyboardForm when closing, closes the LoginForm and Reloads it, causing the textbox to change to the designated value I inserted.

But that way it wouldn't change the textbox as I clicked the buttons, but instead would only change it after I reloaded it.

What I wanted to know is, if there is a way, from a second form, to change the textbox.text value in the LoginForm as I click the buttons on the KeyboardForm.

Sorry for the long description, and thank you for the patience.
Posted
Updated 3-Jul-12 5:16am
v2
Comments
[no name] 3-Jul-12 11:22am    
Pass the textbox to your keyboard form.

in your child form

C#
//delegate type for the event declare first
public delegate void UpdateEventHandler(object sender, EventArgs e);

public partial class KeyboardForm : Form
{

    //Next, the event itself is declared.
    public event UpdateEventHandler UpdateParent;

    public KeyboardForm()
    {
        InitializeComponent();
    }

    private void UpdateParent_Click(object sender, EventArgs e)
    {
        //Invoking an event
        if (UpdateParent != null)
            UpdateParent(this, null);
    }
}


in your login form

C#
public LoginForm()
{
    InitializeComponent();
}

private void login_Click(object sender, EventArgs e)
{
    KeyboardForm child = new KeyboardForm();
    // subscribe to child event 
    child.UpdateParent += new UpdateEventHandler(child_UpdateParent);
    child.Show();
}

void child_UpdateParent(object sender, EventArgs e)
{
    // increment text box value
    int newValue = int.Parse(textBox1.Text) +1;
    textBox1.Text = newValue.ToString();
}
 
Share this answer
 
Comments
Steven Borges 4-Jul-12 4:40am    
This did the trick, thank you.
But I removed the:

int newValue = int.Parse(textBox1.Text) +1;

From the child_UpdateParent, since because of it it would only let me increment a value once.
Created a global variable instead and made it the following:

void child_UpdateParent(object sender, EventArgs e)
{
// increment text box value
textBox1.Text = MainMDI.globalstring;
}

My "MainMDI" is a form that loads all the others, and that hosts the global variables.
DamithSL 4-Jul-12 4:45am    
Ok, Change as you wish, it seems you are learning fast :)
create an function in first form, call it from second form by creating an object of first form.
than pass the sting value to that form.

or else
u can call directly that textbox.text property through creating an object of first form from second form
 
Share this answer
 
Comments
Steven Borges 3-Jul-12 11:38am    
Could you explain it better ^^
I'm kinda new to this, I unfortunately don't have your knowledge when it comes to coding.

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