Reputation: 1
hi every one i have a form which contain a group control that have 47 button so, i want to loop through all the buttons and compare the text(button.text) of each button with textbox.text when a match is found then that button will be disable .. how to do this help please!!!!!
i am trying the following code .
private void simpleButton1_Click(object sender, EventArgs e)
{
int seatno = int.Parse(textEdit1.Text.Trim().ToString());
foreach (Button c in gp_seatss.Controls)
{
foreach (string str in Convert.ToString( c.Text))
{
if (str == Convert.ToString(seatno))
{
c.Enabled = false;
}
}
return;
}
}
Upvotes: 0
Views: 1074
Reputation: 11
this is what you want. Place this method in all textboxes textchange event. e.g.
TxtTextBox1.TextChanged(object sender, EventArgs e) {
CompareText(sender, e);
}
// And so on ...
internal void CompareText(object sender, EventArgs e) {
TextBox tTextBox = ((TextBox)sender);
foreach(Button bButton in GrpCompareText.Controls.OfType<Button>().ToList()) {
if(tTextBox != null && bButton != null) {
tTextBox.TextChanged += (sender, e) => {
if(string.IsNullOrWhiteSpace(tTextBox.Text) is false) {
if(bButton.Text.Equals(tTextBox.Text.Trim())) {
if(bButton.Enabled is true) { bButton.Enabled = false; }
}
}
if(string.IsNullOrWhiteSpace(tTextBox.Text) is true ||
bButton.Text.Equals(tTextBox.Text) is false) {
if(bButton.Enabled is false) { bButton.Enabled = true; }
}
};
}
}
}
A better approach would be to put the CompareText
method in a class library and call it from there, but it would require a little more code, this is just a basic example to get you started and to play around with until you get your desired results.
Lets say you have a class called CCompareText
we can use FormCollections to capture all open forms in out application and iterate down the chain to occumplish what we want, just give your groupbox and form a tag name. I find it a lot better to reference a control by its tag name instead of its control name. e.g.
The TextBox1 text changed event will turn into:
namespace YourAppName {
public partial class Form1 : Form {
CComareText? _cCompareText;
public Form1() {
InitializeComponent();
}
private void TxtTextBox1_TextChanged(object sender, EventArgs e) {
_cCompareText = new CComareText();
_cCompareText.CompareText(sender, e);
}
//=> Now Your CCompareTextClass:
//
namespace YourAppName {
public class CCompareText {
FormCollection fOpenForms = Application.OpenForms;
public void CompareText(object sender, EventArgs e) {
TextBox tTextBox = ((TextBox)sender);
//=> Check when `Form1` is open in your application
//
foreach(Form fOpenForm in fOpenForms.OfType<Form>().ToList()) {
if(fOpenForm.Tag != null && fOpenForm.Tag.Equals("Form1")) {
//=> Check for the `GrpCompareText` group box
//
foreach(GroupBox gGroupBox in fOpenForm.Controls.OfType<GroupBox>().ToList()) {
if(gGroupBox.Tag != null && gGroupBox.Tag.Equals("GrpCompareText")) {
//=> Get all buttons in `GrpCompareText`
//
foreach(Button bButton in gGroupBox.Controls.OfType<Button>().ToList()) {
//=> Always perform a null check
//
if(tTextBox != null && bButton != null) {
tTextBox.TextChanged += (sender, e) => {
if(string.IsNullOrWhiteSpace(tTextBox.Text) is false) {
if(bButton.Text.Equals(tTextBox.Text.Trim())) {
if(bButton.Enabled is true) { bButton.Enabled = false; }
}
}
if(string.IsNullOrWhiteSpace(tTextBox.Text) is true ||
bButton.Text.Equals(tTextBox.Text) is false) {
if(bButton.Enabled is false) { bButton.Enabled = true; }
}
};
}
}
}
}
}
}
}
}
}
I hope this helps.
Upvotes: 0
Reputation:
private void myMethod(Control.ControlCollection controls, string text)
{
foreach (Control ctrl in controls)
{
if (ctrl is Button)
ctrl.Enabled = ctrl.Text != text;
if (ctrl.HasChildren)
myMethod(ctrl.Controls, text);
}
}
Upvotes: 0
Reputation: 12439
Loop through all Button
s of Groupbox
and set the condition:
foreach (Button btn in groupBox1.Controls.OfType<Button>())
{
if (btn.Text == textBox1.Text)
btn.Enabled = false;
}
Upvotes: 1