【问题标题】:Change highlighted text while looping throw data base c#循环通过数据库c#时更改突出显示的文本
【发布时间】:2016-02-19 13:04:48
【问题描述】:

对不起我的英语不好。 我的问题是(我根本没有在网上回答)

如何突出显示文本框中的某些文本,将其用于某些操作,然后当我“翻转”到下一个数据时,我将新选择的文本用于相同的操作。

我的 WinApp 示例: 我正在编写一个 WinApp 来读取一些数据库,获取所有信息并将其存储在“列表”中 然后我通过按钮(下一步)开始翻页,每次我这样做时我都希望应用程序突出显示 Textbox3 中的文本并发送键“ALT+Q”以触发我已经构建的一些操作。

我已经完成了这个程序,但是我有一个问题,当“翻转”到下一个“客户”信息页面时,程序会突出显示它的电话号码,并且 SendKeys “ALT+Q”......但是!当它触发动作时,它会执行 WinAPP 在程序开始时选择的第一个数字的命令。一些帮助请求? 我将发布我的 FORM CLASS(小编码)

表单加载:

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


         }

    }

下一个按钮:

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

** 每次我点击“下一步”时,即使程序突出显示新的 textbox3 值,当我发送“ALT + q”时,第一个突出显示的信息上的 acctoine accure 也不会改变。

感谢

【问题讨论】:

  • 我已经进行了更改,我已经删除了加载和按钮代码中的所有选择和发送键,并且我创建了一个函数“Call()”

标签: c# textbox highlight windows-applications selectall


【解决方案1】:

这并不能完全回答您的问题,但https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send%28v=vs.110%29.aspx 指出“如果您的应用程序旨在通过各种键盘在国际上使用,则使用 Send 可能会产生不可预知的结果,应避免使用。”

也就是说,尝试使用 Application.DoEvents() 而不是 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();
        }
    }
}

【讨论】:

  • 哦,别忘了 Focus()。
  • 没有工作,我已经调试了问题,我看到了一件奇怪的事情。如果我通过键盘上的“CTRL + A”选择文本,选择不起作用,我必须使用鼠标选择数字/文本,然后它就像鼠标一样工作,现在我搜索如何通过“crouser”选择文本而不真的用我的鼠标
  • 对于一个确认 CTRL+A 的文本框,您需要像这样注册一个 KeyDown 处理程序(在设计器文件中或通过 texbox1 属性): textbox1.KeyDown += new System.Windows.Forms .KeyEventHandler(this.textbox1_keyDown); ...然后将您的代码添加到类文件中: private void textbox1_keyDown(object sender, KeyEventArgs e) { if (e.Control & e.KeyCode == Keys.A) textbox1.SelectAll(); } ... ...但我们正在偏离主题。我想知道您的控件的属性是否会发生冲突。您能否从您的 Designer.cs 文件中粘贴 textbox1、textbox2 和 textbox3 的 sn-ps?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2019-01-25
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多