none
Change the character to Upper case when I keying RRS feed

  • Question

  • Dear all,

               Thanks for all your help for newbie as me. Now I want to enter some letter to datagridview cell. I want to change the letter key in to Upper case during my keying process. I mean if key "a" then the Cell will show "A", and next character is "b" then the Cell show "AB". How can I do that? Thanks.


    hon123456
    Saturday, January 22, 2011 9:07 AM

Answers

  • First, add this event to the designer.cs or to the Form1 methos right after InitializeComponent() :

    this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);

     

    Then. use this code:

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
          dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
        }
    
        void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
        {
          e.Handled = true;
          TextBox tb = sender as TextBox;
          tb.Text = tb.Text.ToUpper() + e.KeyChar.ToString().ToUpper();
        }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Sunday, January 23, 2011 8:55 AM
    • Marked as answer by hi123456 Monday, January 24, 2011 2:09 AM
    • Unmarked as answer by hi123456 Monday, January 24, 2011 5:37 AM
    • Marked as answer by hi123456 Monday, January 24, 2011 8:06 AM
    Sunday, January 23, 2011 8:54 AM

All replies

  • Hi again ;)

    Try

    
       private void dataGridView1_CellValidated(object sender, DataGridViewCellEventArgs e)
      {
    
       if (!dataDridView1.Rows[e.RowIndex].IsNewRow &&(dataGridView1[e.ColumnIndex,e.RowIndex].Value==null ) )
       {
        MessageBox.Show("請輸入貨品編號");
        return;
       }
    
       else 
       {
        if(typeof(string).Equals(dataGridView1.Coulmns[e.ColumnIndex].ColumnType/*or DataType, I don't remember*/))
         dataGridView1[e.ColumnIndex,e.RowIndex].Value=dataGridView1[e.ColumnIndex,e.RowIndex].Value.ToString().ToUpper();
        CheckStockcode();
       }
    
    

    LEARN HOW TO USE WINDOWS API DURING A QUICK, SIMPLE AND PRACTICAL HOW TO:
    How To: Changing TextBox blinking caret using Windows API
    Saturday, January 22, 2011 9:23 AM
  • Thanks Yasser . I will try it tomorrow. Thanks very much. But my question is if it can change each charater that I typed to UpperCase. Not until all the

    character are entered and then change to Upper Case wholely. Thanks.


    hon123456
    Saturday, January 22, 2011 9:30 AM
  • First, add this event to the designer.cs or to the Form1 methos right after InitializeComponent() :

    this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing);

     

    Then. use this code:

        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
          dataGridView1.EditingControl.KeyPress += new KeyPressEventHandler(EditingControl_KeyPress);
        }
    
        void EditingControl_KeyPress(object sender, KeyPressEventArgs e)
        {
          e.Handled = true;
          TextBox tb = sender as TextBox;
          tb.Text = tb.Text.ToUpper() + e.KeyChar.ToString().ToUpper();
        }
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Sunday, January 23, 2011 8:55 AM
    • Marked as answer by hi123456 Monday, January 24, 2011 2:09 AM
    • Unmarked as answer by hi123456 Monday, January 24, 2011 5:37 AM
    • Marked as answer by hi123456 Monday, January 24, 2011 8:06 AM
    Sunday, January 23, 2011 8:54 AM
  • Dear all,

                I have trid above code, it works in first row. But when it comes to second row and third row, every character I typed will be repeated 2 times and 3 times. What can I do to fix this. Thanks.


    hon123456
    Monday, January 24, 2011 5:39 AM
  • Thanks very much for all your help.
    hon123456
    Monday, January 24, 2011 8:06 AM
  • Isn't it simpler changing just the input char to upper?

      private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
      {
        e.Control.KeyPress -= new KeyPressEventHandler(Control_KeyPress);
        e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
      }
    
      void Control_KeyPress(object sender, KeyPressEventArgs e)
      {
        e.KeyChar = char.ToUpper(e.KeyChar);
      }
    
    • Proposed as answer by pjb58 Monday, November 9, 2015 3:38 AM
    Monday, January 24, 2011 2:09 PM