修改其他类中的变量 [英] Modifying variables in other classes
问题描述
我真的不知道我在做什么,我看到数百个论坛上有类似的帖子,涉及访问其他形式和类等,但是我不知道他们在说什么.我对汇编器和C没问题,但是我不懂C#
然而.我想做的是拥有一个函数或类",在文本框中更改文本的值. 似乎很简单,但是无论我怎么键入它,都会出现一些错误,这些错误在Google上进行搜索进一步困扰了我.甚至
如果有人因为之前已经做过某事而不想回答这个问题,我将不胜感激,这只手为我指明了正确的方向!
I really don't know what I'm doing and I see hundreds of forums with similar posts involving accessing other forms and classes and whatnot, however I have no idea what they are talking about. I am okay at assembler and C, but I don't understand C#
yet. What I'm trying to do is have a function or "class," change the value of the text in a textbox. Seems simple enough, however no matter how I type this, I get some error which searching for on Google baffles me further. Even
if someone isn't going to answer this question because it's been done before or something, I would really appreciate a hand pointing me the right direction!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Texter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void send_button_Click(object sender, EventArgs e)
{
TextBuffer.ShowText(send_box.Text);
send_box.Text = String.Empty;
recv_box.Text = TextBuffer.Text;
}
}
public class TextBuffer
{
object stuff;
public static string Text;
public static bool ShowText(string TextForAppending)
{
TextBuffer.Text += TextForAppending;
TextBuffer.Text += Environment.NewLine;
return true;
}
}
}
这是我当前的代码,使用GUI创建表单时,空白页几乎没有修改.我在表单的顶部有一个大文本框,"recv_box" 和一个小的"send_box"按钮"send_button"旁边.
我让send_button将文本发送到TextBuffer,然后让它将TextBuffer的内容写入recv_box. 如果我手动声明
This is my current code, barely modified from the blank page when you use the GUI to create a form. I have a large text box in the top part of the form, "recv_box" and a small one "send_box" beside a button "send_button."
I have the send_button send the text to TextBuffer, and then have it write the contents of TextBuffer to recv_box. If I manually state
recv_box.Text = TextBuffer.Text;
然后它可以工作,但是我不希望按钮更新框,我希望TextBuffer可以更新屏幕,所以我尝试将这一行放入TextBuffer类中,但它不起作用.我尝试写作
then it works, but I don't want the button to update the box, I want TextBuffer to update the screen, so I tried puting this line in the TextBuffer class and it didn't work. I tried writing
Form1.recv_box.Text = TextBuffer.Text;
无济于事. 在Form1内部,我尝试了一种方法" 可以,但是在那也不行. 我想做的事情很简单,而且我知道如何在汇编器中做到这一点,但是我不知道如何在C#中做到这一点!
to no avail. Inside Form1 I tried making a "method" which does it, but it doesn't work there either. What I'm trying to do is so simple, and I know how I could do it in assembler, but I just have no idea how to do it in C#!
我们将不胜感激!谢谢!
Help would be appreciated! Thank you!
推荐答案
您想让该类更改文本框的值吗?在您的类中,您必须将Form1的实例传递给它.例如:
You want to have the class changing the value of a textbox? In your class you must pass the instance of Form1 to it. For example:
namespace Texter
{
public partial class Form1 : Form
{
private TextBuffer textBuffer = new TextBuffer(this);
public Form1()
{
InitializeComponent();
}
private void send_button_Click(object sender, EventArgs e)
{
TextBuffer.ShowText(send_box.Text);
send_box.Text = String.Empty;
recv_box.Text = TextBuffer.Text;
}
}
public class TextBuffer
{
object stuff;
public static string Text;
private Form form;
public TextBuffer(Form form)
{
this.form = form;
}
public static bool ShowText(string TextForAppending)
{
form.recv_box.Text += TextForAppending;
form.recv_box.Text += Environment.NewLine;
return true;
}
}
}
您需要的东西.
这篇关于修改其他类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!