Solved

Two simple questions.

Posted on 2002-07-26
8
227 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

12 Experts available now in Live!

Get 1:1 Help Now