Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Enter umjesto Tab

[es] :: .NET :: Enter umjesto Tab

[ Pregleda: 2377 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

mladenmp

Član broj: 193827
Poruke: 51
89.188.32.*

Jabber: info@mladenmp.ml
Sajt: www.mladenmp.ml


+1 Profil

icon Enter umjesto Tab10.10.2008. u 01:44 - pre 189 meseci
Evo polako zavrasvam program, pa kao prvo red bi bio da se zahvalim svima Vama koji ste mi pomogli.
Sad me samo interesuje kako da umjesto TAB-a koristim ENTER za prelazak iz text boxa u drugi text box.
Mislim ostalo mi je jos to sminkanje programa, a enter mi dodje bolji od taba za neki unos.
S tim sto bih zadrzao i opciju TAB (dakle ako se pritisne TAB preazi se u novi texbox i ako se pritisne enter prelazi se u textbox)
Jos jednom hvala svima koji su mi pomogli. Eto i ja sam nesto naucio od vas, pa sad mogu to da podijelim i sa drugima

 
Odgovor na temu

Igor Gajic

Član broj: 93194
Poruke: 747
*.ADSL.neobee.net.



+987 Profil

icon Re: Enter umjesto Tab10.10.2008. u 06:19 - pre 189 meseci
Potrebno je samo da obradis Keydown event, i da dodas sledeci kod:


Code:

        private void BazaTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.Handled = true;
                this.SelectNextControl((Control)sender, true, true, true, true);
            }
        }


tako ces prelaziti na sledecu kontrolu i sa Enter i sa TAB.
 
Odgovor na temu

mladenmp

Član broj: 193827
Poruke: 51
89.188.32.*

Jabber: info@mladenmp.ml
Sajt: www.mladenmp.ml


+1 Profil

icon Re: Enter umjesto Tab10.10.2008. u 11:18 - pre 189 meseci
Hvala na odgovoru. Ja sam zaboravio da jos sinoc (tj. jutros) postavim da sam rijesio problem.
Jos jednom hvala, ako neko bude imao isti problem, evo kako sam ja to rijesio:
Code:

    Private Sub u1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles u1.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            If sender.name = "u1" Then
                e.Handled = True
                u2.Focus()
            ElseIf sender.name = "u2" 
                e.Handled = True
                u3.Focus()
            ElseIf sender.name = "u3"
                e.Handled = True
                u4.Focus()
            ElseIf sender.name = "u4"
                e.Handled = True
                u5.Focus()
            ElseIf sender.name = "u5"
                e.Handled = True
                u6.Focus()
            ElseIf sender.name = "u6"
                e.Handled = True
                u7.Focus()
            ElseIf sender.name = "u7"
                e.Handled = True
                u8.Focus()
                        End If
        End If

    End Sub

u1, u2, u3...su imena text boxova
 
Odgovor na temu

DarkMan
Darko Matesic

Član broj: 20445
Poruke: 572
77.46.221.*

Jabber: DarkMan


Profil

icon Re: Enter umjesto Tab10.10.2008. u 12:02 - pre 189 meseci
Mozes i ovako:
Code:

    public class Form1: Form
    {
        ...
        public Form1()
        {
                ...
                FormHelpers.SetupFormKeyboardEvents(this, true, true);  // samo dodas ovu liniju u formu
        }
    }


Code:

    public class FormHelpers
    {
        /// <summary>
        /// Adds KeyDown event listener which closes form on Escape key, 
        /// and KeyPress event which selects next form control on Enter
        /// </summary>
        public static void SetupFormKeyboardEvents(Form form, bool closeOnEscape, bool selectNextControlOnEnter)
        {
            form.KeyPreview = true;
            if(closeOnEscape) {
                form.KeyDown += new KeyEventHandler(form_KeyDown_CloseOnEscape);
                form.CancelButton = null;
            }
            if(selectNextControlOnEnter) {
                form.KeyDown += new KeyEventHandler(form_KeyDown_SelectNextControlOnEnter);
                form.AcceptButton = null;
            }
        }
        public static void RemoveFormKeyboardEvents(Form form)
        {
            form.KeyDown -= form_KeyDown_CloseOnEscape;
            form.KeyDown -= form_KeyDown_SelectNextControlOnEnter;
        }

        #region SetupFormKeyboardEvents handlers

        private static void form_KeyDown_CloseOnEscape(object sender, KeyEventArgs e)
        {
            try { if(e.KeyCode == Keys.Escape) ((Form)sender).Close(); } catch { };
        }
        private static void form_KeyDown_SelectNextControlOnEnter(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter) FormHelpers.SelectFormNextControl((Form)sender, true);
        }

        #endregion

        /// <summary>
        /// Moves focus to next control (and selects all text if it is a text box)
        /// </summary>
        /// <param name="form">Form that contains controls to inspect and move focus.</param>
        /// <param name="tabStopOnly">If true, inspect only controls with TabStop set.</param>
        public static void SelectFormNextControl(Form form, bool tabStopOnly)
        {
            Control control = GetContainerActiveControl(form);
            if(form != null && control != null) {
                try {
                    bool isTextBoxBaseSubclass = control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBoxBase));
                    if(isTextBoxBaseSubclass && ((TextBoxBase)control).Multiline) return;
                    form.SelectNextControl(control, true, tabStopOnly, true, true);
                    if(isTextBoxBaseSubclass) ((TextBoxBase)control).SelectAll();
                } catch { }
            }
        }

        /// <summary>
        /// Returns container currently active control (not container control)
        /// </summary>
        /// <returns>Returns null if it fails</returns>
        public static Control GetContainerActiveControl(ContainerControl containerControl)
        {
            if(containerControl == null) return null;
            Control control = containerControl.ActiveControl;
            while(control != null && control.GetType().IsSubclassOf(typeof(ContainerControl))) control = GetContainerActiveControl((ContainerControl)control);
            return control;
        }

    }


Sa ovom pomocnom metodom mozes selektovati sledecu kontrolu pomocu entera, kada se selektuje textbox selektuje se ceo text u kontroli, ako je textbox multiline nece se obradjivati enter, mozes da stavis da se forma zatvori na escape.
 
Odgovor na temu

[es] :: .NET :: Enter umjesto Tab

[ Pregleda: 2377 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.