Вы должны войти, чтобы создавать сообщения и темы.
Менять строки принимающего метода в зависимости от события в котором он выполняется
Игорь@onlyhgamescom
11 Сообщений
#1 · 19.11.2023, 06:51
Цитата: Игорь от 19.11.2023, 06:51Доброго времени суток
Представьте что у меня есть два разных события с буквально одним и тем же методом, но принимает этот метод разные переменные
private void urlTextBox1_Enter(object sender, EventArgs e) { TextBox_ChangeUrl(urlTextBox1, defaultUrlText1, urlRegex, "https://www.$1"); TextBox_ChangeTextColor(urlTextBox1, defaultUrlText1); } private void urlTextBox2_Enter(object sender, EventArgs e) { TextBox_ChangeUrl(urlTextBox2, defaultUrlText2, urlRegex, "https://www.$1"); TextBox_ChangeTextColor(urlTextBox2, defaultUrlText2); } void TextBox_ChangeUrl(System.Windows.Forms.TextBox textBox, string line, Regex regex, string regReplace) { if (textBox.Text == line) textBox.Text = ""; else if (textBox.Text == "") textBox.Text = line; else textBox.Text = regex.Replace(Convert.ToString(textBox.Text), regReplace); } void TextBox_ChangeTextColor(System.Windows.Forms.TextBox textBox, string line) { if (textBox.Text == line) { textBox.ForeColor = SystemColors.GrayText; textBox.BackColor = SystemColors.Window; } else { textBox.ForeColor = SystemColors.WindowText; textBox.BackColor = SystemColors.Window; } }События urlTextBox1_Enter и urlTextBox2_Enter во многом повторяются. Можно как то это это дело сократить?
Доброго времени суток
Представьте что у меня есть два разных события с буквально одним и тем же методом, но принимает этот метод разные переменные
private void urlTextBox1_Enter(object sender, EventArgs e)
{
TextBox_ChangeUrl(urlTextBox1, defaultUrlText1, urlRegex, "https://www.$1");
TextBox_ChangeTextColor(urlTextBox1, defaultUrlText1);
}
private void urlTextBox2_Enter(object sender, EventArgs e)
{
TextBox_ChangeUrl(urlTextBox2, defaultUrlText2, urlRegex, "https://www.$1");
TextBox_ChangeTextColor(urlTextBox2, defaultUrlText2);
}
void TextBox_ChangeUrl(System.Windows.Forms.TextBox textBox, string line, Regex regex, string regReplace)
{
if (textBox.Text == line)
textBox.Text = "";
else if (textBox.Text == "")
textBox.Text = line;
else
textBox.Text = regex.Replace(Convert.ToString(textBox.Text), regReplace);
}
void TextBox_ChangeTextColor(System.Windows.Forms.TextBox textBox, string line)
{
if (textBox.Text == line)
{
textBox.ForeColor = SystemColors.GrayText;
textBox.BackColor = SystemColors.Window;
}
else
{
textBox.ForeColor = SystemColors.WindowText;
textBox.BackColor = SystemColors.Window;
}
}
События urlTextBox1_Enter и urlTextBox2_Enter во многом повторяются. Можно как то это это дело сократить?
Голосуйте - палец вниз.0Голосуйте - палец вверх.0