Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Two simple questions.

Posted on 2002-07-26
8
Medium Priority
?
247 Views
Last Modified: 2010-04-15
Dear Experts,

I just turn from VB6 to C# and have a few questions:

1. In VB, I can write global functions and varables for all forms in a standard module, where should I put those thing for C#.  Should I write them in a class and make them all static?  If not static, is it too troublesome to instanate that class everytime I use the methods?
2. In VB, I can refer to a form control by "formname.controlname", how can I do this in C#?  Can I refer to the control inside a static method?

Thanks.

Ken
0
Comment
Question by:kennon2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 4

Expert Comment

by:wile_e_coyote
ID: 7180990
Regarding #1, you can
1. Create a class containing static methods (as you suggested)
2. Create a singleton class (a class which is instantiated at most 1 time)

Regarding #2, you need to instantiate an object from your form class, then you can refer to any Public controls that are defined on the form.  Here is some sample code:

class myForm
{
  private System.Windows.Forms.Label myLabel;

...
}

...
myForm x = new myForm();
MessageBox.Show (x.myLabel.Text);
0
 
LVL 4

Expert Comment

by:wile_e_coyote
ID: 7180996
ooops, that sample code should have defined the Label as PUBLIC, not private, ie:

 public System.Windows.Forms.Label myLabel;
0
 

Expert Comment

by:Aibras
ID: 7181400
Q1)
you can use class with static method like
(Console.WriteLine) method which can be called at
any region in your code.

Q2)
you can use (this) keyword.
look to the (changeButtonName) method in this code

---------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{

     public class Form1 : System.Windows.Forms.Form
     {
          private System.Windows.Forms.Button button1;

          private System.ComponentModel.Container components = null;

          public Form1()
          {

               InitializeComponent();
               changeButtonName("experts-exchange");


          }
          private void changeButtonName(string str)
          {
               this.button1.Text = str ;
          }


          protected override void Dispose( bool disposing )
          {
               if( disposing )
               {
                    if (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.button1 = new System.Windows.Forms.Button();
               this.SuspendLayout();
               //
               // button1
               //
               this.button1.Location = new System.Drawing.Point(120, 216);
               this.button1.Name = "button1";
               this.button1.TabIndex = 0;
               this.button1.Text = "button1";
               //
               // Form1
               //
               this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
               this.ClientSize = new System.Drawing.Size(292, 266);
               this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                            this.button1});
               this.Name = "Form1";
               this.Text = "Form1";
               this.ResumeLayout(false);

          }
          #endregion

          public static void Main()
          {
               Application.Run(new Form1());
          }
     }
}

---------------
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kennon2000
ID: 7181511
Dear all,

Please don't misunderstand my question 2.  What I want is to refer to a control from method or static method in another class.
Say, I have a class with two methods that I want to share them to all other class:

public class Share
{
 public static void ShowStatus(String Message)
  {
    FormMain Form1=new FormMain();
    Form1.statusBarMain.Panels[0].Text=Message;
  }

 public static void ShowError(String ErrorDescription)
  {
    MessageBox.Show(ErrorDescription,"Error");
  }
}

I use this two methods as "ShowStatus("Hello");" in class FormMain, but I get compile error saying that ShowStatus does not exist in class FormMain.  FormMain is the starting form of my application.

I know that is because status method initialized before creation of any form's instant.  But how to make this simple static methods work on other forms?

Ken
0
 

Author Comment

by:kennon2000
ID: 7181525
Dear all,

The compile error erased if I use Share.ShowStatus("Hello"); in class FormMain.  But another problem come:

FormMain Form1=new FormMain(); create another instant of FormMain and shows message in the new instant, how can I, in a static method, show message on the FormMain without creating new instant?

Thanks,

Ken
0
 

Author Comment

by:kennon2000
ID: 7183077
Please don't locked my question by proposing answer.
0
 
LVL 4

Accepted Solution

by:
wile_e_coyote earned 400 total points
ID: 7184989
One way around your problem is to define a global variable which holds a reference to your form AND a public method in the form class to display the message, ie:

public class Share
{
  public static FormMain gFrm = null;

public static void ShowError(String ErrorDescription)
 {
   gFrm.ShowStatus (ErrorDescription);
 }
}

public class FormMain : System.Windows.Forms.Form
{
...
   public void ShowStatus (msg)
   {
       this.statusBarMain.Panels[0].Text=Message;
   }

   public FormMain ()
   {
      gFrm = this;
   }
}

0
 

Author Comment

by:kennon2000
ID: 7185685
Very good method!
Thanks.
I have another C# method about using windows authentication in Window form client application.  Please help if you know the answer.

Thanks again.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question