×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

就顶一次。随便凑满三题。本论坛.NET好手众多,欢迎大家指正

在WinForm中加控件ListBox1(ListBox)。并有如下代码,当事件ListBox1.SelectedIndexChanged发生, Private Sub ABC()被调用。如果我按住向下键不放,ListBox1.SelectedIndex连续变化,ListBox1.SelectedIndexChanged连续发生,Private Sub ABC()被连续调用。问题:我只要最后一个SelectedIndexChanged事件(松开向下键)或选项滚到底( ListBox1.SelectedIndex = 1000 )的时候,Private Sub ABC()才被调用,中间的SelectedIndexChanged事件不调用Private Sub ABC()。


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
ListBox1.Items.Clear()
For i = 0 To 1000
ListBox1.Items.Add(i)
Next
ListBox1.SelectedIndex = 0
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ABC()
End Sub
Private Sub ABC()

End Sub
Report

Replies, comments and Discussions:

  • 工作学习 / 职位情报 / To feihong,guestagain: 我也来两道.NET题目, 不算难. 不过能实现至少我认为能力不错.
    1. 在WinForm 中添加控件TextBox(TextBox1), Button(Button1),Button(Button2). 并添加如下代码. 如果TextBox1是Focused. 无论click Button1或Button2, Private Sub TextBox1_Validating 的代码执行. 问题: 如果click Button1, 我不执行Private Sub TextBox1_Validating 的代码; 如果click Button2, Private Sub TextBox1_Validating 的代码照常执行; 请将下面代码改进.

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If Not IsValidated() Then
    e.Cancel = True
    End If
    End Sub

    Private Function IsValidated() As Boolean
    'Do Validating
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    2. 第二问题是如何Disable WinForm 右上角的按钮"X". 不用Windows API, 纯.NET
    • 就顶一次。随便凑满三题。本论坛.NET好手众多,欢迎大家指正
      在WinForm中加控件ListBox1(ListBox)。并有如下代码,当事件ListBox1.SelectedIndexChanged发生, Private Sub ABC()被调用。如果我按住向下键不放,ListBox1.SelectedIndex连续变化,ListBox1.SelectedIndexChanged连续发生,Private Sub ABC()被连续调用。问题:我只要最后一个SelectedIndexChanged事件(松开向下键)或选项滚到底( ListBox1.SelectedIndex = 1000 )的时候,Private Sub ABC()才被调用,中间的SelectedIndexChanged事件不调用Private Sub ABC()。


      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim i As Integer
      ListBox1.Items.Clear()
      For i = 0 To 1000
      ListBox1.Items.Add(i)
      Next
      ListBox1.SelectedIndex = 0
      End Sub

      Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
      ABC()
      End Sub
      Private Sub ABC()

      End Sub
      • 第二题答案: this.ControlBox=false;
        private void Form1_Load(object sender, System.EventArgs e)
        {
        this.ControlBox=false;
        }

        这样做将把所有的三个控制按钮全部抹掉,好象并不是搂主所讲的只将close按钮抹掉,但这是我能想到的最好的办法了。还有更好的方法,希望大家贴出来
        • 这不是作者要得答案。systemMenu 还要的。
        • 这题面试我肯定不会。没做过。我只知道VC++里CreateWindow可以做。不过google 一下答案就有了。用CreateParams 。不过看MSDN 里的用Style 不灵,一定要ClassStyle
    • If you focus on this kind of "Skill", then you or what you are looking for is too low. It is just like 123 x 123 = ? which is for my calculator not for me.
      • 说这个没意思。这些东西都是有迹可寻的。就说我今天的题,如果你的电脑是Windows系统,那么请你打开Explore, 用上下键去滚动左边Folders栏里的项,看看右边栏的反应。
    • I can answer. But is it possible to ask so simple questions in a .NET job interview? Last time I answered phone from RA_95, the questions were not like that. Most of them are concepts of .NET.
      • 那就将你的答案贴出来。你要能回答至少我觉得你.NET能力不错,没别的意思。这是对於答案以外回贴的最后跟贴
    • how could I know how much memory has been used by one application running on Windows?
      • You mean Process.WorkingSet Property ?. BTW. Pls ask question in separated post.
    • These kind of questions is totally unuseful..Let me ask you a .net question,what is the basic pattern of async communication .
      • What is your mean "basic pattern"?
      • Come on. Can't you search keyword asynchronous in title of your MSDN for .NET. Pls read whatever you want.
      • 闲着也闲着。.NET里的异步主要两个方法: BeginInvoke和EndInvoke. 其实就是CallBack.
        调用Asynchronous Delegate的EndInvoke时还在子thread. 要回到Main Thread. 必须用ISynchronizeInvoke.Invoke Method 。一个现成实现这个接口的方法是System.Windows.Forms.Control.Invoke Method (Delegate, Object[])
        • well,maybe I am not make it clearly enough,what I mean is the design pattern
          • Asynchronous Design Pattern Overview
        • Multi-tier async call design pattern,that means you need to specify the async call across the physical boundary.
      • 我觉得这个问题换一下问,举一个例子,使用异步模式解决一个实际问题。还有一个问题,大家对Web Services 如何看,优点缺点,何时使用,如何使用.
      • 我只会用C/C++自己实现异步通讯,至于用什么API来调用,这个就比较简单了
    • Hopefully you don't have opportunity to ask this kind of question:))
      • Exactly what i am thinking too...
      • 和ra_95的问题比起来,我宁愿被问这样的问题,
        • What is ra_95's question?
          • 留着他下次问你吧。
            • I searched history. I don't think I will give him this opportunity.:)
        • 我不会.NET...所以....比较恶心....lol
          • Agent?
    • 罗嗦了半天,除了都想证明自己水平很高,不屑一顾之外,怎么没一个人做做题?没必要这样曲高和寡吧!我来作下第三题
      本文发表在 rolia.net 枫下论坛vb.net会 ,但是用着不习惯,用C#来写。
      此题关键是考你event和win form 键盘输入的知识,还是能考出面试者一些功底的,太晚了,明天再作上面2道

      // define a bool just to let listbox judge whether to run ABC() or not
      private bool DownKeyPressed=false;

      private void Form1_Load(object sender, System.EventArgs e)
      {
      for(int i=1;i<=1000;i++)
      this.listBox1.Items.Add(i);
      // add the key up and down event to the listbox
      this.listBox1.KeyDown+=new KeyEventHandler(OnKeyDown);
      this.listBox1.KeyUp+=new KeyEventHandler(OnKeyUp);
      }

      // the key up handler function
      void OnKeyUp(object o, KeyEventArgs e)
      {
      if(e.KeyCode==Keys.Down)
      {
      int i=this.listBox1.SelectedIndex;
      this.listBox1.SelectedIndex=0;
      this.DownKeyPressed=false;
      this.listBox1.SelectedIndex=i;
      }
      }

      // the key down handler function
      void OnKeyDown(object o,KeyEventArgs e)
      {
      if(e.KeyCode==Keys.Down)
      this.DownKeyPressed=true;
      }

      private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
      {
      if(this.DownKeyPressed==true && this.listBox1.SelectedIndex<999)
      return;
      ABC();
      }更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • I don't know if I can do or not. I just not interested doing any similar thing I need to do during daytime.
      • 思路不错. 不过我记得SelectedIndexChanged事件先于KeyDown事件发生. 所以有待改进.
        • 对不起. 多年前的版本是我记得SelectedIndexChanged事件先于KeyDown事件发生. 刚刚我在VS2003上试一下, 这方法可以.
          不过如果实际应用, 需要判断的键太多(UP,Down,PageUp,PageDown等). 请见你的PM
    • 第一题答案.....
      就是在 validating 函数里加一句判断

      private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
      {
      if(this.button2.Focused==true)
      {
      If (!IsValidated())
      e.Cancel = True
      }
      }
      • 你测试过吗?
        • 对不起. 我测试了一下,可以的. 好方法
      • 对不起, 我可以将题目变化一下否?
        如何在通过系统菜单(窗口的左上角菜单)或右上角的按扭"X"关闭窗口时不调用Validating中的代码.