Method '*****' not found. Warning displayed in 'Form1.cs [Design]'pane


A

anthony

I am new to C# programming and have a simple question for the group
here. I'll just display my code and Warning message.

-------------------------------------------------------------------------------------
//Form1.cs

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;
using System.Runtime.InteropServices;


namespace NewForm
{
public partial class Form1 : Form {
[DllImport("ole32.dll")]
static extern void CoFreeUnusedLibraries();
private System.Windows.Forms.ComboBox comboBox1;

public Form1() {
InitializeComponent();
}

public void DisplayMessage() {
MessageBox.Show("test");
}
}
}
-------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------
//Form1.Designer.cs

namespace NewForm
{
partial class Form1 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox1.SuspendLayout();
this.SuspendLayout();
//
// comboBox1
this.comboBox1.Anchor =
((System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.comboBox1.DropDownWidth = 280;
this.comboBox1.Location = new System.Drawing.Point(21,
20);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(284, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.BackColor = System.Drawing.Color.White;
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.DodgerBlue;
this.ClientSize = new System.Drawing.Size(338, 67);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "ComboBox";
this.DisplayMessage(); // WARNING
ASSOCIATED WITH THIS LINE
this.comboBox1.ResumeLayout(false);
this.comboBox1.PerformLayout();
this.ResumeLayout(false);
}

#endregion
}
}
-------------------------------------------------------------------------------------

Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.

Comment: I know that DisplayMessage is not a method of the Form
object. I am trying to get Form1 class to define a new form in
adddition to those derived from Form. I thought that was what the
following line was doing from Form1.cs:

public partial class Form1 : Form {


Apparently, I do not know how to add additional methods to a class
which was derived from another class. In any event, this is what I
wish to do. Does someone know how to do this? Also, I know that this
method does not have a purpose here. It is going to be substituted
with another method, but I chose to write something smaller and
simpler to generally illustrate the issue I am having. Thanks you for
your assistance in advance.
 
Ad

Advertisements

J

Jeff Gaines

Comment: I know that DisplayMessage is not a method of the Form
object.

And it shouldn't be in the designer file - generally you shouldn't
manually edit the designer file, your code should be in the Form1.cs file.
What's happening here is that you are trying to call a function before the
form has been initialised.
I am trying to get Form1 class to define a new form in
adddition to those derived from Form. I thought that was what the
following line was doing from Form1.cs:

public partial class Form1 : Form {

If you want, say, Form2 derived from Form1 then it would be (preferably in
its own code file):

public partial class Form2 : Form1 {
 
A

anthony

And it shouldn't be in the designer file - generally you shouldn't
manually edit the designer file, your code should be in the Form1.cs file..
What's happening here is that you are trying to call a function before the
form has been initialised.



If you want, say, Form2 derived from Form1 then it would be (preferably in
its own code file):

public partial class Form2 : Form1 {

My apologies, I meant to say:

I am trying to get Form1 class to define a new METHOD in adddition to
those derived from Form.

-anthony
 
P

Peter Duniho

[...]
Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.

Comment: I know that DisplayMessage is not a method of the Form
object.

But it needs to be for the Designer to know what to do with it.

The file where you're trying to add the call site to your new method is a
special file: in addition to being compiled by the C# compiler to create
your Form sub-class, it is parsed by the Visual Studio Designer as part of
showing you the graphical representation of the object and allowing you to
edit it.

If the Designer comes across something it doesn't recognize, it has no way
to know what to do with it. So, while the compiles and runs okay (or at
least, in the example you gave it should), when it comes time for you to
try to edit the control graphically with the Designer, it emits the
warning you describe, explaining to you that it found something it doesn't
understand.
I am trying to get Form1 class to define a new form in
adddition to those derived from Form. I thought that was what the
following line was doing from Form1.cs:

public partial class Form1 : Form {

Well, yes...the ": Form" does indicate to the C# compiler that your new
Form1 class inherits the Form class. But the C# compiler isn't what's
giving the message.
Apparently, I do not know how to add additional methods to a class
which was derived from another class.

You are adding the method just fine. It's your use of the method that is
causing problems.
In any event, this is what I
wish to do. Does someone know how to do this? Also, I know that this
method does not have a purpose here. It is going to be substituted
with another method, but I chose to write something smaller and
simpler to generally illustrate the issue I am having.

As Jeff says, you should not be editing the *.Designer.cs file. It is not
literally possible to do what you seem to be wanting to do, while still
preserving the Designer's ability to work with your class correctly. It's
not clear from your question why you want to insert the method call in the
*.Designer.cs file, nor for that matter why you have declared a control
member in the regular *.cs file (where the Designer can't see it...not
that this issue is connected to the specific question you're asking).

It's possible that you should simply call the method from your own
construction, after the call to InitializeComponent(), or that you should
override the OnFormLoad() or OnShown() methods and call the method in the
override. Or it's possible there's some entirely different thing you
should be doing. If you can describe in a clear, specific way what it is
you're actually trying to accomplish, we might be able to offer useful
advice along those lines.

But whatever it is you're really trying to do here, manually writing code
in the *.Designer.cs file isn't the right way to do it.

Pete
 
F

Family Tree Mike

anthony said:
I am new to C# programming and have a simple question for the group
here. I'll just display my code and Warning message.

-------------------------------------------------------------------------------------
//Form1.cs

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;
using System.Runtime.InteropServices;


namespace NewForm
{
public partial class Form1 : Form {
[DllImport("ole32.dll")]
static extern void CoFreeUnusedLibraries();
private System.Windows.Forms.ComboBox comboBox1;

public Form1() {
InitializeComponent();
}

public void DisplayMessage() {
MessageBox.Show("test");
}
}
}
-------------------------------------------------------------------------------------


-------------------------------------------------------------------------------------
//Form1.Designer.cs

namespace NewForm
{
partial class Form1 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox1.SuspendLayout();
this.SuspendLayout();
//
// comboBox1
this.comboBox1.Anchor =
((System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.comboBox1.DropDownWidth = 280;
this.comboBox1.Location = new System.Drawing.Point(21,
20);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(284, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.BackColor = System.Drawing.Color.White;
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.DodgerBlue;
this.ClientSize = new System.Drawing.Size(338, 67);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "ComboBox";
this.DisplayMessage(); // WARNING
ASSOCIATED WITH THIS LINE
this.comboBox1.ResumeLayout(false);
this.comboBox1.PerformLayout();
this.ResumeLayout(false);
}

#endregion
}
}
-------------------------------------------------------------------------------------

Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.

Comment: I know that DisplayMessage is not a method of the Form
object. I am trying to get Form1 class to define a new form in
adddition to those derived from Form. I thought that was what the
following line was doing from Form1.cs:

public partial class Form1 : Form {


Apparently, I do not know how to add additional methods to a class
which was derived from another class. In any event, this is what I
wish to do. Does someone know how to do this? Also, I know that this
method does not have a purpose here. It is going to be substituted
with another method, but I chose to write something smaller and
simpler to generally illustrate the issue I am having. Thanks you for
your assistance in advance.


I'm sorry, but I don't see why this would give a warning, and I don't
get any warning.
 
F

Family Tree Mike

Peter said:
[...]
Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.

Comment: I know that DisplayMessage is not a method of the Form
object.

But it needs to be for the Designer to know what to do with it.

The file where you're trying to add the call site to your new method is
a special file: in addition to being compiled by the C# compiler to
create your Form sub-class, it is parsed by the Visual Studio Designer
as part of showing you the graphical representation of the object and
allowing you to edit it.

Ah, thanks. I didn't realize the error showed when switching to the
designer. I looked, and compiled, and saw no problem.
 
Ad

Advertisements

A

anthony

Peter said:
[...]
Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.
Comment: I know that DisplayMessage is not a method of the Form
object.
But it needs to be for the Designer to know what to do with it.
The file where you're trying to add the call site to your new method is
a special file: in addition to being compiled by the C# compiler to
create your Form sub-class, it is parsed by the Visual Studio Designer
as part of showing you the graphical representation of the object and
allowing you to edit it.

Ah, thanks.  I didn't realize the error showed when switching to the
designer.  I looked, and compiled, and saw no problem.

Ok, sounds like some additional detail would be useful. The method
that I am going to substitute for DisplayMessage is going to extract
some things from one object to add as Items to the combobox. This is
why I was thinking that it needed to be called in the Initialize
component method, as it seems to be part of initializing this form.
If not called w/in InitializeComponent, how should this method be
invoked such that these combobox Items get created?

-Anthony
 
F

Family Tree Mike

anthony said:
Peter said:
[...]
Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.
Comment: I know that DisplayMessage is not a method of the Form
object.
But it needs to be for the Designer to know what to do with it.
The file where you're trying to add the call site to your new method is
a special file: in addition to being compiled by the C# compiler to
create your Form sub-class, it is parsed by the Visual Studio Designer
as part of showing you the graphical representation of the object and
allowing you to edit it.
Ah, thanks. I didn't realize the error showed when switching to the
designer. I looked, and compiled, and saw no problem.

Ok, sounds like some additional detail would be useful. The method
that I am going to substitute for DisplayMessage is going to extract
some things from one object to add as Items to the combobox. This is
why I was thinking that it needed to be called in the Initialize
component method, as it seems to be part of initializing this form.
If not called w/in InitializeComponent, how should this method be
invoked such that these combobox Items get created?

-Anthony


Put the call in this routine after the call to IntializeComponent:

public Form1() {
InitializeComponent();
// put call here...
}
 
Ad

Advertisements

A

anthony

anthony said:
Peter Duniho wrote:
[...]
Warning message: Method 'System.Windows.Forms.Form.DisplayMessage' not
found.
Comment: I know that DisplayMessage is not a method of the Form
object.
But it needs to be for the Designer to know what to do with it.
The file where you're trying to add the call site to your new method is
a special file: in addition to being compiled by the C# compiler to
create your Form sub-class, it is parsed by the Visual Studio Designer
as part of showing you the graphical representation of the object and
allowing you to edit it.
Ah, thanks.  I didn't realize the error showed when switching to the
designer.  I looked, and compiled, and saw no problem.
Ok, sounds like some additional detail would be useful.  The method
that I am going to substitute for DisplayMessage is going to extract
some things from one object to add as Items to the combobox.  This is
why I was thinking that it needed to be called in the Initialize
component method, as it seems to be part of initializing this form.
If not called w/in InitializeComponent, how should this method be
invoked such that these combobox Items get created?

Put the call in this routine after the call to IntializeComponent:

         public Form1() {
             InitializeComponent();
             // put call here...
         }

Worked like a charm. Thanks again to everyone for their assistance.

-anthony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top