Autor Beitrag
tomycat
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Do 30.03.17 20:31 
hallo,
ich habe ein paar Textboxen, da möchte ich z.B. Tho eintippen und der pc erschlägt mir Thomas vor.
Ich überlege mir, ob eine fix und fertigen quellcode mit Datenbank nehmen soll.
O D E R
Einfach nur eine Textdatei mit 100.000 Wörter in Arry laden und jeden Tastenanschlag mit der arry vergleichen?

Was würdet ihr mir empfehlen?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Fr 31.03.17 13:08 
Ohne konkrete Anforderung sind Empfehlung schwer und das was du beschreibst ist keine.

Wieso sollte bei der Eingabe von Tho gerade Thomas vorgeschlagen werden? Allein in Deutsch gibt es vermutlich eine fast 3-stellige Zahl von Wörtern die mit Tho anfangen wenn wir Wörter die Tho enthalten mitnehmen ist es sicher eine 4-stellige Zahl. Dann noch andere Sprachen mitnehmen? oder ähnliche Wörter? Oder Wörter mit gleicher Bedeutung?

Ohne Context kann man da irgendwie nix vorschlagen.

Für diesen Beitrag haben gedankt: tomycat
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 31.03.17 15:53 
- Nachträglich durch die Entwickler-Ecke gelöscht -

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Sa 01.04.17 20:24 
thx all,
sorry, aber ich weis selber nicht genauwie die autovervollständigung, sein soll. Ich will ein Auto kaufen, aber ich muss erst mal schauen was es gibt ?!
Dieswegen fragte ich ins Blaue.

@Frühlingsrolle
Das was du gepostet hast schmeckt mir gut, sieht einfach und zuverlässig aus. Danke.
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: So 02.04.17 20:47 
@Frühlingsrolle
fast die perfekte Lösung, eine Frage noch dazu,
CSharp macht auf M einen Vorschloag wie Millan. Was muss ich machen, wenn ich einen Text schreibe wie ... Mein bester Freund ist M... UND dann soll der Vorschlag kommen.
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 02.04.17 23:09 
- Nachträglich durch die Entwickler-Ecke gelöscht -

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mo 03.04.17 16:14 
thx,
sorry aber ich kann dir nicht ganz folgen. wie soll ich das "ist" umgehen in der Textbox?
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 03.04.17 16:30 
- Nachträglich durch die Entwickler-Ecke gelöscht -

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Di 04.04.17 12:55 
ok, thx,
ich habe eine bischen gegoogelt, da hatte jemand das gleiche Problem.
stackoverflow.com/qu...-middle-of-a-textbox
Ich verstehe auch den Quellcode vom Forum so lala. Leider bekomme ich die Combobox1 nicht mit der neuen Klasse vom Forum verbunden.
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            // AutoCompleteStringCollection 
            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            data.Add("Mahesh Chand");
            data.Add("Mac Jocky");
            data.Add("Millan Peter");

            comboBox1.AutoCompleteCustomSource = data;           
        }

        /////////////////////// NEU ////// vom Forum....

        public class AutoCompleteTextBox : TextBox
        {
            private ListBox _listBox;
            private bool _isAdded;
            private String[] _values;
            private String _formerValue = String.Empty;

            public AutoCompleteTextBox()
            {
                InitializeComponent();
                ResetListBox();
            }

            private void InitializeComponent()
            {
                _listBox = new ListBox();
                this.KeyDown += this_KeyDown;
                this.KeyUp += this_KeyUp;
            }

            private void ShowListBox()
            {
                if (!_isAdded)
                {
                    Form parentForm = this.FindForm(); // new line added
                    parentForm.Controls.Add(_listBox); // adds it to the form
                    Point positionOnForm = parentForm.PointToClient(this.Parent.PointToScreen(this.Location)); // absolute position in the form
                    _listBox.Left = positionOnForm.X;
                    _listBox.Top = positionOnForm.Y + Height;
                    _isAdded = true;
                }
                _listBox.Visible = true;
                _listBox.BringToFront();
            }



            private void ResetListBox()
            {
                _listBox.Visible = false;
            }

            private void this_KeyUp(object sender, KeyEventArgs e)
            {
                UpdateListBox();
            }

            private void this_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                    case Keys.Tab:
                        {
                            if (_listBox.Visible)
                            {
                                Text = _listBox.SelectedItem.ToString();
                                ResetListBox();
                                _formerValue = Text;
                                this.Select(this.Text.Length, 0);
                                e.Handled = true;
                            }
                            break;
                        }
                    case Keys.Down:
                        {
                            if ((_listBox.Visible) && (_listBox.SelectedIndex < _listBox.Items.Count - 1))
                                _listBox.SelectedIndex++;
                            e.Handled = true;
                            break;
                        }
                    case Keys.Up:
                        {
                            if ((_listBox.Visible) && (_listBox.SelectedIndex > 0))
                                _listBox.SelectedIndex--;
                            e.Handled = true;
                            break;
                        }


                }
            }

            protected override bool IsInputKey(Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Tab:
                        if (_listBox.Visible)
                            return true;
                        else
                            return false;
                    default:
                        return base.IsInputKey(keyData);
                }
            }

            private void UpdateListBox()
            {
                if (Text == _formerValue)
                    return;

                _formerValue = this.Text;
                string word = this.Text;

                if (_values != null && word.Length > 0)
                {
                    string[] matches = Array.FindAll(_values,
                                                     x => (x.ToLower().Contains(word.ToLower())));
                    if (matches.Length > 0)
                    {
                        ShowListBox();
                        _listBox.BeginUpdate();
                        _listBox.Items.Clear();
                        Array.ForEach(matches, x => _listBox.Items.Add(x));
                        _listBox.SelectedIndex = 0;
                        _listBox.Height = 0;
                        _listBox.Width = 0;
                        Focus();
                        using (Graphics graphics = _listBox.CreateGraphics())
                        {
                            for (int i = 0; i < _listBox.Items.Count; i++)
                            {
                                if (i < 20)
                                    _listBox.Height += _listBox.GetItemHeight(i);
                                // it item width is larger than the current one
                                // set it to the new max item width
                                // GetItemRectangle does not work for me
                                // we add a little extra space by using '_'
                                int itemWidth = (int)graphics.MeasureString(((string)_listBox.Items[i]) + "_", _listBox.Font).Width;
                                _listBox.Width = (_listBox.Width < itemWidth) ? itemWidth : this.Width; ;
                            }
                        }
                        _listBox.EndUpdate();
                    }
                    else
                    {
                        ResetListBox();
                    }
                }
                else
                {
                    ResetListBox();
                }
            }

            public String[] Values
            {
                get
                {
                    return _values;
                }
                set
                {
                    _values = value;
                }
            }

            public List<String> SelectedValues
            {
                get
                {
                    String[] result = Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    return new List<String>(result);
                }
            }

        }



    }
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 04.04.17 13:00 
Setze den Code der neuen Klasse in eine eigene Datei und kompiliere es. Dann benutze in deiner Form (z.B. über den Designer) diese AutoCompleteTextBox-Komponente (anstatt der bisherigen ComboBox).

Für diesen Beitrag haben gedankt: tomycat
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 04.04.17 13:01 
Es ist keine ComboBox sondern eine neue TextBox. Steck den Code dafür besser in eine eigene Datei und schon gar nicht als Inner class einer anderen class so wie es jetzt aussieht.
Wenn dein Projekt kompiliert solltest du die neue TextBox dann wie jedes andere Control in der ToolBox finden und dann einfach auf deine Form ziehen.

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Di 04.04.17 13:54 
THX ALL,
ich habe echt Respekt vor euch. Mein Projekt hat über 13000 Zeilen c#, aber ich denke, ich kann noch viel von euch lernen :-)

@Ralf Jansen
bevor ich in die falsche Richtung fahre, frage ich lieber nach was ich googlen soll um deinen Plan zu vervollständigen.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 04.04.17 14:10 
Zitat:
@Ralf Jansen
bevor ich in die falsche Richtung fahre, frage ich lieber nach was ich googlen soll um deinen Plan zu vervollständigen.


Ich habe einen Plan :gruebel: Ich hoffe du hast einen :wink:
Wenn dir irgendwas unklar ist dann frag nach. Ich/wir vergessen schonmal Details zu erwähnen weil wir die für selbstverständlich halten auch wenn das vielleicht nicht immer ganz der Fall ist.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 04.04.17 15:24 
Details sind überbewertet. ;-)
Wie sagte meine Mathematikprofessorin: "Der Rest (des Beweises) ist trivial. qed."

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Di 04.04.17 15:30 
ok, ich folge mal euch...
Datei Neu -> Datei -> Visual c# Klasse
code eingebau und compiliert.
In der Toolbox hat nicht nichts getan. Was mache ich falsch?
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
using System;

public class Class1
{
    public Class1()
    {
    }
            public class AutoCompleteTextBox : TextBox
    {
        private ListBox _listBox;
        private bool _isAdded;
        private String[] _values;
        private String _formerValue = String.Empty;

        public AutoCompleteTextBox()
        {
            InitializeComponent();
            ResetListBox();
        }

        private void InitializeComponent()
        {
            _listBox = new ListBox();
            this.KeyDown += this_KeyDown;
            this.KeyUp += this_KeyUp;
        }

        private void ShowListBox()
        {
            if (!_isAdded)
            {
                Form parentForm = this.FindForm(); // new line added
                parentForm.Controls.Add(_listBox); // adds it to the form
                Point positionOnForm = parentForm.PointToClient(this.Parent.PointToScreen(this.Location)); // absolute position in the form
                _listBox.Left = positionOnForm.X;
                _listBox.Top = positionOnForm.Y + Height;
                _isAdded = true;
            }
            _listBox.Visible = true;
            _listBox.BringToFront();
        }



        private void ResetListBox()
        {
            _listBox.Visible = false;
        }

        private void this_KeyUp(object sender, KeyEventArgs e)
        {
            UpdateListBox();
        }

        private void this_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                case Keys.Tab:
                    {
                        if (_listBox.Visible)
                        {
                            Text = _listBox.SelectedItem.ToString();
                            ResetListBox();
                            _formerValue = Text;
                            this.Select(this.Text.Length, 0);
                            e.Handled = true;
                        }
                        break;
                    }
                case Keys.Down:
                    {
                        if ((_listBox.Visible) && (_listBox.SelectedIndex < _listBox.Items.Count - 1))
                            _listBox.SelectedIndex++;
                        e.Handled = true;
                        break;
                    }
                case Keys.Up:
                    {
                        if ((_listBox.Visible) && (_listBox.SelectedIndex > 0))
                            _listBox.SelectedIndex--;
                        e.Handled = true;
                        break;
                    }


            }
        }

        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Tab:
                    if (_listBox.Visible)
                        return true;
                    else
                        return false;
                default:
                    return base.IsInputKey(keyData);
            }
        }

        private void UpdateListBox()
        {
            if (Text == _formerValue)
                return;

            _formerValue = this.Text;
            string word = this.Text;

            if (_values != null && word.Length > 0)
            {
                string[] matches = Array.FindAll(_values,
                                                 x => (x.ToLower().Contains(word.ToLower())));
                if (matches.Length > 0)
                {
                    ShowListBox();
                    _listBox.BeginUpdate();
                    _listBox.Items.Clear();
                    Array.ForEach(matches, x => _listBox.Items.Add(x));
                    _listBox.SelectedIndex = 0;
                    _listBox.Height = 0;
                    _listBox.Width = 0;
                    Focus();
                    using (Graphics graphics = _listBox.CreateGraphics())
                    {
                        for (int i = 0; i < _listBox.Items.Count; i++)
                        {
                            if (i < 20)
                                _listBox.Height += _listBox.GetItemHeight(i);
                            // it item width is larger than the current one
                            // set it to the new max item width
                            // GetItemRectangle does not work for me
                            // we add a little extra space by using '_'
                            int itemWidth = (int)graphics.MeasureString(((string)_listBox.Items[i]) + "_", _listBox.Font).Width;
                            _listBox.Width = (_listBox.Width < itemWidth) ? itemWidth : this.Width; ;
                        }
                    }
                    _listBox.EndUpdate();
                }
                else
                {
                    ResetListBox();
                }
            }
            else
            {
                ResetListBox();
            }
        }

        public String[] Values
        {
            get
            {
                return _values;
            }
            set
            {
                _values = value;
            }
        }

        public List<String> SelectedValues
        {
            get
            {
                String[] result = Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                return new List<String>(result);
            }
        }

    }



}
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 04.04.17 15:50 
Warum steckt AutoCompleteTextBox denn in der Class1 Klasse? Wie gesagt du solltest Klassen wenn sie Controls sind nicht als innerere Klassen anderer Klassen definieren.
Schmeiß die umgebende class1 weg.

Für diesen Beitrag haben gedankt: tomycat
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 04.04.17 15:51 
- Nachträglich durch die Entwickler-Ecke gelöscht -

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Di 04.04.17 16:26 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
using System;

//public class Class1
//{
   // public Class1()
   // {
  //  }
            public class AutoCompleteTextBox : TextBox
    {
        private ListBox _listBox;
        private bool _isAdded;
        private String[] _values;
        private String _formerValue = String.Empty;
...................
// letzte Zeile ->>> //}

kein Unterschied.
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 04.04.17 17:18 
toolbox

Habe ich auch gemacht. Geht einfach. Kein Ahnung was du anders machst oder was man anders machen könnte damit es nicht geht.
Einloggen, um Attachments anzusehen!

Für diesen Beitrag haben gedankt: tomycat
tomycat Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 265
Erhaltene Danke: 1



BeitragVerfasst: Mi 05.04.17 07:38 
thx,
aber in der Toolbox hat sich nicht getan, bin alles nochmal durchgegagen.
muss ich vielleicht die class1.cs in mein Projekt einbinden?