简体   繁体   English

鼠标单击弹出框弹出一个文本框以添加到网格视图

[英]Popping up a TextBox on mouse click over a PictureBox for adding to grid view

I try to apply http://stackoverflow.com/questions/5549150/popping-up-a-textbox-on-mouse-click-over-a-picturebox-for-adding-custom-note-to-p to my project in different way.When I click on picturebox, textbox should appear and after it closed on that clicked position that entered value should go to datagridview by row by row with previous data. 我尝试将http://stackoverflow.com/questions/5549150/popping-up-a-textbox-on-mouse-click-over-a-picturebox-for-adding-custom-note-to-p应用于我的项目当我单击图片框时,应显示文本框,并在该单击位置将其关闭后,输入的值应与以前的数据逐行转到datagridview。

But in this method all the time previous data will clear. 但是在这种方法下,以前的数据将一直清除。 How should I adjust it 我应该如何调整

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
  // in this case we create a TextBox, but the
  // PopupForm can hold any type of control.
  TextBox textBox = new TextBox();
  Point location = pictureBox1.PointToScreen(e.Location);
  PopupForm form = new PopupForm(textBox, location, () => this.addToGrid(textBox.Text,e.Y)); 
  form.Show();
}


 private void addToGrid(String s,int loc)
 {
     DataGridViewRow row = new DataGridViewRow(); this.dataGridView1.Rows.Add(row);
     this.dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[0].Value = loc.ToString();
     this.dataGridView1.Rows[dataGridView1.RowCount - 1].Cells[1].Value = s; 
 }

I think to resolve your problem you need to do something like this: 我认为要解决您的问题,您需要执行以下操作:

private void addToGrid(String s,int loc)
 {
     //create a new row
     DataGridViewRow row = new DataGridViewRow();

     //after initialize  values
     row.Cells[0].Value = loc.ToString();
     row.Cells[1].Value = s; 

     //add row to grid
     this.dataGridView1.Rows.Add(row);       
 }

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

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