Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# TabControl WinForm
When I click the tab then the error message popup as follows:
 
The control System.Windows.Forms.TabControl has thrown an unhandled exception in the designer and has been disabled.
 
Exception:
Object reference not set to an instance of an object.
 
Stack trace:
at
System.Windows.Forms.TabControl.UpdateTabSelection(Boolean updateFocus)
at
System.Windows.Forms.TabControl.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.TabControl.WmSelChange()
at System.Windows.Forms.TabControl.Wndproc(Message& m)
 
only two tabs

     //
            // tabControlServings
            //
            this.tabControlServings.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
            this.tabControlServings.Controls.Add(this.tabPageServingList);
            this.tabControlServings.Controls.Add(this.tabPageServingType);
            this.tabControlServings.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControlServings.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.tabControlServings.ItemSize = new System.Drawing.Size(120, 40);
            this.tabControlServings.Location = new System.Drawing.Point(0, 0);
            this.tabControlServings.Name = "tabControlServings";
            this.tabControlServings.SelectedIndex = 0;
            this.tabControlServings.Size = new System.Drawing.Size(409, 567);
            this.tabControlServings.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
            this.tabControlServings.TabIndex = 11;
//
// tabPageServingType
//
this.tabPageServingType.Controls.Add(this.panel2);
this.tabPageServingType.Controls.Add(this.panel1);
this.tabPageServingType.Controls.Add(this.dataRepeaterServingType);
this.tabPageServingType.Location = new System.Drawing.Point(4, 44);
this.tabPageServingType.Name = "tabPageServingType";
this.tabPageServingType.Padding = new System.Windows.Forms.Padding(3);
this.tabPageServingType.Size = new System.Drawing.Size(401, 519);
this.tabPageServingType.TabIndex = 1;
this.tabPageServingType.Text = "Serving Type";
this.tabPageServingType.UseVisualStyleBackColor = true;
 
 
It build success, the error appears just when clicking the tab in the designer view.
F5 and debugging there is no error appeared, I don't know why.
Posted 31-Jul-12 18:51pm
Edited 1-Aug-12 17:43pm
v3
Comments
Christian Graus at 31-Jul-12 23:56pm
   
So, your code, which is being executed, has a null object in it. Did you consider telling us the code, or did you not want help ?
chenghuichao at 1-Aug-12 0:11am
   
this comment is moved to question
Christian Graus at 1-Aug-12 0:12am
   
The design view is rubbish. It doesn't run all your code, only some of it. If it works fine when you run it, if you need it to run fine in the designer, run another instance of VS, attach it to the first, and it will catch the exception and you can work out what is happening, and what code you need to add, to make the designer work.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are showing unrelated code. No, don't show any code right now, you don't really understand what your own code does. You sample is unrelated, because the exception is in System.Windows.Forms:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tabcontrol.updatetabselection.aspx[^].
 
And you did not show any code around this call shown in your stack trace. You see, you exception is one of the easiest to fix. Run it under debugger. To start with, locate your call to the method shown above and put a breakpoint on this line. Try to run. Before the call, look at the members and variables involved. At least one of them is null but is not supposed to be null. Inspect all relevant value under debugger before a call and find out the bug. Apply this approach to other cases. Fix the bug: either make sure the problematic reference variable or member is initialized with a non-null reference (constructor called or your reference is assigned to some already initialized variable), or, if appropriate, make a conditional statement: check a variable or a member in question for null and don't the operation where it is used if it is null.
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100