Daniel Lip
Daniel Lip

Reputation: 11335

How do i make that only if the user entered any text to textBox so the button1 will be enabled?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GatherLinks
{
    public partial class CrawlLocaly : Form
    {
        public CrawlLocaly()
        {
            InitializeComponent();

        }

        public string getText()
        {
            return textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text))
            {
                DialogResult = DialogResult.OK;

            }
            else
            {

            }
        }




    }
}

In Form1 i Show this Form and its textBox:

private void button6_Click(object sender, EventArgs e)
        {

            using (var w = new StreamWriter(keywords))
            {
                crawlLocaly1 = new CrawlLocaly();
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                DialogResult dr = crawlLocaly1.ShowDialog(this);

I want to do in the crawlLocaly new Form that when i click in Form1 and open/show the new Form button1 on the new Form will be Enabled = false and once the user typed anything in the textBox in the new Form button1 will be enabled true and only then the user will able to click on the button1 in the new Form wich is OK(the text of the button in the new Form is OK).

Tried in the new Form to use on the button1 textchanged event but it didnt work. the button became false only after i clicked on it.

Upvotes: 0

Views: 1352

Answers (3)

Ravindra Bagale
Ravindra Bagale

Reputation: 17665

private void textBox1_TextChanged(object sender, EventArgs e) {
    button1.Enabled = true;
}

Upvotes: 0

AntLaC
AntLaC

Reputation: 1225

If you want to keep it in C# you can do this, don't forget to set AutoPostBack to True and test if the length is 0 to disable the button again:

    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" Enabled="false" />
    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_OntTextChanged" />

protected void TextBox1_OntTextChanged(object sender, EventArgs e)
{
    if (TextBox1.Text.Length > 0)
    {
        Button1.Enabled = true;
    }
    else
    {
        Button1.Enabled = false;
    }
}

Upvotes: 2

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can try with this code - based on TextChanged event

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   if(TextBox1.Text.Trim().Length > 0)
   {
      Button.Enabled = true;
   }
}

Nota : Initialize Button.Enabled to false; on control

Upvotes: 4

Related Questions