Solved

Two simple questions.

Posted on 2002-07-26
8
230 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to call weather service by using asmx? 4 45
Hey!!!! 1 31
C# Linq - Join two objects into one 3 16
Adding where clause to entity statement c# 1 21
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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