No Extension Method for Client Name in C# - c#

Given the code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form1 client;
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!= "" &&textBox4.Text!="")
{
client = new Form1();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
I get the following error message whenever I try to compile:
'WindowsFormsApplication1.Form1' does not contain a definition for
ClientName and no extension method 'ClientName' accepting a first
argument of type.
Do you have any idea on how to fix this?

There is no ClientName property on a Windows Form class. However, since you are inheriting from Form, you can add one. But that doesn't make sense either. Are you sure you want a variable of type Form1 to have properties for ClientName, ServerIP, and a method for Connect()? Much more likely you want either some other pre-existing class or to make your own.
public class ClientService
{
public string ClientName {get; set;}
public string ServerIp {get; set;}
public void Connect()
{
//logic here
}
}
And change your UI logic to
if (!String.IsNullOrEmpty(textBox3.Text) && !String.IsNullOrEmpty(textBox4.Text))
{
var client = new ClientService();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}

This is the documentation for the Form class in .NET: https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
Notice there's no member for ClientName listed. You cannot reference it because it doesn't exist.

Related

How to Access web page TextBox from another class?

I have a web page called Default.aspx and a textbox called textBox1
In the Default.aspx.cs, I can set the text by typing:
TextBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class? so I want to change the text for textBox1 in this class.
So far i tried like this it is working fine in Mymethod but it is not working in Myclass.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Drawing;
using System.Threading;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitEventMethod2(object sender, EventArgs e)
{
this.Mymethod();
}
public void mymethod1()
{
TextBox1.Text = "some text";
}
class Myclass
{
public void mymethod2()
{
TextBox1.Text = "some text";
}
}
}
}
I have a web page called Default.aspx and a textbox called textBox1
In the Default.aspx.cs, I can set the text by typing:
TextBox1.text = "change text";
Now I have created another class. How do I call textBox1 in this class? so I want to change the text for textBox1 in this class.
So far i tried like this it is working fine in Mymethod but it is not working in Myclass.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Drawing;
using System.Threading;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitEventMethod2(object sender, EventArgs e)
{
this.Mymethod();
}
public void mymethod1()
{
Myclass myClass=new Myclass ();
myClass.mymethod2(TextBox1);
}
class Myclass
{
public void mymethod2(TextBox textBox)
{
textBox.Text = "some text";
}
}
}
}
Use Session[] for this.
e.g.
TextBox1.Text="abc";
Session["TextBox_Text"]=TextBox1.Text;
and in other class use this Session[] to assigning text to another TextBox by using,
TextBox2.Text=Session["TextBox_Text"].ToString();
Hope this will help you
Thank You.
You cannot access TextBox Text like this in class. You can access it like this as well:
class Myclass
{
private string _myText;
public string mystring
{
get
{
return _myText;
}
set
{
_myText = value;
}
}
}
public void Mymethod()
{
Myclass obj = new Myclass();
obj.mystring = TextBox1.Text.Trim();
//do what else you want
}
create another class
public class DataAccess
{
private string _value;
public string value
{
get
{
return this._value;
}
set
{
this._value = value.Trim();
}
}
}
use these code in your aspx.cs page and create object DataAccess
class. so you can access textbox value.
public partial class Default : System.Web.UI.Page
{
DataAccess objDaAccess = new DataAccess();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submitEventMethod2(object sender, EventArgs e)
{
this.Mymethod();
}
public void Mymethod()
{
TextBox1.Text = "some text";
objDaAccess.value=TextBox1.Text;
}
class Myclass
{
TextBox1.Text = objDaAccess.value;
}
}

messagebox.show error/ no overload for method 'show' takes 1 arguments

I'm trying to show a MessageBox but i'm getting the error:
no overload for method 'show' takes 1 arguments.
I cant seem to find a solution in any forum (stackoverflow,msdn...) and I have tried everything that has been suggested. What am I missing?
Any help is appreciated.
BTW. I'm new to windows forms and c# in general but I have written this from a tutorial and it should work.
This is the complete code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
using System.IO;
namespace AdvancedClientChat
{
public partial class Form1 : Form
{
Client client;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnConnect_Click(object sender, EventArgs e)
{
if (textBoxIP.Text !="" && textBoxName.Text !="" && textBoxPort.Text !="")
{
client = new Client();
client.ClientName = textBoxName.Text;
client.ServerIp = textBoxIP.Text;
client.ServerPort = textBoxPort.Text;
}
else
{
MessageBox.Show("You must fill all the boxes");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
}
private void MessageBox_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
It looks like you have a control named MessageBox which is causing your problems. Either rename the control, or you will have to specify the MessageBox class with its full namespace, System.Windows.Forms.MessageBox.Show("myMessage");
private void btnConnect_Click(object sender, EventArgs e)
{
if (textBoxIP.Text !="" && textBoxName.Text !="" && textBoxPort.Text !="")
{
client = new Client();
client.ClientName = textBoxName.Text;
client.ServerIp = textBoxIP.Text;
client.ServerPort = textBoxPort.Text;
}
else
{
System.Windows.Forms.MessageBox.Show("You must fill all the boxes");
}
}

An object reference is required for the non-static field, why?

I did some research about this error, and all awnsers i found include removing static from the method or the property, but in my code there isnt any static, so i dont know whats happening, thanks for your help.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class textoTitular : Form
{
public textoTitular()
{
InitializeComponent();
}
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
}
}
Your problem is in
private void textoTitular_Load(object sender, EventArgs e)
{
textoTitular.Text = "testing"; /// prints testing on the textbox
}
You are referencing the form class in a static way.
Rather try using this. Something like
private void textoTitular_Load(object sender, EventArgs e)
{
this.Text = "testing"; /// prints testing on the textbox
}
Added bonus, you can omit the this and use the object property
private void textoTitular_Load(object sender, EventArgs e)
{
Text = "testing"; /// prints testing on the textbox
}

Use object between multiple functions

I am creating a spam email checker. One method scans the email, another adds a known flag to an array of words and phrases to check against; both methods are part of Tester class. Currently I have a button per method, however each event creates its own spam object. How do I get both events to use the same object, allowing the scan to recognize the flag I just added?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HW8_DR
{
public partial class Spam_Scanner : Form
{
public Spam_Scanner()
{
InitializeComponent();
}
private void testButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}
private void addButton_Click(object sender, EventArgs e)
{
Tester scan = new Tester();
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}
}
Move the Tester variable to the class field, like this:
public partial class Spam_Scanner : Form
{
Tester scan;
public Spam_Scanner()
{
InitializeComponent();
scan = new Tester();
}
private void testButton_Click(object sender, EventArgs e)
{
scan.tester(Convert.ToString(emailBox.Text));
this.SpamRatingBox.Text = string.Format("{0:N1}%", Tester.countSpam / Tester.wordCount * 100);
this.WordsBox.Text = Tester.posSpam;
this.OutputPanal.Visible = true;
this.pictureBox1.Visible = false;
}
private void addButton_Click(object sender, EventArgs e)
{
scan.addSpam(Convert.ToString(addFlagBox.Text));
this.addFlagBox.Text = "";
}
}
Variables declared inside of a method (as yours are) have method scope, so they can't be seen by other methods.
Instead, declare the variable in the class scope so that both the class's methods can see it.
public partial class Spam_Scanner : Form
{
private Tester scan;
private void testButton_Click(object sender, EventArgs e)
{
scan = new Tester();
...
}
private void addButton_Click(object sender, EventArgs e)
{
scan.addSpam(Convert.ToString(addFlagBox.Text));
...
}
}
Depending on the order of button clicks, you may want to initialize the variable in the declaration rather than in the testButton_Click method, but thats up to you. The important thing to remember is that scopes can see their own members, and all scopes they are nested in. Thus, methods can see class-scope variables, but not each other's.

C# timer - Getting the 'Method name expected' error

When I try to set the tick event of my timer, and use the method, I get this error. What's going wrong here?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace QueueSimulation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
}
public void goButton_Click(object sender, EventArgs e)
{
ProcessCustomers CustomerQueue = new ProcessCustomers(); // create the CustomerQueue
System.Windows.Forms.Timer queueTimer = new System.Windows.Forms.Timer();
queueTimer.Interval = Convert.ToInt32(customerArriveChooser.Value*1000);
queueTimer.Tick += new ElapsedEventHandler(CustomerQueue.Arrive());
CustomerQueue.Arrive();
}
private void stopButton_Click(object sender, EventArgs e)
{
// put code here to break out of the program
}
}
public class Customer
{
int timeInQueue;
}
public class ProcessCustomers
{
public void Arrive(){}
public void Leave(){}
}
public class Server
{
bool servingStatus = false; // true for serving, false for not serving
}
public class Queue
{
Customer[] queue = new Customer[49]; // initialise a queue (array) capable of holding 50 customers
}
}
I suspect you mean to use the method name, not call it and use the return value:
queueTimer.Tick += new EventHandler(CustomerQueue.Arrive);
Since the return value of Arrive is not a delegate type, you can't use it.
Note that the event handler signature should match the delegate signature - in the case of Tick, it is EventHandler:
public delegate void EventHandler(
Object sender,
EventArgs e
)
So, your Arrive method should take these two parameters:
public void Arrive(Object sender, EventArgs e){}
ElapsedEventHandler is the handle for System.Timer not for System.Windows.Forms.Timer
the event must look like this:
queueTimer.Tick += new ElapsedEventHandler(queueTimer_Tick);
void queueTimer_Tick(object sender, EventArgs e)
{
CustomerQueue.Arrive();
}

Categories

Resources