Calling a class from a button

  • Thread starter Thread starter ReidarT
  • Start date Start date
R

ReidarT

How do I call a public class function in C# from a button
like

private void Button1_Click(...
{
TextFromFile(); ????????????????????
}

public class TextFromFile
{
....
}
regards
reidarT
 
ReidarT said:
How do I call a public class function in C# from a button
like

You can't "call" a class. You can call a method, or create an instance
of a class, and then call a method from that class. Or you could call a
static method. Now, what do you actually want to do?

(I would strongly advise learning the basics of C# before doing GUI
coding, btw. GUI coding has all kinds of added complications which will
be even harder to cope with if you're not confident about the basic
platform.)
 
ReidarT said:
How do I call a public class function in C# from a button
like
private void Button1_Click(...)
{
new TextFromFile();
}

public class TextFromFile
{
// do something
}
 
Oh No!

public void Button1_Click()
{
myClass c = new myClass()
c.MyMethod()
}

public class myClass
{
public void myMethod()
{
// Do Something
}
}

But you should (both) follow Jon Skeet's advice.
 
Marinus Holkema said:

*** Oh Yes! ***

Have fun!



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

namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}

<snipped>

#region Windows Form Designer generated code
<snipped>
#endregion


[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
//******************
new Class1(1);
//******************
}
}
}

using System;
using System.Runtime.InteropServices;
namespace WindowsApplication2
{

public class Class1
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequuency, int duration);

int tone;
public Class1(int tone)
{
this.tone = tone;
execute();
}
void execute()
{
if (tone == 0)
{
Random random = new Random();
for(int i = 0; i < 10000; i++)
{
Beep(random.Next(1000),100);
}
}

if(tone == 1)
{
Beep(800,100);
Beep(900,100);
Beep(800,100);
}
}
}
}
 
Back
Top