Text Box Display (Without building public interface)

Hi

I have 2 window forms. First form has a text box and a CmdButton. My second form has one text box. Both the text boxes are in Public modifier. My first form will show as default.

My goal is whenever I type anything on Textbox1 (form1) and press command button, my first form should be closed and second form should be on visible, on that time, the contents which I typed on the First form’s text box will appear on the text box of my second form.

Like this

public void BtnSend_Click(object sender, EventArgs e)

{

SecondForm SF = new SecondForm();

SF.AllowDrop=true;

SF.TextBox1.Text = This.TextBox1.Text;

This.close();

SF.show();

SF.TextBox1.Focus();

}

Can any one help me

Regards

Ahmed Sahib




Answer this question

Text Box Display (Without building public interface)

  • Leaf.

    Mr.Nahgum Thanks for your reply.

    Sorry for my inconvenience for not to put my query as crystal clear. Let me explain you clearly.

    I have a form named as MyMain. It has two controls. One textbox (named as TxtResultTextBox) and one Command Button (named as BtnShowSecondaryForm). This form is my default form, so it should visible on my startup. Its windows state is maximized.

    And I have another form named as Secondary. It also contains one textbox (named as TxtSourceTextBox) and one Command Button (BtnSendText) Respectively.

    While clicking on the BtnShowSecondaryForm command button of my MyMain form, The Secondary Form should visible. Its windows state is Normal. It is smaller than MyMain form. So user can see both the forms but the focus now with Secondary Form. MyMain form showing on the backside of it.

    I don’t want to hide my MyMain form when user clicks on the button btnShowSecondaryForm (ie when Secondary form is visible).

    Now assume that, User enters some texts or values on TxtSourceTextBox Textbox of my Secondary form. Once he clicks on the Command Button BtnSendText, I have to send the text/values he enters to my MyMain form’s TxtResultTextBox and immediately close my Secondary form. This is what I need.

    I don’t like to create any instance to MyMain form which opens another instace.

    I hope you may get it clear now.

    Regards

    Ahmed Sahib



  • TSZ

    That's not what you asked orignally. You said "press command button, my first form should be closed and second form should be on visible". First form CLOSED. To answer the question as you have described the second time, the below will do the job.

     

    Secondary:

     

    public string TxtSourceTextBoxText

    {

    get { return this.txtSourceTextBox.Text; }

    set { this.txtSourceTextBox.Text = value; }

    }

    private void btnSendText_Click(object sender, EventArgs e)

    {

    this.DialogResult = DialogResult.OK;

    this.Close();

    }

    MyMain:

    private void btnShowSecondaryForm_Click(object sender, EventArgs e)

    {

    Secondary secondary = new Secondary();

    secondary.TxtSourceTextBoxText = this.txtResultTextBox.Text;

    if (secondary.ShowDialog() == DialogResult.OK)

    {

    this.txtResultTextBox.Text = secondary.TxtSourceTextBoxText;

    }

    }


  • Urban Terror target

    Hi

    What you have done is the following: you define an local variable called SF in BtnSend_CLick so when the execution leaves the function the SF goes out of scope and will be subject to garbage collection. To correct this problem you have many ways you can adopt, however I will give you the simplest solution.
    Change the SecondForm as follows:

    class SecondForm : Form{
    private static SecondForm instance; // a static field that keeps track of the current instance of SecondForm

    // this property returns the current instance of the SeconfForm, if it does not exist it is created
    public static SecondForm Instance{
    get{
    if(instance == null || !instance.Created){
    instance = new SecondForm();
    }
    return instance;
    }
    }
    }

    now back to your FirstForm

    public void BtnSend_Click(object sender, EventArgs e)

    {

    SecondForm SF = SecondForm.Instance; // get the current instance
    SF.AllowDrop=true;

    SF.TextBox1.Text = This.TextBox1.Text;

    This.close();

    SF.show();

    SF.TextBox1.Focus();

    }

    Hope it helps

    regards


  • JoshKorn

    Dear Mr. Z.Y.S

    Thanks for your prompt reply.

    It is working but it doesn’t satisfy my condition. Because “instance = new SecondForm();” statement creating a new instance of my form (form2), and passes the value to that new form not to the original form (form2).

    I don’t like to close or hide the base form (it is meaningless).

    Is it possible to take clone (Mirroring) to so and so textbox in C#

    So Anymore comment
    Regards

    Ahmed Sahib



  • DarkFire2

    Thanks Mr. Nahguam, I will check your latest coding too.Thanks for all who were willing to solve my problem. Finally I got the solution; it is working fine with my following codes.

    MyMain Form

    private void BtnShowSecondaryForm _Click(object sender, System.EventArgs e)

    {

    Secondary SF = new Secondary ();

    SF.txtout= TxtResultTextBox;

    SF.ShowDialog();

    }

    Secondary Form

    public System.Windows.Forms.TextBox txtout ;

    private void BtnSendText _Click(object sender, System.EventArgs e)

    {

    if(TxtSourceTextBox.Text != "")

    {

    try

    {

    txtout.Text = this.TxtSourceTextBox.Text;

    this.Close();

    }

    catch (Exception xe)

    {

    Console.WriteLine(""+ xe);

    }

    }

    }

    Regards

    Ahmed Sahib



  • redneon

    Form 2:


    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

    public string TextBoxText
    {
    set
    { textBox1.Text = value; }

    }
    }


    Form 1:


    public partial class Form1 : Form
    {
    public
    Form1()

    {
    InitializeComponent()
    }

    private string text;

    public
    void Form2ThreadStart()

    {

    Form2
    form2 = new Form2();

    form2.TextBoxText = text;

    Application
    .Run(form2);

    }


    private
    void button1_Click(object sender, EventArgs e)

    {

    text =
    this.textBox1.Text;
    Thread
    thread = new Thread(new ThreadStart(Form2ThreadStart));

    thread.Start();

    this
    .Close();

    }
    }



  • Text Box Display (Without building public interface)