Entwickler-Ecke

Datenbanken (inkl. ADO.NET) - Autovervollständigung leicht gemacht....


tomycat - Do 30.03.17 20:31
Titel: Autovervollständigung leicht gemacht....
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 - 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.


Delete - Fr 31.03.17 15:53

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - 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 - 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.


Delete - So 02.04.17 23:09

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Mo 03.04.17 16:14

thx,
sorry aber ich kann dir nicht ganz folgen. wie soll ich das "ist" umgehen in der Textbox?


Delete - Mo 03.04.17 16:30

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Di 04.04.17 12:55

ok, thx,
ich habe eine bischen gegoogelt, da hatte jemand das gleiche Problem.
http://stackoverflow.com/questions/1437002/winforms-c-sharp-autocomplete-in-the-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.

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 - 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).


Ralf Jansen - 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.


tomycat - 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 - 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 - Di 04.04.17 15:24

Details sind überbewertet. ;-)
Wie sagte meine Mathematikprofessorin: "Der Rest (des Beweises) ist trivial. qed."


tomycat - 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?

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 - 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.


Delete - Di 04.04.17 15:51

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Di 04.04.17 16:26


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 - 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.


tomycat - 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?


Delete - Mi 05.04.17 07:47

- Nachträglich durch die Entwickler-Ecke gelöscht -


Th69 - Mi 05.04.17 09:25

Hallo tomycat,

selbstverständlich muß die Datei, in der der Code für die AutoCompleteTextBox drin ist, Teil des Projektes sein (eben über "Datei->Neue Datei"), also im Solution-Explorer auftauchen (alternativ natürlich in einer eigenen Assembly und per Referenz ins Projekt eingebunden).

Solange derselbe Namensbereich (namespace) wie die Applikation benutzt wird, ist auch kein using nötig - und wenn man den Designer (ToolBox) benutzt, dann kümmert sich dieser auch um den richtigen Namensbereich beim Codeerzeugen.

PS: Hier noch ein Tutorial: Creating Custom Controls In C# [http://www.c-sharpcorner.com/UploadFile/f5a10c/creating-custom-controls-in-C-Sharp/]


Delete - Mi 05.04.17 12:24

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Mi 05.04.17 16:40

hmmmmmm

ich habe die class1.cs hinzugefügt:

Projektmappe "AutoCompleteSample" (Projekt1)
Solution Items
Class1.cs
AutoCompleteSample
Properties
Verweise
Form1.cs
Program.cs


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
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;


Irgendwo mache ich einen Denkfehler?!


Ralf Jansen - Mi 05.04.17 16:47

Class1.cs muß zu einem Projekt gehören nicht zur Projektmappe. Hinter einer Projektmappe steht nichtst was kompiliert wird. Die Projekte in der Projektmappe werden kompiliert die Mappe ist nur eine Verwaltungsklammer. Deine class1.cs muß aber kompiliert werden sonst ist da nix was ausgeführt werden kann. Genauso wie Programm.cs und Form1.cs gehört die also in ein Projekt. Solutions Items kannst du für dokumentarische Zwecke nutzen für dich als Entwickler. Das Zeug landet aber nicht in der kompilierten Anwendung.


tomycat - Do 06.04.17 10:45

@Ralf Jansen
vielen Dank, deine Worte sind Goldwert.
in der Class1.cs habe ich die using von Hauptprojekt eingefügt.

Ich habe noch ein kleines weiteres Problem

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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AutoCompleteSample
{
    public partial class Form1 : Form
    {
        private readonly String[] _values = { "one""two""three""tree""four""fivee" }; //neu

        public Form1()
        {
            InitializeComponent();
            AutoComplete.Values = _values; //neu....AutoComplete <--- ist Rot.
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            data.Add("Mahesh Chand");
            comboBox1.AutoCompleteCustomSource = data;           
        }
    }
}



Der Name AutoComplete ist im Kontext nicht vorhanden.


Delete - Do 06.04.17 11:46

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Fr 07.04.17 13:22

thx, aber ich habe irgendwo den Faden verloren.


user profile iconFrühlingsrolle hat folgendes geschrieben Zum zitierten Posting springen:
Ist AutoComplete ein Objekt von der Klasse AutoCompleteTextBox? Wenn ja, wo wird es erzeugt?


oohh, eigentlich nicht-

user profile iconFrühlingsrolle hat folgendes geschrieben Zum zitierten Posting springen:
Ist im Code nicht ersichtlich.


Quelle:
http://stackoverflow.com/questions/1437002/winforms-c-sharp-autocomplete-in-the-middle-of-a-textbox

Aber in dem Beispiel geht vor...

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
using System;
using System.Windows.Forms;

namespace AutoComplete
{
    public partial class TestForm : Form
    {
        private readonly String[] _values = { "one""two""three""tree""four""fivee" };

        public TestForm()
        {
            InitializeComponent();
            // AutoComplete is our special textbox control on the form
            AutoComplete.Values = _values;
        }

    }
}

Was will der Programmierer damit ausdrücken, wenn AutoComplete in der Luft hängt?
Wo soll ich das Stück Code einbauen?


Ralf Jansen - Fr 07.04.17 13:33

Wie stehts um dein english?

In dem Kommentar steht
Zitat:
// AutoComplete is our special textbox control on the form

Du hast diese Combox scheinbar comboBox1 genannt. Also ersetze den Namen von comboBox1 durch AutoComplete oder den Bezeichner AutoComplete an der Stelle durch comboBox1.


tomycat - Fr 07.04.17 13:53

user profile iconRalf Jansen hat folgendes geschrieben Zum zitierten Posting springen:
Wie stehts um dein english?

so lala :-)

user profile iconRalf Jansen hat folgendes geschrieben Zum zitierten Posting springen:

In dem Kommentar steht
Zitat:
// AutoComplete is our special textbox control on the form

Du hast diese Combox scheinbar comboBox1 genannt. Also ersetze den Namen von comboBox1 durch AutoComplete oder den Bezeichner AutoComplete an der Stelle durch comboBox1.


Nein, Combobox1 ist etwas zum testen es hat nichts mit diese speziellen Textbox zutun :-)


tomycat - Fr 07.04.17 14:01

ok, habs umbenannt.

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
 comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            data.Add("Mahesh Chand");
            comboBox1.AutoCompleteCustomSource = data;

           // autoCompleteTextBox1
            autoCompleteTextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            autoCompleteTextBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
     //     AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            data.Add("Mahesh Chand");
            autoCompleteTextBox1.AutoCompleteCustomSource = data;


Aber er macht NUR am Zeilenanfang die Autovervollständigung. Nicht mitten im Satz.


tomycat - Fr 14.04.17 20:31

Planänderung, bevor ich weiter an dem Code herummache, der vielleicht sowieso nicht geht :-)
Also baue ich dem Kram selber. Wenn jemand einen Code sucht, der Mittendrin nach 1,2,3,4 oder 5 Zeichen sucht und die Wörter vervollständigt, bitte sehr:

Die Löschtaste musste abgefangen werden, weil man sich im Kreis bewegt. Einfach mit der Maus makieren und löschen.

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:
196:
197:
198:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        string[] lex = new string[] { "one""two""three" };
        string ergebnis;
        public Form1()
        {
            InitializeComponent();
           
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            int begrifflaenge = textBox1.SelectionStart;
            string schluesselwort = "";
            string tmp = textBox1.Text;
            string mein_ergebnis;
            int courserpos_tmp = textBox1.SelectionStart;
            ///////////////////////////////////////////////////////////////////
            // Nach einem Zeichen Suchen:
            if (courserpos_tmp == 1)
                if (tmp.Substring(01) != " ")
                {
                   
                    schluesselwort = tmp.Substring(01);
                    begrifflaenge = 1;
                    Console.WriteLine("1 zeichen, Anfang" + " -->>" + schluesselwort);
                }
            // Nach einem Zeichen Suchen:
            if (tmp.Length > 1 && tmp.Substring(tmp.Length - 21) == " " && tmp.Substring(tmp.Length - 11) != " ")
            {
      
                schluesselwort = tmp.Substring(tmp.Length - 11);
                begrifflaenge = 1;
                Console.WriteLine("1 zeichen, Mittendrin" + " -->>" + schluesselwort);
            }
            ////////////////////////////////////////////////////////////////////

            // Nach 2 Zeichen Suchen:
            if (courserpos_tmp == 2)
                if (tmp.Substring(01) != " " && tmp.Substring(11) != " ")
                {
                  
                    schluesselwort = tmp.Substring(01) + tmp.Substring(11);
                    begrifflaenge = 2;
                  //  Console.WriteLine(schluesselwort);
                    Console.WriteLine("2 zeichen, Anfang" + " -->>"+ schluesselwort);
                   
                }
            // Nach 2 Zeichen Suchen:
            if (tmp.Length > 2 && tmp.Substring(tmp.Length - 31) == " " && tmp.Substring(tmp.Length - 21) != " " && tmp.Substring(tmp.Length - 11) != " ")
            {
   
                schluesselwort = tmp.Substring(tmp.Length - 21) + tmp.Substring(tmp.Length - 11);
                begrifflaenge = 2;
                Console.WriteLine("2 zeichen, Mittendrin" + " -->>" + schluesselwort);
            }
            /////////////////////////////////////////////////////////////////////
            // Nach 3 Zeichen Suchen:
            if (courserpos_tmp == 3)
                if (tmp.Substring(01) != " " && tmp.Substring(11) != " " && tmp.Substring(21) != " ")
                {
         
                    schluesselwort =  tmp.Substring(01) +tmp.Substring(11) + tmp.Substring(21);
                        begrifflaenge = 3;
                    Console.WriteLine("3 zeichen, Anfang" + " -->>" + schluesselwort);
                }
            // Nach 2 Zeichen Suchen:
            if (courserpos_tmp > 3 && tmp.Substring(tmp.Length - 41) == " " && tmp.Substring(tmp.Length - 31) != " " && tmp.Substring(tmp.Length - 21) != " " && tmp.Substring(tmp.Length - 11) != " ")
            {
               
                //   Console.WriteLine("-" + tmp.Substring(tmp.Length - 3, 2) + "+");
                schluesselwort = tmp.Substring(tmp.Length - 31) + tmp.Substring(tmp.Length - 21) + tmp.Substring(tmp.Length - 11);
                  begrifflaenge = 3;
                Console.WriteLine("3 zeichen, Mittendrin" + " -->>" + schluesselwort);
            }
            /////////////////////////////////////////////////////////////////////
            // Nach 4 Zeichen Suchen:
            if (courserpos_tmp == 4)
                if (tmp.Substring(01) != " " && tmp.Substring(11) != " " && tmp.Substring(21) != " " && tmp.Substring(31) != " ")
                {
                 
                    schluesselwort = tmp.Substring(01) + tmp.Substring(11) + tmp.Substring(21) + tmp.Substring(31);
                    begrifflaenge = 4;
                    Console.WriteLine("4 zeichen, Anfang" + " -->>" + schluesselwort);
                }
            // Nach 4 Zeichen Suchen:
            if (courserpos_tmp > 4 && tmp.Substring(tmp.Length - 51) == " " && tmp.Substring(tmp.Length - 41) != " " && tmp.Substring(tmp.Length - 31) != " " && tmp.Substring(tmp.Length - 21) != " " && tmp.Substring(tmp.Length - 11) != " ")
            {

                //   Console.WriteLine("-" + tmp.Substring(tmp.Length - 3, 2) + "+");
                schluesselwort = tmp.Substring(tmp.Length - 41) + tmp.Substring(tmp.Length - 31) + tmp.Substring(tmp.Length - 21) + tmp.Substring(tmp.Length - 11);
                begrifflaenge = 4;
                Console.WriteLine("4 zeichen, Mittendrin" + " -->>" + schluesselwort);
            }
            /////////////////////////////////////////////////////////////////////
            // Nach 5 Zeichen Suchen:
            if (courserpos_tmp == 5)
                if (tmp.Substring(01) != " " && tmp.Substring(11) != " " && tmp.Substring(21) != " " && tmp.Substring(31) != " " && tmp.Substring(41) != " ")
                {
                   
                    schluesselwort = tmp.Substring(01) + tmp.Substring(11) + tmp.Substring(21) + tmp.Substring(31) + tmp.Substring(41);
                    begrifflaenge = 5;
                    Console.WriteLine("5 zeichen, Anfang" + " -->>" + schluesselwort);
                }
            // Nach 5 Zeichen Suchen:
            if (courserpos_tmp > 5 && tmp.Substring(tmp.Length - 61) == " " && tmp.Substring(tmp.Length - 51) != " " && tmp.Substring(tmp.Length - 41) != " " && tmp.Substring(tmp.Length - 31) != " " && tmp.Substring(tmp.Length - 21) != " " && tmp.Substring(tmp.Length - 11) != " ")
            {

                //   Console.WriteLine("-" + tmp.Substring(tmp.Length - 3, 2) + "+");
                schluesselwort = tmp.Substring(tmp.Length - 51) + tmp.Substring(tmp.Length - 41) + tmp.Substring(tmp.Length - 31) + tmp.Substring(tmp.Length - 21) + tmp.Substring(tmp.Length - 11);
                begrifflaenge = 4;
                Console.WriteLine("5 zeichen, Mittendrin" + " -->>" + schluesselwort);
            }
            /////////////////////////////////////////////////////////////////////


            switch (begrifflaenge)           
            {
                case 1:            
                    mein_ergebnis = seach_the_word(schluesselwort, 1);
                    if (ergebnis.Length != 0)
                    {
                        textBox1.Text += mein_ergebnis.Substring(1, ergebnis.Length - 1);
                        textBox1.Select(courserpos_tmp, textBox1.TextLength);
                    }
                    textBox1.SelectionStart = courserpos_tmp;
                    break;
                case 2:             
                    mein_ergebnis = seach_the_word(schluesselwort,2);
                    if (ergebnis.Length != 0)
                    {
                        textBox1.Text += mein_ergebnis.Substring(2, ergebnis.Length - 2);
                        textBox1.Select(courserpos_tmp, textBox1.TextLength);
                    }
                    textBox1.SelectionStart = courserpos_tmp;
                    break;
                case 3:              
                    mein_ergebnis = seach_the_word(schluesselwort, 3);
                    if (ergebnis.Length != 0)
                    {
                        textBox1.Text += mein_ergebnis.Substring(3, ergebnis.Length - 3);
                        textBox1.Select(courserpos_tmp, textBox1.TextLength);
                    }
                    textBox1.SelectionStart = courserpos_tmp;
                    break;
                case 4:     
                    mein_ergebnis = seach_the_word(schluesselwort, 4);
                    if (ergebnis.Length != 0)
                    {
                        textBox1.Text += mein_ergebnis.Substring(4, ergebnis.Length - 4);
                        textBox1.Select(courserpos_tmp, textBox1.TextLength);
                    }
                    textBox1.SelectionStart = courserpos_tmp;
                    break;
                case 5:
                    mein_ergebnis = seach_the_word(schluesselwort, 5);
                    if (ergebnis.Length != 0)
                    {
                        textBox1.Text += mein_ergebnis.Substring(5, ergebnis.Length - 5);
                        textBox1.Select(courserpos_tmp, textBox1.TextLength);
                    }
                    textBox1.SelectionStart = courserpos_tmp;
                    break;
            }


        }
        string seach_the_word(string a, int laenge)
        {
            ergebnis = "";
            for (int xy =0 ;xy < 3;xy++)
            {
                if (lex[xy].Length >= laenge)
                {
                    if (a == lex[xy].Substring(0, laenge))
                    {
                        ergebnis = (lex[xy]);
                    }
                }

            }
            Console.WriteLine(ergebnis);
            return (ergebnis);
        }
    }
}


Delete - Fr 14.04.17 21:04

- Nachträglich durch die Entwickler-Ecke gelöscht -


tomycat - Sa 15.04.17 19:51

Aufgeben = verlieren
nicht aufgeben führt oft zu freude und stolz. :-)

Die Backspacetaste macht mir noch Kummer, sie geht nicht. Bei jeder Veränderung wird automatisiert. Hätte jemand eine Idee?


Delete - Sa 15.04.17 21:08

- Nachträglich durch die Entwickler-Ecke gelöscht -