Customize TextBox autocomplete - c#

How do I change the autocomplete on a TextBox? I want that when I type a string, the box suggest items containing that string instead of starting with.
My code is:
class MyClass
{
private AutoCompleteStringCollection autoCompleteList = new AutoCompleteStringCollection();
public MyClass()
{
InitializeComponent();
autoCompleteList.AddRange(ListNames.Select(x=>x.Name).ToArray());
textBoxName.AutoCompleteCustomSource = autoCompleteList;
textBoxName.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxName.AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxName.KeyDown += TextBoxtextName_KeyDown;
}
private void TextBoxClient_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
this.Name = (sender as TextBox).Text;
}
}
}
What I want:

Looking at your code you have everything you need but 1 line of code. That line is:
This will only work if the start of a string is entered
//Suggestion only
textBoxName.AutoCompleteMode = AutoCompleteMode.Suggest;
//Suggest and autocomplete
textBoxName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
This will work as a contains method but works with a custom control
You can also make a custom textbox control that fits your needs.
Custom textbox class:
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)
{
Parent.Controls.Add(_listBox);
_listBox.Left = Left;
_listBox.Top = Top + 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);
}
}
}
Usage:
string[] nameArray = { "name1", "name2", "name3", "bla name" };
AutoCompleteTextBox tb = new AutoCompleteTextBox();
tb.Values = nameArray;
tb.Location = new Point(10,10);
tb.Size = new Size(25,75);
this.Controls.Add( tb );
I got the code for the custom control from: SO Question - Autocomplete contains

Related

How to display value from not selected combobox c#

I need to display an unselected ComboBox value.
I have 10 ComboBoxes with tag values 1 through 10.
When I select a ComboBox with a tag value of 1, I have to check what is displayed from the ComboBox that has a tag value of 2.
Selecting a ComboBox with a tag value of 5 should give me the value of the ComboBox that has a tag value of 6.
public partial class workPlacePlan : Form
{
public workPlacePlan()
{
InitializeComponent();
}
private void workPlacePlan_Load(object sender, EventArgs e)
{
this.MaximumSize = this.Size;
this.MinimumSize = this.Size;
int cboId;
int i = 1;
string cboName; int c = 1;
var items = new Dictionary<int, string>();
ComboBox[] cbo = {
cbo_1, cbo_2
};
foreach(ComboBox cbos in cbo)
{
items.Add(0, "-------");
i = 1;
while (i <= 24)
{
cboId = i;
cboName = TimeSpan.FromHours(i).ToString("hh':'mm");
items.Add(cboId, cboName);
i++;
}
cbos.Tag = c;
cbos.DataSource = new BindingSource(items, null);
cbos.DisplayMember = "Value";
cbos.ValueMember = "Key";
items.Clear();
cbos.SelectedIndexChanged += new EventHandler(cboSelected);
c++;
}
}
public void cboSelected(object sender, EventArgs e)
{
ComboBox cb = ((ComboBox)sender);
int i_tmp;
int tg = Int32.Parse(cb.Tag.ToString());
if(tg % 2 == 0) //if is even
{
i_tmp= tg - 1;
}
else
{
i_tmp = tg + 1;
}
}
private void cmd_Save_Click(object sender, EventArgs e)
{
}
}
Solution:
public partial class workPlacePlan : Form
{
public workPlacePlan()
{
InitializeComponent();
}
private void workPlacePlan_Load(object sender, EventArgs e)
{
this.MaximumSize = this.Size;
this.MinimumSize = this.Size;
int cboId;
int i = 1;
string cboName; int c = 1;
var items = new Dictionary<int, string>();
ComboBox[] cbo = {
cbo_1_0, cbo_1_1, cbo_2_0, cbo_2_1, cbo_3_0, cbo_3_1, cbo_4_0, cbo_4_1, cbo_5_0, cbo_5_1, cbo_6_0, cbo_6_1,
cbo_13_0,cbo_13_1,cbo_14_0,cbo_14_1,cbo_15_0,cbo_15_1,cbo_16_0,cbo_16_1,cbo_17_0,cbo_17_1,cbo_18_0,cbo_18_1,
cbo_19_0,cbo_19_1
};
foreach(ComboBox cbos in cbo)
{
items.Add(0, "-------");
i = 1;
while (i <= 24)
{
cboId = i;
cboName = TimeSpan.FromHours(i).ToString("hh':'mm");
items.Add(cboId, cboName);
i++;
}
cbos.Tag = c;
cbos.DataSource = new BindingSource(items, null);
cbos.DisplayMember = "Value";
cbos.ValueMember = "Key";
cbos.SelectedIndex = 0;
items.Clear();
cbos.SelectedIndexChanged += new EventHandler(cboSelected);
c++;
}
}
public void cboSelected(object sender, EventArgs e)
{
ComboBox cb = ((ComboBox)sender);
int i_tmp;
int tg = Int32.Parse(cb.Tag.ToString());
int idx;
if(tg % 2 == 0) //if is even
{
i_tmp= tg - 1;
}
else
{
i_tmp = tg + 1;
}
//string y = cb.GetItemText(cb.SelectedItem);
//MessageBox.Show(y.ToString());
foreach (ComboBox cbt in panel1.Controls.OfType<ComboBox>()) {
if(Int32.Parse(cbt.Tag.ToString()) == Int32.Parse(i_tmp.ToString()))
{
idx = Int32.Parse(cbt.SelectedIndex.ToString());
cbt.SelectedIndex = idx;
MessageBox.Show(cbt.SelectedIndex.ToString());
}
}
}
private void cmd_Save_Click(object sender, EventArgs e)
{
}
}

Why are the values still adding up after i clear the output label?

I'm not sure what is going on. I thought I had it set up to clear the output label at the end. Everytime I clear it, the program still remembers the previous number and adds to it. I honestly have no idea what is going on.
Also on a side note, how do I put set up radiobuttons to be used in the this method?
First coding class so i'm still kind of a beginner.
private double oilandlube()
{
if (checkBox1.Checked == true)
{
Oil_change = 26;
}
if (checkBox2.Checked == true)
{
Lube_job = 18;
}
return Oil_change + Lube_job;
}
private void Oiltype ()
{
if (radioButton1.Checked)
{
Regular = 0;
}
if (radioButton2.Checked)
{
Mixed = 10;
}
else
{
Mixed = 0;
}
if (radioButton3.Checked)
{
Full_Synthetic = 18;
}
else
{
Full_Synthetic = 0;
}
}
private void carwash()
{
if (radioButton4.Checked)
{
none = 0;
}
if (radioButton5.Checked)
{
complimentary = 0;
}
if (radioButton6.Checked)
{
Full_service = 6;
}
else
{
Full_service = 0;
}
if (radioButton7.Checked)
{
Premium = 9;
}
else
{
Premium = 0;
}
}
private double flushes()
{
if (checkBox3.Checked == true)
{
Radiator_flush = 30;
}
if (checkBox4.Checked == true)
{
Transmission_flush = 80;
}
return Radiator_flush + Transmission_flush;
}
private double misc()
{
if (checkBox5.Checked == true)
{
inspection = 15;
}
if (checkBox6.Checked == true)
{
replace_muffler = 100;
}
if (checkBox7.Checked == true)
{
tire_rotation = 20;
}
return inspection + replace_muffler;
}
private double partsandlabor()
{
double.TryParse(textBox1.Text, out parts);
double.TryParse(textBox2.Text, out labor);
return parts + labor;
}
private double tax()
{
return partsandlabor() * taxes;
}
private void summary()
{
service = oilandlube() + flushes() + misc();
total_parts = partsandlabor();
double total_tax = tax();
double grand_total = service + total_parts + total_tax;
label7.Text = service.ToString("c");
label8.Text = total_parts.ToString("c");
label9.Text = total_tax.ToString("c");
label10.Text = grand_total.ToString("c");
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
oilandlube();
carwash();
flushes();
misc();
partsandlabor();
summary();
}
private void ClearOilandlube()
{
checkBox1.Checked = false;
checkBox2.Checked = false;
}
private void ClearMisc()
{
checkBox5.Checked = false;
checkBox6.Checked = false;
checkBox7.Checked = false;
}
private void ClearFlushes()
{
checkBox3.Checked = false;
checkBox4.Checked = false;
}
private void ClearSummary()
{
label7.Text = "";
label8.Text = "";
label9.Text = "";
label10.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
ClearOilandlube();
ClearMisc();
ClearFlushes();
ClearSummary();
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
textBox1.Text = "0";
textBox2.Text = "0";
}
}
}
When you clear the contents of your controls, you should also clear the values of the backing variables, so they don't retain their previous values. You should be able to just set them back to zero in your Clear methods.
For example, oil and lube might look like:
private void ClearOilandlube()
{
checkBox1.Checked = false;
checkBox2.Checked = false;
Oil_change = 0;
Lube_job = 0;
Mixed = 0;
Regular = 0;
Full_Synthetic = 0;
}
It looks like you're holding state of some your variables globally so you can access them elsewhere.
Mixed = 10;
You'll have to reset that to some default value as well.

Stopping the calculations because of predicted error

I am unsure how to ask this question as I can't quite well translate it.
I am currently working on my own Windows Form Application that will calculate the dimensions of a given package in inches (written all together in string format) and calculate the dimensions in centimeters, milimeters or even meters and at this point I was wondering what if someone entered wrong dimensions and given measures can not be parsed.
Something like Environment.Exit(), but without closing the application just stopping calculations and writing a message that an error has occured.
If there is a question like this answered, please do link it because I haven't been able to find it.
namespace PretvaranjeDimenzija
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double mjera = 1;
public bool pogreska = false;
public string mjeraKratica = " cm";
public string dimenzijeIspis = "";
private void buttonIzracunaj_Click(object sender, EventArgs e)
{
string dim = textBoxDimenzije.Text;
if (dim.Contains('.'))
{
dim = dim.Replace('.', ',');
}
if (dim.Length != 0)
{
if (dim.IndexOf('x') != -1)
{
string[] multiDim = dim.Split('x');
double[] multiDimCentimetara = new double[multiDim.Length];
bool[] uspjeh = new bool[multiDim.Length];
for (int i = 0; i < multiDim.Length; i++)
{
uspjeh[i] = double.TryParse(multiDim[i], out multiDimCentimetara[i]);
if (uspjeh[i] == false)
{
pogreska = true;
goto kraj;
}
}
kraj:
if (pogreska == true)
{
MessageBox.Show("Doslo je do pogreske!");
pogreska = false;
}
else
{
double[] dimenzije = new double[multiDim.Length];
for (int i = 0; i < dimenzije.Length; i++)
{
dimenzije[i] = multiDimCentimetara[i] * 2.54 * mjera;
if (i == dimenzije.Length - 1)
{
dimenzijeIspis += dimenzije[i].ToString() + mjeraKratica;
}
else
{
dimenzijeIspis += dimenzije[i].ToString() + "x";
}
}
textBoxIspisDimenzija.Text = dimenzijeIspis;
dimenzijeIspis = "";
}
}
else
{
double dimCentimetara;
if(double.TryParse(dim, out dimCentimetara))
{
double dimenzija = dimCentimetara * 2.54 * mjera;
dimenzijeIspis = dimenzija.ToString() + mjeraKratica;
textBoxIspisDimenzija.Text = dimenzijeIspis;
dimenzijeIspis = "";
}
else
{
MessageBox.Show("Doslo je do pogreske!");
return;
}
}
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
mjera = 0.01;
mjeraKratica = " m";
if (radioButton2.Checked == true)
{
radioButton2.Checked = false;
radioButton1.Checked = true;
}
if (radioButton3.Checked == true)
{
radioButton3.Checked = false;
radioButton1.Checked = true;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
mjera = 1;
mjeraKratica = " cm";
if (radioButton1.Checked == true)
{
radioButton1.Checked = false;
radioButton2.Checked = true;
}
if (radioButton3.Checked == true)
{
radioButton3.Checked = false;
radioButton2.Checked = true;
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
mjera = 10;
mjeraKratica = " mm";
if (radioButton2.Checked == true)
{
radioButton2.Checked = false;
radioButton3.Checked = true;
}
if (radioButton1.Checked == true)
{
radioButton1.Checked = false;
radioButton3.Checked = true;
}
}
}
It should be pretty simple, depending on your requirements. For example, you could just use a basic if block in your method.
void CalculateStuff()
{
// Get input. Do stuff.
if (IsInvalid)
{
MessageBox.Show("You did a bad thing.");
return; // exit the method.
}
// now that we know the input is good, do other stuff.
}
Substitute IsInvalid with whatever check condition you want that will return true if the input is not valid.

C# Make an autocomplete to a RichTextBox

I have a list of numberes, and I want to make a RichTextBox with autocomplete, so when the user start typing a number, it will give him autocomplete options from the list.
And I want it to be in a RichTextBox because I want the user to be able writing each number in a new line in the RichTextBox, so everytime the user presses enter, it will do the whole autocomplete thing all over again.
I don't really have an idea how to et this ting done.
any help would be very thankfull.
Thanks
I'll paste the code here. I never got the positioning of the box correct, so you will have to play around with the YOffset and the XOffset.
In your form OnLoad:
intellisenseRichTextBox.IntellisenseWords = new string[] { "Test", "Foo", "Bar", "Supercalifragilisticexpialidocious" };
intellisenseRichTextBox.YOffset = 132;
intellisenseRichTextBox.XOffset = 70;
And the classes:
public class NotificationForm : Form
{
public NotificationForm()
{
this.ShowInTaskbar = false;
//this.Enabled = true;
this.FormBorderStyle = FormBorderStyle.None;
}
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams baseParams = base.CreateParams;
const int WS_EX_NOACTIVATE = 0x08000000;
const int WS_EX_TOOLWINDOW = 0x00000080;
baseParams.ExStyle |= (int)(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
return baseParams;
}
}
}
public class RichTextBoxIntellisense : RichTextBox
{
private NotificationForm intelliAutoCompleteForm;
private ListBox intelliAutoCompleteListBox;
private bool IsNotificationFormShowing;
private bool IsFromReplaceTab;
private bool IsFromMouseClick;
private int _suggestionCount = 20;
public string[] IntellisenseWords { get; set; }
public int YOffset { get; set; }
public int XOffset { get; set; }
public int SuggestionCount
{
get
{
return _suggestionCount;
}
set
{
_suggestionCount = value;
}
}
public RichTextBoxIntellisense()
{
this.AcceptsTab = true;
this.YOffset = 30;
intelliAutoCompleteForm = new NotificationForm();
intelliAutoCompleteForm.Size = new Size(200, 200);
intelliAutoCompleteForm.TopMost = true;
intelliAutoCompleteListBox = new ListBox();
intelliAutoCompleteListBox.Dock = DockStyle.Fill;
intelliAutoCompleteForm.Controls.Add(intelliAutoCompleteListBox);
this.TextChanged += RichTextBoxIntellisense_TextChanged;
this.KeyDown += RichTextBoxIntellisense_KeyDown;
this.KeyPress += RichTextBoxIntellisense_KeyPress;
this.SelectionChanged += RichTextBoxIntellisense_SelectionChanged;
intelliAutoCompleteListBox.Click += IntelliAutoCompleteListBox_Click;
intelliAutoCompleteListBox.SelectedIndexChanged += IntelliAutoCompleteListBox_SelectedIndexChanged;
intelliAutoCompleteForm.Show();
intelliAutoCompleteForm.Hide();
}
private void IntelliAutoCompleteListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(IsFromMouseClick)
{
if (intelliAutoCompleteListBox.SelectedIndex >= 0)
{
ReplaceCurrentWordWith(intelliAutoCompleteListBox.SelectedItem.ToString());
IsFromReplaceTab = false;
}
}
IsFromMouseClick = false;
}
private void IntelliAutoCompleteListBox_Click(object sender, EventArgs e)
{
IsFromMouseClick = true;
}
private void RichTextBoxIntellisense_SelectionChanged(object sender, EventArgs e)
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
private void RichTextBoxIntellisense_KeyPress(object sender, KeyPressEventArgs e)
{
if (IsFromReplaceTab)
{
e.Handled = true;
IsFromReplaceTab = false;
}
else
{
if(e.KeyChar == (char)Keys.Escape)
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
e.Handled = true;
}
}
}
private void RichTextBoxIntellisense_KeyDown(object sender, KeyEventArgs e)
{
if (IsNotificationFormShowing)
{
if (e.KeyCode == Keys.Up)
{
if(intelliAutoCompleteListBox.Items.Count > 0)
{
if(intelliAutoCompleteListBox.SelectedIndex < 0)
{
intelliAutoCompleteListBox.SelectedIndex = 0;
}
else if(intelliAutoCompleteListBox.SelectedIndex > 0)
{
intelliAutoCompleteListBox.SelectedIndex = intelliAutoCompleteListBox.SelectedIndex - 1;
}
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Down)
{
if (intelliAutoCompleteListBox.Items.Count > 0)
{
if (intelliAutoCompleteListBox.SelectedIndex < 0)
{
intelliAutoCompleteListBox.SelectedIndex = 0;
}
else if (intelliAutoCompleteListBox.SelectedIndex < intelliAutoCompleteListBox.Items.Count - 1)
{
intelliAutoCompleteListBox.SelectedIndex = intelliAutoCompleteListBox.SelectedIndex + 1;
}
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Enter)
{
if(intelliAutoCompleteListBox.SelectedIndex >= 0)
{
ReplaceCurrentWordWith(intelliAutoCompleteListBox.SelectedItem.ToString());
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Tab)
{
if (intelliAutoCompleteListBox.SelectedIndex >= 0)
{
ReplaceCurrentWordWith(intelliAutoCompleteListBox.SelectedItem.ToString());
}
e.Handled = true;
}
}
}
private void RichTextBoxIntellisense_TextChanged(object sender, EventArgs e)
{
if (IntellisenseWords != null && IntellisenseWords.Length > 0)
{
string wordText;
int lastIndexOfSpace;
int lastIndexOfNewline;
int lastIndexOfTab;
int lastIndexOf;
if(this.SelectionStart == this.Text.Length)
{
if(this.SelectionStart > 0 && this.Text[this.SelectionStart - 1] != ' ' && this.Text[this.SelectionStart - 1] != '\t' && this.Text[this.SelectionStart - 1] != '\n')
{
wordText = this.Text.Substring(0,this.SelectionStart);
lastIndexOfSpace = wordText.LastIndexOf(' ');
lastIndexOfNewline = wordText.LastIndexOf('\n');
lastIndexOfTab = wordText.LastIndexOf('\t');
lastIndexOf = Math.Max(Math.Max(lastIndexOfSpace,lastIndexOfNewline),lastIndexOfTab);
if (lastIndexOf >= 0)
{
wordText = wordText.Substring(lastIndexOf + 1);
}
if (PopulateIntelliListBox(wordText))
{
ShowAutoCompleteForm();
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
else
{
char currentChar = this.Text[this.SelectionStart];
if(this.SelectionStart > 0)
{
if(this.SelectionStart > 0 && this.Text[this.SelectionStart - 1] != ' ' && this.Text[this.SelectionStart - 1] != '\t' && this.Text[this.SelectionStart - 1] != '\n'
&& (this.Text[this.SelectionStart] == ' ' || this.Text[this.SelectionStart] == '\t' || this.Text[this.SelectionStart] == '\n'))
{
wordText = this.Text.Substring(0, this.SelectionStart);
lastIndexOfSpace = wordText.LastIndexOf(' ');
lastIndexOfNewline = wordText.LastIndexOf('\n');
lastIndexOfTab = wordText.LastIndexOf('\t');
lastIndexOf = Math.Max(Math.Max(lastIndexOfSpace, lastIndexOfNewline), lastIndexOfTab);
if (lastIndexOf >= 0)
{
wordText = wordText.Substring(lastIndexOf + 1);
}
if(PopulateIntelliListBox(wordText))
{
ShowAutoCompleteForm();
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
}
else
{
intelliAutoCompleteForm.Hide();
IsNotificationFormShowing = false;
}
}
private bool PopulateIntelliListBox(string wordTyping)
{
intelliAutoCompleteListBox.Items.Clear();
List<KeyValuePair<int, string>> tempWords = new List<KeyValuePair<int, string>>();
string[] outArray;
for(int i = 0; i < IntellisenseWords.Length; i++)
{
if(IntellisenseWords[i].StartsWith(wordTyping, StringComparison.CurrentCultureIgnoreCase))
{
tempWords.Add(new KeyValuePair<int, string>(1, IntellisenseWords[i]));
}
}
if(tempWords.Count < _suggestionCount && wordTyping.Length > 1)
{
for (int i = 0; i < IntellisenseWords.Length; i++)
{
if (IntellisenseWords[i].Contains(wordTyping, StringComparison.CurrentCultureIgnoreCase))
{
if(tempWords.Count(c => c.Value == IntellisenseWords[i]) == 0)
{
tempWords.Add(new KeyValuePair<int, string>(2, IntellisenseWords[i]));
}
}
}
}
outArray = tempWords.OrderBy(o => o.Key).Take(_suggestionCount).Select(s => s.Value).ToArray();
intelliAutoCompleteListBox.Items.AddRange(outArray);
return outArray.Length > 0;
}
private void ShowAutoCompleteForm()
{
Point tmpPoint = this.PointToClient(this.Location);
Point tmpSelPoint = this.GetPositionFromCharIndex(this.SelectionStart);
intelliAutoCompleteForm.Location = new Point((-1 * tmpPoint.X) + tmpSelPoint.X + this.XOffset, (-1 * tmpPoint.Y) + tmpSelPoint.Y + this.Margin.Top + this.YOffset);
intelliAutoCompleteForm.Show();
IsNotificationFormShowing = true;
this.Focus();
}
private void ReplaceCurrentWordWith(string Word)
{
string startString = "";
string endString = this.Text.Substring(this.SelectionStart);
string wordText = this.Text.Substring(0, this.SelectionStart);
int lastIndexOfSpace = wordText.LastIndexOf(' ');
int lastIndexOfNewline = wordText.LastIndexOf('\n');
int lastIndexOfTab = wordText.LastIndexOf('\t');
int lastIndexOf = Math.Max(Math.Max(lastIndexOfSpace, lastIndexOfNewline), lastIndexOfTab);
if (lastIndexOf >= 0)
{
startString = wordText.Substring(0, lastIndexOf + 1);
wordText = wordText.Substring(lastIndexOf + 1);
}
this.Text = String.Format("{0}{1} {2}", startString, Word, endString);
if(lastIndexOf >= 0)
{
this.SelectionStart = startString.Length + Word.Length + 1;
}
else
{
this.SelectionStart = Word.Length + 1;
}
IsFromReplaceTab = true;
}
}

How to build an xml with data retrieved from usercontrols?

I'm trying to make a little software in which I could modify a pre-existing xml file. I would have the chance to add some values (via usercontrols) to the alphabet letters, and send these values back to the xml, saving it.
I already call back the xml file, so that I can load the list of letters I need.
anyway, first things first, I'd need to retrieve the data from the usercontrols.
Here's the code in the form that contains quite everything:
public partial class SettingLetterInput : UserControl {
Dictionary<char, Actions> ListaLettere;
public SettingLetterInput() {
InitializeComponent();
}
private void SettingLetterInput_Load(object sender, EventArgs e) {
string path = Path.GetFullPath("Config.xml");
ListaLettere = Actions.GetFromXML(path);
listLetterBox1.Items.AddRange(
ListaLettere.Select(l => (object)l.Key).ToArray());
foreach (var a in Enum.GetValues(typeof(Actions.acts))) {
actionsBox.Items.Add(a);
}
}
private void AddActionBtn_Click(object sender, EventArgs e) {
if (actionsBox.Text != "") {
Actions.acts act = (Actions.acts)Enum.Parse(typeof(Actions.acts), actionsBox.Text);
AddRow(act, null);
} else {
MessageBox.Show("no action selected");
}
}
public void AddRow(Actions.acts act, object values) {
ActionRowCtrl arc;
#region switch per controls
switch (act) {
case Actions.acts.addPosition:
arc = new ActionRow_SizeCtrl(values);
arc.actNameLbl.Text = this.actionsBox.Text;
break;
case Actions.acts.changeColor:
arc = new ActionRow_ColorCtrl(values);
arc.actNameLbl.Text = this.actionsBox.Text;
break;
case Actions.acts.addCircle:
arc = new ActionRow_FormCtrl();
arc.actNameLbl.Text = this.actionsBox.Text;
break;
default:
arc = new ActionRowCtrl();
break;
}
#endregion
this.panel1.Controls.Add(arc);
positionRow();
}
public void positionRow() {
int lastTop = -1;
int lastHeight=0;
int rowN = 0;
foreach (ActionRowCtrl c in panel1.Controls) {
rowN++;
c.rowLbl.Text = rowN.ToString();
if (( rowN % 2 ) == 0)
c.BackColor = Color.White;
else
c.BackColor = Color.Bisque;
c.Top = 0;
if (lastTop > -1) {
c.Top = lastTop + lastHeight;
}
lastTop = c.Top;
lastHeight = c.Height;
}
}
public void deleteRow(ActionRowCtrl arc) {
this.panel1.Controls.Remove(arc);
arc.Dispose();
positionRow();
}

Categories

Resources