清除了文本框值 [英] cleared the textbox value

查看:65
本文介绍了清除了文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对c#windows应用程序有一个小小的澄清。

我有两个表单如form1和form2。

在form1中我有一个文本框和一个按钮。

当我在文本框中输入任何文本然后我点击下面给出的确定按钮代码,

我的代码是:form1



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsApplication2
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
Form2 fm = new Form2();
fm.Show();
}
}
}





在form2中我有一个后退按钮。



在后退按钮中,我返回以下代码,重定向到上一页,这意味着我会来的。



我的代码:form2



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsApplication2
{
public partial class Form2:表单
{
public Form2()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
Form1 fm = new Form1();
fm.Show();
}
}
}









但我的问题是当我从form1转到第二个表单并单击form2按钮然后我的form1文本框值在form1中自动清除。



但我的目标是我不想清除我在form1文本框中输入的文本框值



如果有人知道这个问题请告诉我..这样对我很有帮助



提前感谢。

解决方案

而不是创建(并显示)<$ c的新实例$ c> Form1 ,在你的 Form2 '后退'按钮中,你可以简单地调用 Close()方法(这将关闭 Form2 并再次显示 Form1 ,其状态与之前相同) 。


正如ThePhantomUpvoter指出的那样,每次单击其中一个按钮时,您都会显示一个新表单。而不是这样,您可以在按下按钮时隐藏每个表单并显示另一个表单。方法如下:



首先,您需要引用其他表单中可访问的每个表单。为此,我修改了Program.cs文件,如下所示:

  public   static  Form1 f1; 
public static Form2 f2;

/// < 摘要 >
/// 应用程序的主要入口点。
/// < / summary >
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
f1 = new Form1();
Application.Run(f1);
}





然后,您需要修改按钮的事件以隐藏当前表单并显示其他表单:

  private   void  button1_Click( object  sender,EventArgs e)
{
if (Program.f2 == null
{
Program.f2 = new Form2();
}

this .Hide();
Program.f2.Show();
}



  private   void  button1_Click( object  sender,EventArgs e)
{
if (Program.f1 == null
{
Program.f1 = new Form1();
}

this .Hide();
Program.f1.Show();
}


只需要调用活动表单关闭事件

你必须编写相同的代码



Form.ActiveForm.Close();



和vs 2008





你必须写上





me.close()

Hi all,

I have a small clarification about c# windows application.
The moment i have two form such as form1 and form2.
In form1 i have one textbox and one button.
when i have enter any text in to the textbox and then i click ok button which is by the given below code,
my code is: form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.Show();
        }
    }
}



In form2 i have one back button.

In the back button i have return the below code which is redirect to the previous page which mean the were would i came.

my code:form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 fm = new Form1();
            fm.Show();
        }
    }
}





But my problem is when i go to second form from form1 and click form2 button then my form1 textbox value automatically cleared in form1.

But my goal is i dont want cleared my textbox value which i entered in form1 textbox

so please let me know if anybody knows this issue.. so that its very kind helpful for me

thanks in advance.

解决方案

Instead of creating (and showing) a new instance of Form1, in your Form2 'back' button, you might simply call the Close() method (this would close Form2 and show again the Form1, with the same state it had before).


As ThePhantomUpvoter pointed out, you are showing a new form every time you click one of the buttons. Instead of this, you can hide each form when its button is pressed and show the other form. Here's how:

First, you need to have a reference to each form accessible in the other form. For this, I modified the Program.cs file as follows:

public static Form1 f1;
public static Form2 f2;

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



Then, you need to modify the buttons' events to hide the current form and show the other form:

private void button1_Click(object sender, EventArgs e)
{
    if (Program.f2 == null)
    {
        Program.f2 = new Form2();
    }

    this.Hide();
    Program.f2.Show();
}


private void button1_Click(object sender, EventArgs e)
{
    if (Program.f1 == null)
    {
        Program.f1 = new Form1();
    }

    this.Hide();
    Program.f1.Show();
}


simply you have to call the active forms close event
you have to write same code

Form.ActiveForm.Close();

and in vs 2008


you have to write


me.close()


这篇关于清除了文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆