Solved

Two simple questions.

Posted on 2002-07-26
8
224 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

15 Experts available now in Live!

Get 1:1 Help Now