如何将字符串从类返回到文本框 [英] How to return the string from class to textbox

查看:77
本文介绍了如何将字符串从类返回到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我使用传递一些值的createdatagrid()函数在运行时创建了一个datagridview,并创建了该datagridview的keypress事件.
在该按键事件中,当用户按下Enter键(即13)时,我将所选行设置为文本框.

上面提到的功能可以正常工作.

现在我要在班级上做.

例如我想将textbox5的值传递给createdatagrid()或filldata,然后将结果显示回textbox5.

注意:我已经成功传递了其他值,但是我不明白如何传递textbox5的值以及如何从函数中取回结果.

以下是代码
请帮帮我.

谢谢.

Hello friends,

I have created a datagridview at runtime using the createdatagrid() function passing some values and created the keypress event of that datagridview.
In that keypress event I set the selected row to the textbox when user presses the enter key i.e 13.

The above mentioned functionality works fine.

Now I want to make it at class level.

For e.g. I want to pass the textbox5 value to the createdatagrid() or filldata and then display the result back to the textbox5.

Note: I have successfully passed other values but I don''t understand how to pass the value of textbox5 and how should I take the result back from function.

Following is the code
Please help me.

Thank you.

public void createdatagrid(int left,int top,int width,int height,)
  {
      dgvnew = new DataGridView();
      dgvnew.Left = left;
      dgvnew.Top = top;
      dgvnew.Size = new System.Drawing.Size(width,height);
      dgvnew.ReadOnly = true;
      dgvnew.RowHeadersWidth = 5;
      dgvnew.ScrollBars = ScrollBars.Vertical;
      this.Controls.Add(dgvnew);
      dgvnew.KeyPress += new KeyPressEventHandler(dgvnew_KeyPress);
  }
  public void filldata(string spname)
  {
      //fill datagirdview
      dsdgv = new DataSet();
      dsdgv = clsobj.getDataset(spname);
      dgvnew.DataSource = dsdgv.Tables[0];
      dgvnew.AutoSize = false;
      for (int i = 0; i < dsdgv.Tables[0].Rows.Count - 1; i++)
          dgvnew.AutoResizeColumn(i);
          dgvnew.Show();
  }
  void dgvnew_KeyPress(object sender, KeyPressEventArgs e)
  {
      if (e.KeyChar == 13)
      {
          textBox5.Text = dgvnew.Rows[dgvnew.CurrentRow.Index - 1].Cells[0].Value.ToString();
          dgvnew.Hide();
      }
  }
  private void textBox5_TextChanged_1(object sender, EventArgs e)
  {
      createdatagrid(550, 172, 300, 200);
      filldata("SP_PC");
  }



编辑语法和拼写[/EDIT]



Edited for grammar and spelling [/EDIT]

推荐答案

首先,每次文本框的值更改时创建一个新的datagridview并不是一个好主意.为什么不只是回收当前的datagridview.其次,如果datagridview更改了文本框,则文本框将通过更改datagridview做出反应-这通常不是一个好兆头.最终将无限触发效果.
First, creating a new datagridview everytime your textbox''s value changes is not such a good idea. Why don''t you just recycle the current datagridview. Second,if the datagridview changes the textbox, the textbox will react by changing the datagridview -- this is generally not a good sign. It will end up in infinite triggering of effects.



最简单的方法是将整个文本框作为参数传递给类.
另一种方法是在主窗体中创建一个委托并将其传递给您的类,类似于回调函数,然后在该函数中更改您的文本框值.

The easiest way is to pass whole your textbox as a parameter to your class.
Another way is to have a delegate in your main form and pass it to your class, something like callback function and change your textbox value in that function.


这篇关于如何将字符串从类返回到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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