AutoCompleteBox 这个控件,到底要怎么样才能把光标定位上去?

zjyx 2013-06-26 04:49:48
大神们快来帮帮忙,搞两天了。

http://msdn.microsoft.com/zh-cn/library/system.windows.controls.autocompletebox(v=VS.95).aspx

可以看到这是个组合控件,经测试

.focus() 时焦点是在控件上,但不在TextBox里

如果手动用鼠标点上光标 .IsFocused 显示为 false

按Tab键切换,顺序应该是 控件本身 ->Popup -> TextBox


也不能用查找子类找到里面的TextBox,我现在怎么才能用代码把光标移到里面的TextBox上去?

快来救命呀,200分送上...
...全文
224 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjyx 2013-06-27
  • 打赏
  • 举报
回复
而且keybd_event是向当前发送。最小化就窗体后,仍然在执行。
zjyx 2013-06-27
  • 打赏
  • 举报
回复

public MainWindow()
        {
            InitializeComponent();

            var timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 1) };
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (!acp.IsFocused)
            {
                acp.Focus();

                keybd_event(0xD, 0, 0, 0);
            }
        }

如果正在输入的话,会导致框内容被选中,并覆盖。
zjyx 2013-06-27
  • 打赏
  • 举报
回复
谢谢adrianEvin的热心解答,加入回车是可以实现光标上去的。 我感觉这个控件在WPF是不是个BUG,因为加入回车后光标是在TextBox里,但是acp.IsFocused 返回的却是false,这样给判断带来很大的麻烦。 因为我是想实现一个条码输入框, 想用一个Timer定时判断焦点在不在输入框内,不在的话就定位过去,但是acp.IsFocused返回false的话就没办法了。 帮我想想有什么办法吧,其实要能够获取里面的TextBox就好办了。
adrianEvin 2013-06-27
  • 打赏
  • 举报
回复
。。。。。。还要添加Toolkit.dll,真麻烦。 试了的,其实是获取焦点了,如果手动再按个 回车就可以看到光标。 其实可以给了Focus()之后,再模拟键盘按一次就会就可以达到效果。 我之前是在web里面的可以直接获取焦点。 给你代码吧。暂时这样是可以获取焦点的。【0xD】是【回车】。
[DllImport("USER32.DLL")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);  //导入模拟键盘的方法

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            acp.Focus();
            keybd_event(0xD, 0, 0, 0);
        }
zjyx 2013-06-27
  • 打赏
  • 举报
回复
没有啊。我专门弄了窗体测试,就一句代码也不行。 我是在WPF里用的。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"                                          
        Title="MainWindow" Height="350" Width="525" FontSize="14">
    <Grid>
        
        <input:AutoCompleteBox Name="acp" IsTabStop="True" IsEnabled="True" Margin="40,136,29,145" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="40,76,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);

        }



        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            acp.Focus();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            acp.Focus();
           
            
        }

        

    }
}

adrianEvin 2013-06-27
  • 打赏
  • 举报
回复
你是不是在 autoCompleteBox1 的textchange事件里面写了东西了的啊
adrianEvin 2013-06-27
  • 打赏
  • 举报
回复
我怎么试的是 autoCompleteBox1.Focus(); text里面也有光标啊
zjyx 2013-06-27
  • 打赏
  • 举报
回复
谁来挽救我
zjyx 2013-06-27
  • 打赏
  • 举报
回复
zjyx 2013-06-26
  • 打赏
  • 举报
回复
IsEnabled 肯定是 true 鼠标可以点上去
吴青峰 2013-06-26
  • 打赏
  • 举报
回复
你是不是在什么地方设置了IsEnabled的值,注意要控制好。
吴青峰 2013-06-26
  • 打赏
  • 举报
回复
将控件AutoCompleteBox的IsEnabled属性设置为true看看。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧