Solved

Two simple questions.

Posted on 2002-07-26
8
220 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
  • 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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 100 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now