同學要做
像是打AA BB CC
文字游標在CC之後
按ctrl+A把DatagridView顯示在文字游標旁
DatagridView會顯示資料庫裡開頭是CC的值(假設是CCDD
然後選擇CCDD後
會取代掉CC
變成AA BB CCDD
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
我幫忙的部分沒有資料庫那一塊
所以就是
像是打AA BB CC
文字游標在CC之後
按ctrl+A把DatagridView顯示在文字游標旁
然後有兩種
1.
同學做的是用取代的方式
按DatagridView會把CC取代成我要的(假設是CCDD)
小問題是如果前面也有CC
如AA BB CC AA CC
我只要取代後面的CC但是前面的也會一起被取代
變成AA BB CCDD AA CCDD
2.
我做的放在Button
用 刪除字(空白後());插入字串();
也可以把CC取代成我要的
但是小問題是換行不算空白
所以會連前一行的最後一個字串一起刪掉
------------------------------------------------------------------------------------------------------------------------------------
string cmdText, insertText, txt;
string[] stringArray;
int txt1;
Point p, P;
private void textBox1_KeyDown(object sender, KeyEventArgs e)//按ctrl+A把DatagridView顯示在文字游標旁
{
if (e.KeyCode == Keys.A && e.Control == true)
{
if (e.KeyCode == Keys.A)
{
p = GetCursorPos(this.textBox1.Handle.ToInt32());
this.label1.Text = string.Format("Line:{0}, Col:{1}", p.X, p.Y);
P.X = (p.Y) * (int)textBox1.Font.Size + textBox1.Location.X ;
P.Y = (p.X+1) * (int)textBox1.Font.Size + textBox1.Location.Y;
dataGridView1.Location = P;
dataGridView1.Show();
//抓空白
stringArray = textBox1.Text.Split(' ', '\n');
//抓陣列最後一個字串
txt = stringArray[stringArray.Length - 1];
}
}
}
//
#region 游標在第幾行第幾個
const int EM_GETSEL = 0xB0;
const int EM_LINEFROMCHAR = 0xC9;
const int EM_LINEINDEX = 0xBB;
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage( int hwnd, int wMsg,int wParam,ref int lParam );
public Point GetCursorPos(int TextHwnd)
{
int i = 0, j = 0, k = 0;
int lParam = 0, wParam = 0;
i = SendMessage(TextHwnd, EM_GETSEL, wParam, ref lParam);
j = i / 65536;
int lineNo = SendMessage(TextHwnd, EM_LINEFROMCHAR, j, ref lParam) + 1;
k = SendMessage(TextHwnd, EM_LINEINDEX, -1, ref lParam);
int colNo = j - k + 1;
Point ret = new Point(lineNo, colNo);
return ret;
}
#endregion
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//將選取內容插入鼠標位置
string insertText = "選取內容";
int insertPos = textBox1.SelectionStart;
//刪除游標所在的陣列
textBox1.Text = textBox1.Text.Replace(txt, insertText);
textBox1.SelectionStart = insertPos + insertText.Length;
//關閉dataGridView
dataGridView1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)//刪除游標前一個字元加入我要的字串
{
刪除字(空白後());
插入字串();
}
public void 刪除字(int CC)
{
int insertPos = textBox1.SelectionStart;
char[] c = textBox1.Text.ToCharArray();
textBox1.Text = "";
for(int y=0;y< CC;y++)
c[insertPos - 1-y] = ' ';
for (int i = 0; i < c.Length; i++)
{
bool BB = true;
for (int y = 0; y < CC; y++)
if (i == insertPos - 1 - y)
{
BB = false;
break;
}
if (BB)
textBox1.Text += c[i];
}
}
public int 空白後()
{
int CC = 0;
int insertPos = textBox1.SelectionStart;
insertPos--;
char[] c = textBox1.Text.ToCharArray();
while(c[insertPos--]!=' ')
{
CC++;
}
return CC;
}
public void 插入字串()
{
string insertText = "";
int insertPos = textBox1.SelectionStart;
textBox1.Text = textBox1.Text.Insert(insertPos, insertText);
textBox1.SelectionStart = insertPos + insertText.Length;
}
沒有留言:
張貼留言