Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have multiple text boxes on a form and I want to display the name of the textbox when I click on it in a specific textbox. As I have 300 textboxes I dont want to create 300 click Events. Do I need some kind of loop?
Yes, exactly, iterate through all your Controls on your Form and if Control is a TextBox, subscribe to an event like this:
private void subScribeAllTextBoxClickEvents()
{
foreach(var ctrl in this.Controls)
{
var textBox = ctrl as TextBox;
if(textBox != null)
{
textBox.Click += textBox_Click;
}
}
}
private void textBox_Click(object sender, EventArgs e)
{
}
You could call subScribeAllTextBoxClickEvents method for example in your constructor.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to do a filtering algorithm with C#, it will be a textbox and I will enter the forbidden words there. If banned words are found in the text I enter in richtextbox later, the word will be deleted as soon as it is written. How can I do this?
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
string[] badWords = textBox1.Text.Split(',');
string[] myText = richTextBox1.Text.Split(',');
foreach (var badWord in badWords)
{
if (myText.Contains(badWord))
{
richTextBox1.Text.Replace(badWord, "");
}
}
}
C# strings are immutable, you need to assign the Text property of your RichTextBox:
if (myText.Contains(badWord))
{
richTextBox1.Text = richTextBox1.Text.Replace(badWord, "");
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Using a Windows Form I am trying to replicate the functionality of a "Tab Control" but instead of the panel selection being controlled by a tab selection it would be controlled by a list. Is there a built in way to produce this using neatly like the tab control or should I just check if a list value is equal to some value and if yes display panel else don't?
This is probably a dumb question so sorry for wasting anyone's time and thanks in advance!
Besides, you can also use tabpages in tabcontrol as "panel". Just hide the header via the code
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
foreach (TabPage tab in tabControl1.TabPages)
{
tab.Text = "";
}
}
Then select the tabpage like,
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(((ListBox)sender).SelectedItem.ToString() == "tabPage2")
{
tabControl1.SelectedTab = tabPage2;
}
//...
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
So I'm trying to switch from C# console to form, but I always read that never repeat our code etc.
My first project would be a calculator, and I found a site just to take a look how it looks like in win form, but in this code there are a lot of repeating. Is this normal in form, in let's say a calculator?
Here is the link that I am talking about.
That is a lot of repetition, to improve it add one click event handler for all of the buttons, eg:
btn1.Click += btnClick;
btn2.Click += btnClick;
Then cast the sender to a Button to get which one was clicked, a rough example:
private void btnClick(object sender, EventArgs e)
{
var btnName = ((Button)sender).Name;
var btnValue = btnName.Replace("btn",string.Empty);
if (textBox1.Text == "0" && textBox1.Text != null)
{
textBox1.Text = btnValue;
}
else
{
textBox1.Text = textBox1.Text + btnValue;
}
}
Don't forget to unhook the event subscriptions in the form unload event:
btn1.Click -= btnClick;
btn2.Click -= btnClick;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to put button click effect in Xamarin forms. How can I do this? Am I need to use any plugin or any best way.
EDITED
I am creating Xamarin.Forms application and I want to make a more user-friendly button click. So, a user can feel like the button was pressed.
You need to create custom button like below:
public class CustomButton : Button
{
public CustomButton() : base()
{
const int _animationTime = 10;
Clicked += async (sender, e) =>
{
try
{
var btn = (CustomButton)sender;
await btn.ScaleTo(1.2, _animationTime);
await btn.ScaleTo(1, _animationTime);
}
catch (Exception ex)
{
ex.Track();
}
};
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a button that when pressed is currently outputting messages to a messagebox. Its not very tidy so I created a new form and placed a text box in there. While the 2nd form picks up the data it presents it in another messagebox rather then the textbox like intended. Help is appreciated.
Form2
public Form2(string strTextBox)
{
InitializeComponent();
textBox1.Text = strTextBox;
}
Form 1
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (string error in errorSet)
{
Form2 frm = new Form2(error);
frm.Show();
}
}
There is some more logic in the button_click if it looks a little strange but its pretty redundant for the problem im having.
Thank you
You should loop over your errors collection and store them in a StringBuilder, then, show the Form2 only one time outside the loop (if there is at least an error).
Do not forget to make your textbox MultiLine=true, give it enough height and a vertical scroll bar.
private void SaveButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string error in errorSet)
sb.AppendLine(error);
if(sb.Lenght> 0)
{
Form2 frm = new Form2(sb.ToString());
frm.Show();
}
}