简体   繁体   English

使用类表单将文本框数据传输到RichTextBox

[英]Textbox data to RichTextBox using class form

I have a simple program I have to make here but I'm not quite sure how to start it. 我有一个简单的程序我必须在这里制作,但我不太清楚如何启动它。 I have my class file already defined as shown below. 我已经定义了我的类文件,如下所示。 What I need to do is to make a Windows Form Application (as shown below in the picture.) I need to enter the information into the textboxes and then output it into the richtextbox1 at the bottom. 我需要做的是创建一个Windows窗体应用程序(如下图所示)。我需要将信息输入文本框,然后将其输出到底部的richtextbox1。 And I don't have it shown below, but in the Windows Form, how do you call a method if I had it in this class file? 我没有在下面显示它,但在Windows窗体中,如果我在这个类文件中有它,你如何调用方法?

在此输入图像描述

class Telephone
{
    private string manufacturer;
    private string model;
    private bool isConnected = false;
    private string lastNumberDialed;
    private string phoneNumber;   

    public Telephone()
    {
    }

    public Telephone(string manufacturer, string model, string phoneNumber)
    {
        Manufacturer = manufacturer;
        Model = model;
        PhoneNumber = phoneNumber;
    }

    public string Manufacturer
    {
        get { return manufacturer; }
        set { manufacturer = value; }
    }

    public string Model
    {
        get { return model; }
        set { model = value; }
    }

    public string PhoneNumber
    {
        get { return phoneNumber; }
        set { phoneNumber = value; }
    }

    new public string ToString()
    {
        return "Manufacturer: " + manufacturer;
    }      
}

And here's the design code: 这是设计代码:

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

    private void btnEnter_Click(object sender, EventArgs e)
    {
    }

    private void btnShowPhones_Click(object sender, EventArgs e)
    {
    }

    private void btnDial_Click(object sender, EventArgs e)
    {
    }

    private void btnRedial_Click(object sender, EventArgs e)
    {
    }

    private void btnHangUp_Click(object sender, EventArgs e)
    {
    }
}

I don't really understand why you're asking someone to finish this, since you've got the entire thing wired up to go... unless someone provided you with the code you posted. 我真的不明白为什么你要求别人完成这个,因为你已经把整个事情连接起来......除非有人向你提供你发布的代码。

If you want to store your textbox data in a separate class before displaying it, instantiate it in your code-behind: 如果要在显示文本框数据之前将其存储在单独的类中,请在代码隐藏中实例化它:

public partial class TelephoneEntryForm : Form
{
    private Telephone telephone;

    public TelephoneEntryForm()
    {
        InitializeComponent();

        telephone = new Telephone();
    }

Then I'd probably subscribe to the KeyDown events for all those text fields, and save the values of each TextBox in their respective properties in your Telephone instance. 然后我可能会订阅所有这些文本字段的KeyDown事件,并将每个TextBox的值保存在Telephone实例的各自属性中。 Then in btnShowPhones_Click , use the values in the Telephone instance for display in the RichTextBox . 然后在btnShowPhones_Click ,使用Telephone实例中的值在RichTextBox显示。

This should give you enough information to make a go of it. 这应该为您提供足够的信息来实现它。 Let us know if you get stuck, and exactly where you're stuck, so someone can help you. 如果您遇到困难,请告诉我们,确切地说,如果您遇到困难,请联系我们。

Well, if I understand You correctly, You already have button Enter click event handler - btnEnter_Click method... That means that all code, which is written in this method, will execute when You click Enter button. 好吧,如果我理解你正确,你已经有了按钮Enter click事件处理程序 - btnEnter_Click方法......这意味着当你click Enter按钮时,所有用这个方法编写的代码都会执行。

private void btnEnter_Click(object sender, EventArgs e)
{
     // Read tha values of textboxes (not necessarily but more readable)
     string manufacturer = manufacturerTextBox.Text;
     string model = modelTextBox.Text;
     string phoneNumber = phoneNumberTextBox.Text;

     // Create new phone instance, based on entered data
     var telephone = new Telephone(manufacturer, model, phoneNumber);

     // Enter this data to reachtextbox
     richtextbox1.Text += string.Format("{0}\n", telephone);

     // Clear text boxes             
     manufacturerTextBox.Text = modelTextBox.Text = phoneNumberTextBox.Text = string.empty;
 }

but the thing is that during such scenario You will lose all entered data, because your ToString() method outputs only Manufacturer ... You should modify this method to output full telephone info... Something like: 但问题是在这种情况下您将丢失所有输入的数据,因为您的ToString()方法仅输出Manufacturer ......您应该修改此方法以输出完整的电话信息...类似于:

class Telephone
{
    ...
    public override string ToString()
    {
         return string.Format("Manufacturer: {0}; Model: {1}; Phone number: {2}", Manufacturer, Model, PhoneNumber)
    }
    ...
}

or save this data in any type of file. 或将此数据保存在任何类型的文件中。

Are you asking how to hook a button up to call your code? 你问的是如何挂一个按钮来调用你的代码? If that's the case, double-click on the button to create the event handler method. 如果是这种情况,请双击按钮以创建事件处理程序方法。 Within that method, add calls to whatever code you want. 在该方法中,添加对所需代码的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM