Multiline Textbox Suggestion/Append property - c#

I am currently in need of a multi-line textbox that can do suggest/append of the desired list items so far I am able to get the suggestion but the only problem is now is that it is working abnormally, i.e. showing suggestion for white spaces, when I press enter in order to autocomplete it gives me multiple strings of that characters, also there is a problem that due to my logic it only show suggestion to the strings that are at the end of the textbox, not the one that is inserted in between.
So far my code is following
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace multiline_text_suggest
{
public partial class Form1 : Form
{
ListBox listBox = new ListBox();
public Form1()
{
InitializeComponent();
Controls.Add(listBox);
listBox.Hide();
textsplit();
}
string intellisenes;
private void textsplit() {
listBox1.Items.Clear();
foreach (string c in textBox.Text.Split()) {
listBox1.Items.Add(c);
}
intellisenes = listBox1.Items[listBox1.Items.Count-1].ToString().Trim();
}
void listBox_SelectedIndexChanged(object sender, KeyEventArgs e)
{
if (e.KeyValue == (decimal)Keys.Enter)
{
textBox.Text += ((ListBox)sender).SelectedItem.ToString();
textBox.Focus();
listBox.Hide();
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
textsplit();
listBox.SetBounds(textBox.Left, textBox.Top + textBox.Height, textBox.Width + 20, 40);
listBox.KeyDown += listBox_SelectedIndexChanged;
List<string> list = new List<string>(){"king","kong"};
var localList = list.Where(z => z.StartsWith(intellisenes.Trim())).ToList();
if (localList.Any() && !string.IsNullOrEmpty(textBox.Text) && !string.IsNullOrWhiteSpace(textBox.Text))
{
listBox.DataSource = localList;
listBox.Show();
listBox.Focus();
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
textsplit();
}
}
}
Forgive me for any mistakes since I am just a noob in coding.

Related

ComboBox menu repeat bug after typing in textbox in Winform app

I have a primitive app with 2 comboBoxes. They work fine after first starting the app.
However after typing in text in the search bar and pressing enter, the comboboxes loop their contents.
It happens after I type in the textbox, even if I do not press enter. Every time I press a key another repeat list of options appends to the comboBox.
How do I prevent this comboBox malfunction? Here is my code.
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;
//Nick Knapp
//CSCI 363 Fall 2019
namespace c363_hw3_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Library Literature Search";
this.BackColor = Color.White;
comboBox2.Items.Add("All");
comboBox2.Items.Add("Books");
comboBox2.Items.Add("Papers");
comboBox2.Items.Add("Films");
comboBox2.Items.Add("CDs");
comboBox2.Items.Add("Other");
comboBox2.SelectedIndex = 0;
comboBox1.Items.Add("Title");
comboBox1.Items.Add("Author");
comboBox1.Items.Add("Publisher");
comboBox1.Items.Add("ISBN");
comboBox1.SelectedIndex = 0;
// this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
//this.Controls.Add(textBox1);
this.ActiveControl = textBox1;
textBox1.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
e.Handled = true;
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
I think your code is correct but have you ever tried these 2 method after calling combo boxes?
comboBox1.ResetText();
comboBox1.Items.Clear();```
i think it works
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
comboBox1.Items.Clear();
e.Handled = true;
}
}

What C# Datagridview event will update column immediately after an operation

I am multiplying 2 cells in my datagridview, to display it on a third column, I want the results on the third cell to be done as am editing the fields on either the first or second cell.
I am using - private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
but this will only update, when i click on another cell in the datagrid.
I need an event suggestion that will deliver it as it's done on MSExcel
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DgvMultiplyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CalculateAnswer()
{
int dgvRow = dataGridView1.CurrentCell.RowIndex;
int dgvColumn = dataGridView1.CurrentCell.ColumnIndex;
int firstNumber = 0;
int secondNumber = 0;
int answer = 0;
if (dataGridView1.Rows[dgvRow].Cells[0].EditedFormattedValue != null &&
dataGridView1.Rows[dgvRow].Cells[1].EditedFormattedValue != null)
{
if ((int.TryParse(dataGridView1.Rows[dgvRow].Cells[0].EditedFormattedValue.ToString(), out firstNumber) == true) &&
(int.TryParse(dataGridView1.Rows[dgvRow].Cells[1].EditedFormattedValue.ToString(), out secondNumber) == true))
{
answer = firstNumber * secondNumber;
dataGridView1.Rows[dgvRow].Cells[2].Value = answer;
}
}
}
private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyUp -= new KeyEventHandler(Control_KeyUp);
e.Control.KeyUp += new KeyEventHandler(Control_KeyUp);
}
private void Control_KeyUp(object sender, KeyEventArgs e)
{
CalculateAnswer();
dataGridView1.Invalidate();
}
}
}
I used private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)and it worked fine. Thank You.

Zedgraph Control accessible from any function

I have this function:
public void MainFormLoad(object sender, EventArgs e)
{
GraphPane myPane = GRAPH.GraphPane;
}
Where myPane is a reference to GraphPane (GRAPH is name of ZedGraphControl which is displayed in GUI)
And now I want to change things like name of "x" or "y" axis, title, colors etd. or whatever you can change, but based on events. For example: I have textbox where I can write text and this text will be displayed in graph as title after textbox_textchanged_event trigger like this:
void TitleTextChanged(object sender, EventArgs e)
{
myPane.Title.Text = textbox1.Text;
}
There will be more functions like this to change properties of the graph. But this is not working.
Is there a way to come around this?
I have also tried this:
void TitleTextChanged(object sender, EventArgs e)
{
GRAPH.GraphPane.Title.Text = textbox1.text.Text;
}
but no help at all.
Please help, any advices are welcome.
**ANSWER:
So far, i have found this solution:
public void MainFormLoad(object sender, EventArgs e)
{
EditGraph(GRAPH);
}
This is the event that handles text change in text box:
public void TB_GRAPH_TITLE_VALUETextChanged(object sender, EventArgs e)
{
//GraphPane myPane2 = GRAPH.GraphPane;
changedGraphTitle = true;
EditGraph(GRAPH);
}
This is the function that find what is changed and update it:
public void EditGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
if(changedGraphTitle)
{
myPane.Title.Text = TB_GRAPH_TITLE_VALUE.Text;
changedGraphTitle = false;
zgc.Refresh();
}
}
"bool changedGraphTitle = false" must be declared also.**
If I've understood your question correctly, here's my simple code to update Zedgraph Axis Titles by a single ButtonClick event.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace updateZedGraph
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myPane = zedGraphControl1.GraphPane;
}
GraphPane myPane;
private void btn_UpdateChart_Click(object sender, EventArgs e)
{
// Update x Axis Text
myPane.XAxis.Title.Text = textBox1.Text;
// Update x Axis Text
myPane.YAxis.Title.Text = textBox2.Text;
// Refresh Chart
zedGraphControl1.Invalidate();
}
}
}
hope that helps..

Cannot Add Dynamically Generated Textbox's Text To A List (C#)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Mod
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int c = 0;
private void button1_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
txtRun.Size = new System.Drawing.Size(200,15);
this.Controls.Add(txtRun);
}
private void button2_Click(object sender, EventArgs e)
{
List<string>tilelocation = List<string>();
tilelocation.Add(); //What goes in this method's arguments?
}
}
}
Here is my code. Button1 creates a theoretically infinite # of textboxes, but I wish to add the text in these dynamically generated textboxes to a list. How can this be done?
[EDIT]
And how can I display them all in a messagebox, each on separate lines?
You need to keep a reference to the control.
The other secret is that you have to keep it in the ViewState so it's available between post backs.
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
int c = 0;
private List<TextBox _lstTextBoxList;
public List<TextBox> lstTextBoxList {
get {
if(_lstTextBoxList == null) {
_lstTextBoxList = ViewState["lstTextBoxList"] as List<TextBox>;
}
return _lstTextBoxList;
}
set { ViewState["lstTextBoxList"] = _lstTextBoxList = value; }
}
private void button1_Click(object sender, EventArgs e) {
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * c));
txtRun.Size = new System.Drawing.Size(200,15);
this.Controls.Add(txtRun);
lstTextBoxList.Add(txtRun);
}
private void button2_Click(object sender, EventArgs e) {
// Not sure of your goal here:
List<string> tilelocation = List<string>();
tilelocation.Add(lstTextBoxList[lstTextBoxList.Count - 1]);
// I would assume you wanted this:
List<string> strValues = lstTextBoxList.Select<TextBox,string>(t => t.Text).ToList();
}
}
but I wish to add the text in these dynamically generated textboxes to
a list. How can this be done?
You should use new List<string> like:
private void button2_Click(object sender, EventArgs e)
{
List<string> tilelocation = new List<string>();
foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(r=> r.Name.StartsWith("txtDynamic"))
titlelocation.Add(tb.Text);
//if you want a string out of it.
string str = string.Join(",", titlelocation);
MessageBox.Show(str);
}
EDIT: For each text on new line use:
string str = string.Join(Environment.NewLine, titlelocation);
MessageBox.Show(str);
I don't know why you want to use a seperate button2 click event to add the text. Why don't you use a global variable tilelocation, and add the text to this list in the button1_click event? Something like:
txtRun.Text=Your Text;
tilelocation.add(Your Text);
If you want to display them in a message box, then add codes:
string str="";
foreach(text in tilelocation)
{
str+=text;
}
MessageBox.Show(str);

how to display only one output ("Recognized, OK") when I say a 2-syllable word or more

I just want my program to say "Recognized, OK!" if it hears words. Words that have either one or more syllable word.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
namespace SpeechRecognitionExample
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine recognitionEngine;
public Form1()
{
InitializeComponent();
recognitionEngine = new SpeechRecognitionEngine();
recognitionEngine.SetInputToDefaultAudioDevice();
recognitionEngine.SpeechRecognized += (s, args) =>
{
foreach (RecognizedWordUnit word in args.Result.Words)
{
if (word.Confidence > 0.0f)
txtOutput.Text += "Recognized, OK!";
}
txtOutput.Text += Environment.NewLine;
};
recognitionEngine.LoadGrammar(new DictationGrammar());
}
private void btnStart_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
}
private void btnStop_Click(object sender, EventArgs e)
{
recognitionEngine.RecognizeAsyncStop();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Please help me. Thank you. Because when I say a word that has two or three syllable it displays "Recognized, OK!" two or three times too. But if I say, example, "START" it just returns 1 output.
You mean, if there's any word. The program should say "Recognized, OK!"?
If that, use this:
recognitionEngine.SpeechRecognized += (s, args) =>
{
if (!args.Result.Words.Any() && word.Confidence <= 0.0f) return;
txtOutput.Text += "Recognized, OK!";
txtOutput.Text += Environment.NewLine;
};

Categories