Click here to Skip to main content
16,013,489 members
Home / Discussions / C#
   

C#

 
GeneralRe: Displaying Version and Build info in my program. Pin
frogb0x21-Jul-03 10:45
frogb0x21-Jul-03 10:45 
GeneralRe: Displaying Version and Build info in my program. Pin
sandman_max21-Jul-03 11:07
sandman_max21-Jul-03 11:07 
GeneralData source/Display memeber works but data binding doesnt Pin
Anonymous21-Jul-03 8:11
Anonymous21-Jul-03 8:11 
General.net sdk and visual studio compatability Pin
Zibar21-Jul-03 7:21
sussZibar21-Jul-03 7:21 
GeneralRe: .net sdk and visual studio compatability Pin
Mazdak21-Jul-03 9:45
Mazdak21-Jul-03 9:45 
GeneralRe: .net sdk and visual studio compatability Pin
Zibar22-Jul-03 1:05
sussZibar22-Jul-03 1:05 
GeneralCustom Validation For TextBox Pin
clayne21-Jul-03 7:19
clayne21-Jul-03 7:19 
GeneralRe: Custom Validation For TextBox Pin
David Stone21-Jul-03 14:11
sitebuilderDavid Stone21-Jul-03 14:11 
Well, you could derive your own textbox and override the OnValidating method to perform validation...or you could have a normal textbox and subscribe to it's Validating event and perform validation that way.

Basically you have:

Way 1)
public class ValidTexbox : TextBox 
{
    protected override void OnValidating(CancelEventArgs e)
    {
        base.OnValidating (e);
        if(this.Visible == true && this.Text.Length == 0) 
        {
            e.Cancel = true;
            MessageBox.Show("Input invalid"); //notify the user that it can't be empty
        }
    }
}


Or

Way 2)
public class Form1 : System.Windows.Forms.Form
{
    private System.Windows.Forms.TextBox textBox1;
    private System.ComponentModel.Container components = null;
    public Form1()
    {
        InitializeComponent();
    }
	
    private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(8, 16);
        this.textBox1.Name = "textBox1";
        this.textBox1.TabIndex = 0;
        this.textBox1.Text = "textBox1";
        this.textBox1.Validating += new System.ComponentModel.CancelEventHandlerthis.textBox1_Validating);
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.textBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if(textBox1.Visible == true && textBox1.Text.Length == 0) 
        {
            MessageBox.Show("Input invalid"); //notify the user that it can't be empty
        }
    }
}


Either way, you have to have the CausesValidation property of the textbox set to true. Then it'll work just fine. Smile | :)



Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...

-Anna-Jayne Metcalfe on Paintballing

Generaluser control in designer : path problem Pin
dake_cdx21-Jul-03 5:48
dake_cdx21-Jul-03 5:48 
GeneralRe: user control in designer : path problem Pin
dake_cdx21-Jul-03 23:20
dake_cdx21-Jul-03 23:20 
GeneralRe: Active window Pin
apferreira21-Jul-03 5:25
apferreira21-Jul-03 5:25 
GeneralRe: Active window Pin
apferreira21-Jul-03 5:55
apferreira21-Jul-03 5:55 
GeneralRe: Active window Pin
apferreira21-Jul-03 6:18
apferreira21-Jul-03 6:18 
GeneralRe: Active window Pin
J. Dunlap21-Jul-03 8:03
J. Dunlap21-Jul-03 8:03 
GeneralRe: Active window Pin
J. Dunlap21-Jul-03 7:57
J. Dunlap21-Jul-03 7:57 
GeneralRe: Active window Pin
J. Dunlap21-Jul-03 8:04
J. Dunlap21-Jul-03 8:04 
GeneralExchange Pin
totig21-Jul-03 5:10
totig21-Jul-03 5:10 
GeneralRe: Exchange Pin
apferreira21-Jul-03 5:19
apferreira21-Jul-03 5:19 
GeneralC# ListView Column Order/Reorder Pin
Scottk (TKS)21-Jul-03 4:24
Scottk (TKS)21-Jul-03 4:24 
GeneralRe: C# ListView Column Order/Reorder Pin
apferreira21-Jul-03 5:00
apferreira21-Jul-03 5:00 
GeneralOwner Draw Control Problems Pin
freshthinking21-Jul-03 3:11
freshthinking21-Jul-03 3:11 
GeneralRe: Owner Draw Control Problems Pin
J. Dunlap21-Jul-03 7:52
J. Dunlap21-Jul-03 7:52 
GeneralRe: Owner Draw Control Problems Pin
freshthinking21-Jul-03 11:28
freshthinking21-Jul-03 11:28 
GeneralRe: Owner Draw Control Problems Pin
J. Dunlap21-Jul-03 11:32
J. Dunlap21-Jul-03 11:32 
GeneralRe: Owner Draw Control Problems Pin
freshthinking21-Jul-03 20:58
freshthinking21-Jul-03 20:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.