在鼠标单击PictureBox上时弹出TextBox以添加到网格视图

时间:2011-08-21 10:57:29

标签: c# textbox popup picturebox

我尝试以不同的方式将http://stackoverflow.com/questions/5549150/popping-up-a-textbox-on-mouse-click-over-a-picturebox-for-adding-custom-note-to-p应用于我的项目。当我点击图片框时,文本框应该出现,在关闭后点击的位置输入值应该逐行转到datagridview与之前的数据

但是在这种方法中,以前的数据一直都会清除。我该如何调整

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; 
 }

1 个答案:

答案 0 :(得分:0)

我认为要解决您的问题,您需要执行以下操作:

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);       
 }