0

sorry for my bad english . my qustion is (i didnt fien answer on net at all)

how can i hightlight some text from a textbox , use it for some action , and then when i "flip" to next data , i use the new selected text for the same action .

Example from my WinApp : I am coding a WinApp the read some data base , takes all the info and store it on a 'List ' then i start flip pages via button (next) , and every time i do it i want the app to highlight the text in Textbox3 and sendkey "ALT+Q" for triger some action the i have already built .

i already did the program but i have one problim , when a "flip" fot the next "customer" info page , the program highlights its phone number , and SendKeys "ALT+Q" ... BUT! when it trigers the acction , it excecute the commands for the first number WinAPP selects on the start of the program . some help pleas ? i will post my FORM CLASS (small coding )

Form Load:

private void Empcalls_Load(object sender, EventArgs e)
    {
        OpenFileDialog OpenFile = new OpenFileDialog();
        if (OpenFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            MainDB = new DataTable();
            Dbop = new Classes.DbConnection();
            rList = new List<int>();
            int num = 0;
            Random ran = new Random();
            string filepath = OpenFile.FileName;
             MainDB = Dbop.CustomerData(filepath);
             for (int i = 0; i < MainDB.Rows.Count-1; i++)
             {
                 do { num = ran.Next(0, (MainDB.Rows.Count-1)); } while (rList.Contains(num));
                 rList.Add(num);
             }
             textBox1.Text = MainDB.Rows[rList[mainindex]]["שם name"].ToString();
             textBox2.Text = MainDB.Rows[rList[mainindex]]["שם lastName"].ToString();
             if (MainDB.Rows[rList[mainindex]]["phone"].ToString().Replace("-", "")=="")
                 textBox3.Text = MainDB.Rows[rList[mainindex]]["phone2"].ToString().Replace("-", "");
             else
                textBox3.Text = MainDB.Rows[rList[mainindex]]["טלפון נייד"].ToString().Replace("-","");
             currNum = textBox3.Text;
             mainindex++;
             System.Threading.Thread.Sleep(1000);
             textBox3.SelectAll();
             SendKeys.Send("%(q)");


         }

    }

Next Btn:

private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = MainDB.Rows[rList[mainindex]]["name"].ToString();
            textBox2.Text = MainDB.Rows[rList[mainindex]]["lastName"].ToString();
            if (MainDB.Rows[rList[mainindex]]["pone"].ToString().Replace("-", "") == "")
                textBox3.Text = MainDB.Rows[rList[mainindex]]["phone"].ToString().Replace("-", "");
            else
                textBox3.Text = MainDB.Rows[rList[mainindex]]["טלפון phone2"].ToString().Replace("-", "");
            textBox3.Focus();
            currNum = textBox3.Text;
            if (mainindex >= rList.Count-1)
                mainindex = 0;
            else
                mainindex++;
            textBox3.SelectAll();
            System.Threading.Thread.Sleep(1000);
            textBox3.SelectAll();

            SendKeys.Send("%(q)");
            Clipboard.Clear();
}

** every time i hit 'Next' even the program highlight new textbox3 value , when i send the 'ALT + q' the acctoine accure on the first Highlighted info it dosnt change .

thanx

wafihsn
  • 5
  • 4
  • i have made changes i have removed all the seletion and sendkeys fro the Load and Button code , and i have created a function "Call()" – wafihsn Nov 17 '15 at 23:48

1 Answers1

0

This doesn't quite answer your question, but https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send%28v=vs.110%29.aspx states "If your application is intended for international use with a variety of keyboards, the use of Send could yield unpredictable results and should be avoided."

That said, try using Application.DoEvents() instead of Sleep...

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;
using System.Threading;

namespace TestFormFieldCycle
{
    public partial class Form1 : Form
    {
        List<string> rList = new List<string>();
        int mainIndex = 0;

        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 100; i++)
            {
                int areaCode = 0;
                int phone1 = 0;
                int phone2 = 0;
                String phoneNumber = String.Empty;
                do
                {
                    Random r = new Random();
                    areaCode = r.Next(0, 999);
                    phone1 = r.Next(0, 999);
                    phone2 = r.Next(0, 9999);
                    phoneNumber = String.Format("({0:000}) {1:000}-{2:0000}", areaCode, phone1, phone2);
                } while (rList.Contains(phoneNumber));
                rList.Add(phoneNumber);
            }
        }

        void renderAndCaptureText1()
        {
            textBox1.Text = rList[mainIndex].ToString();
            string currentPhoneNumber = textBox1.Text;
            mainIndex++;
            textBox1.Focus();
            textBox1.SelectAll();
            SendKeys.Send("^(c)");
            Application.DoEvents();
            textBox2.Focus();
            textBox2.Clear();
            SendKeys.Send("^(v)");
            Application.DoEvents();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            renderAndCaptureText1();
        }
    }
}
Paurian
  • 1,372
  • 10
  • 18
  • Oh - and don't forget the Focus(). – Paurian Nov 18 '15 at 01:24
  • didnt work , i have debuged the problem , and i saw one thing that is strange. if i select TEXT Via "CTRL + A" on the keyboord , the selection dosnt work , i must select number/Text using mouse , then it work , its like the mouse , now im search how can i select text by "crouser" without realy use my mouse – wafihsn Nov 18 '15 at 13:03
  • For a textbox to acknowledge CTRL+A, you'll want to register a KeyDown handler like this (in designer file or through the texbox1 properties): textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textbox1_keyDown); ... then add your code to the class file: private void textbox1_keyDown(object sender, KeyEventArgs e) { if (e.Control & e.KeyCode == Keys.A) textbox1.SelectAll(); } ... ... But we're getting off topic. I'm wondering if your control's properties could conflict. Could you please paste the snippets for textbox1, textbox2 and textbox3 from your designer.cs file? – Paurian Nov 18 '15 at 17:25