C
Carlo Goretti
Guest
Im trying to remove checkboxes and textboxes from Form1.Control() with a method but i cant get it to work.. It dosent do it right! Can anyone see something wrong with the ClearControl() method..
private void LaddaHittaErsätt_Click(object sender, EventArgs e)
{
ClearControl(thisForm.Controls);
if (KundNamnTxt.Text == "")
{
MessageBox.Show("Du måste skriva in kundens namn");
return;
}
KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1);
KundMapp = KundLänk + KundNamnTxt.Text;
if (!Directory.Exists(KundMapp))
{
MessageBox.Show("Kunde inte hitta sökvägen till den nya mappen. " + Environment.NewLine + "Skapa gärna en nedan genom att skriva in Kundens namn och sedan klicka på 'Kör'!");
return;
}
HittaNamn = new List<string>();
files = Directory.GetFiles(KundMapp, "*.sql", SearchOption.AllDirectories);
List<string> wordResults = new List<string>();
foreach (string f in files)
{
List<string> wordList = (from line in File.ReadAllLines(f) from Match match in Regex.Matches(line.ToLower(), @"(?<!\w)momentum_\w+|(?<!\w)konvertering_\w+") select match.Value).ToList();
wordResults.AddRange(wordList);
}
string[] distinctWords = wordResults.Distinct().OrderBy(x => x).ToArray();
if (distinctWords.Count() > 0)
{
HittaLbl.Visible = true;
ErsättLbl.Visible = true;
CreateBtn.Visible = true;
foreach (string s in distinctWords)
{
HittaNamn.Add(s);
}
checkboxeAll = new CheckBox();
btn = new Button();
for (int i = 0; i < HittaNamn.Count(); i++)
{
textBoxes = new TextBox();
textBoxes2 = new TextBox();
checkboxes = new CheckBox();
textBoxes.Tag = "Momentum_Vänster" + i;
textBoxes.Name = "Momentum_Vänster" + i;
textBoxes.Text = HittaNamn.ToString();
textBoxes.Location = new Point(HittaLbl.Location.X + 3, HittaLbl.Location.Y + HittaLbl.Height + 5 + (i * 25));
textBoxes.Width = 200;
textBoxes.ReadOnly = true;
textBoxes.BackColor = SystemColors.Window;
ÄndraFrånLista.Add(textBoxes);
Controls.Add(textBoxes);
textBoxes2.Tag = "Momentum_Höger" + i;
//textBoxes2.Name = "Momentum_Höger" + i;
textBoxes2.Name = "Momentum_Höger" + i;
textBoxes2.Text = "";
textBoxes2.Location = new Point(ErsättLbl.Location.X + 3, ErsättLbl.Location.Y + ErsättLbl.Height + 5 + (i * 25));
textBoxes2.Width = 200;
ÄndraTillLista.Add(textBoxes2);
textBoxes2.TextChanged += new EventHandler(textBoxes2_TextChanged);
Controls.Add(textBoxes2);
checkboxes.Tag = "Momentum_Checkbox_" + i;
checkboxes.Name = "Momentum_Checkbox_" + i;
checkboxes.Font = new Font(checkboxes.Font.FontFamily, 10);
checkboxes.Location = new Point(textBoxes2.Location.X + textBoxes2.Width + 5, textBoxes2.Location.Y - 2);
checkboxes.CheckedChanged += new EventHandler(checkboxes_CheckedChanged);
checkboxes.Checked = false;
checkBoxesList.Add(checkboxes);
Controls.Add(checkboxes);
}
checkboxeAll.Tag = "Momentum_Checkbox_All";
checkboxeAll.Font = new Font(checkboxes.Font.FontFamily, 10);
checkboxeAll.Location = new Point(checkBoxesList[0].Location.X, checkBoxesList[0].Location.Y - 25);
checkboxeAll.CheckedChanged += new EventHandler(checkboxeAll_CheckedChanged);
Controls.Add(checkboxeAll);
btn.Size = new Size(75, 23);
btn.Location = new Point(checkboxeAll.Location.X - 90, checkboxeAll.Location.Y);
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 0;
btn.BackColor = Color.FromArgb(254, 66, 2);
btn.ForeColor = Color.White;
btn.Font = new Font(btn.Font.FontFamily, 10);
btn.Text = "Ta bort";
btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
if (CreateBtn.Location.Y + CreateBtn.Height < textBoxes.Location.Y + textBoxes.Height || CreateBtn.Location.Y + CreateBtn.Height < textBoxes2.Location.Y + textBoxes2.Height)
{
CreateBtn.Location = new Point(CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15);
}
if (Location.Y + Height < CreateBtn.Location.Y + CreateBtn.Height)
{
Height = CreateBtn.Location.Y + CreateBtn.Height + 50;
}
}
else
{
MessageBox.Show("Fanns inte några ord som innehöll 'Momentum_' i någon av filerna.");
HittaLbl.Visible = false;
ErsättLbl.Visible = false;
CreateBtn.Visible = false;
return;
}
}
//public void ClearTextBoxes(Control.ControlCollection ctrlCollection)
//{
// foreach (Control ctrl in ctrlCollection)
// {
// if (ctrl is TextBox && ctrl.Name != /*"StandardSökvägTxt"*/ StandardSökvägTxt.Name && ctrl.Name !=/* "KundNamnTxt"*/ KundNamnTxt.Name && ctrl.Name != /*"SökvägBtn"*/ SökvägBtn.Name && ctrl.Name != SkapaKundMapp.Name && ctrl.Name != LaddaHittaErsätt.Name && ctrl.Name != CreateBtn.Name)
// {
// ctrl.Dispose();
// }
// }
//}
public void ClearControl(Control.ControlCollection ctrlCollection)
{
foreach (Control ctrl in ctrlCollection)
{
if (ctrl is TextBox || ctrl is CheckBox)
{
for (int i = 0; i < ÄndraTillLista.Count(); i++)
{
if (ctrl.Name == "Momentum_Vänster" + i/*ctrl.Name.Contains("Momentum_Vänster")*/)
{
ctrl.Dispose();
}
if (ctrl.Name == "Momentum_Höger" + i/*ctrl.Name.Contains("Momentum_Höger")*/)
{
ctrl.Dispose();
}
if (ctrl.Name == "Momentum_Checkbox_" + i/*ctrl.Name.Contains("Momentum_Checkbox_")*/)
{
ctrl.Dispose();
}
}
}
}
}
Continue reading...
private void LaddaHittaErsätt_Click(object sender, EventArgs e)
{
ClearControl(thisForm.Controls);
if (KundNamnTxt.Text == "")
{
MessageBox.Show("Du måste skriva in kundens namn");
return;
}
KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1);
KundMapp = KundLänk + KundNamnTxt.Text;
if (!Directory.Exists(KundMapp))
{
MessageBox.Show("Kunde inte hitta sökvägen till den nya mappen. " + Environment.NewLine + "Skapa gärna en nedan genom att skriva in Kundens namn och sedan klicka på 'Kör'!");
return;
}
HittaNamn = new List<string>();
files = Directory.GetFiles(KundMapp, "*.sql", SearchOption.AllDirectories);
List<string> wordResults = new List<string>();
foreach (string f in files)
{
List<string> wordList = (from line in File.ReadAllLines(f) from Match match in Regex.Matches(line.ToLower(), @"(?<!\w)momentum_\w+|(?<!\w)konvertering_\w+") select match.Value).ToList();
wordResults.AddRange(wordList);
}
string[] distinctWords = wordResults.Distinct().OrderBy(x => x).ToArray();
if (distinctWords.Count() > 0)
{
HittaLbl.Visible = true;
ErsättLbl.Visible = true;
CreateBtn.Visible = true;
foreach (string s in distinctWords)
{
HittaNamn.Add(s);
}
checkboxeAll = new CheckBox();
btn = new Button();
for (int i = 0; i < HittaNamn.Count(); i++)
{
textBoxes = new TextBox();
textBoxes2 = new TextBox();
checkboxes = new CheckBox();
textBoxes.Tag = "Momentum_Vänster" + i;
textBoxes.Name = "Momentum_Vänster" + i;
textBoxes.Text = HittaNamn.ToString();
textBoxes.Location = new Point(HittaLbl.Location.X + 3, HittaLbl.Location.Y + HittaLbl.Height + 5 + (i * 25));
textBoxes.Width = 200;
textBoxes.ReadOnly = true;
textBoxes.BackColor = SystemColors.Window;
ÄndraFrånLista.Add(textBoxes);
Controls.Add(textBoxes);
textBoxes2.Tag = "Momentum_Höger" + i;
//textBoxes2.Name = "Momentum_Höger" + i;
textBoxes2.Name = "Momentum_Höger" + i;
textBoxes2.Text = "";
textBoxes2.Location = new Point(ErsättLbl.Location.X + 3, ErsättLbl.Location.Y + ErsättLbl.Height + 5 + (i * 25));
textBoxes2.Width = 200;
ÄndraTillLista.Add(textBoxes2);
textBoxes2.TextChanged += new EventHandler(textBoxes2_TextChanged);
Controls.Add(textBoxes2);
checkboxes.Tag = "Momentum_Checkbox_" + i;
checkboxes.Name = "Momentum_Checkbox_" + i;
checkboxes.Font = new Font(checkboxes.Font.FontFamily, 10);
checkboxes.Location = new Point(textBoxes2.Location.X + textBoxes2.Width + 5, textBoxes2.Location.Y - 2);
checkboxes.CheckedChanged += new EventHandler(checkboxes_CheckedChanged);
checkboxes.Checked = false;
checkBoxesList.Add(checkboxes);
Controls.Add(checkboxes);
}
checkboxeAll.Tag = "Momentum_Checkbox_All";
checkboxeAll.Font = new Font(checkboxes.Font.FontFamily, 10);
checkboxeAll.Location = new Point(checkBoxesList[0].Location.X, checkBoxesList[0].Location.Y - 25);
checkboxeAll.CheckedChanged += new EventHandler(checkboxeAll_CheckedChanged);
Controls.Add(checkboxeAll);
btn.Size = new Size(75, 23);
btn.Location = new Point(checkboxeAll.Location.X - 90, checkboxeAll.Location.Y);
btn.FlatStyle = FlatStyle.Flat;
btn.FlatAppearance.BorderSize = 0;
btn.BackColor = Color.FromArgb(254, 66, 2);
btn.ForeColor = Color.White;
btn.Font = new Font(btn.Font.FontFamily, 10);
btn.Text = "Ta bort";
btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
if (CreateBtn.Location.Y + CreateBtn.Height < textBoxes.Location.Y + textBoxes.Height || CreateBtn.Location.Y + CreateBtn.Height < textBoxes2.Location.Y + textBoxes2.Height)
{
CreateBtn.Location = new Point(CreateBtn.Location.X, textBoxes.Location.Y + textBoxes.Height + 15);
}
if (Location.Y + Height < CreateBtn.Location.Y + CreateBtn.Height)
{
Height = CreateBtn.Location.Y + CreateBtn.Height + 50;
}
}
else
{
MessageBox.Show("Fanns inte några ord som innehöll 'Momentum_' i någon av filerna.");
HittaLbl.Visible = false;
ErsättLbl.Visible = false;
CreateBtn.Visible = false;
return;
}
}
//public void ClearTextBoxes(Control.ControlCollection ctrlCollection)
//{
// foreach (Control ctrl in ctrlCollection)
// {
// if (ctrl is TextBox && ctrl.Name != /*"StandardSökvägTxt"*/ StandardSökvägTxt.Name && ctrl.Name !=/* "KundNamnTxt"*/ KundNamnTxt.Name && ctrl.Name != /*"SökvägBtn"*/ SökvägBtn.Name && ctrl.Name != SkapaKundMapp.Name && ctrl.Name != LaddaHittaErsätt.Name && ctrl.Name != CreateBtn.Name)
// {
// ctrl.Dispose();
// }
// }
//}
public void ClearControl(Control.ControlCollection ctrlCollection)
{
foreach (Control ctrl in ctrlCollection)
{
if (ctrl is TextBox || ctrl is CheckBox)
{
for (int i = 0; i < ÄndraTillLista.Count(); i++)
{
if (ctrl.Name == "Momentum_Vänster" + i/*ctrl.Name.Contains("Momentum_Vänster")*/)
{
ctrl.Dispose();
}
if (ctrl.Name == "Momentum_Höger" + i/*ctrl.Name.Contains("Momentum_Höger")*/)
{
ctrl.Dispose();
}
if (ctrl.Name == "Momentum_Checkbox_" + i/*ctrl.Name.Contains("Momentum_Checkbox_")*/)
{
ctrl.Dispose();
}
}
}
}
}
Continue reading...