I have a class file cls_PurchaseValidae.cs in which i have written a code like below:
public void txtMfgDate_Validating(TextBox txtMfgDate,ErrorProvider errorProvider1,string datetoValidate)
{
//method for validating the text box.
ValidateMfgDate(txtMfgDate, errorProvider1,datetoValidate);
}
and i m calling this event in the code behind frm_Purchase.cs like this:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,datetoValidate);
}
but i m getting the error:
Error 1 The name 'datetoValidate' does
not exist in the current context
How can i resolve it.
Thanks in advance
I am not sure why do you want to pass around both TextBox txtMfgDate, and string datetoValidate? Shouldn’t datetoValidate be always txtMfgDate.Text? Anyway, you can do:
private void txtMfgDate_Validating(object sender, CancelEventArgs e)
{
objPurchaseValid.txtMfgDate_Validating(txtMfgDate,errorProvider1,txtMfgDate.Text);
}
if that is what you want.
Related
I have the following button1 Click method:
private void button1_Click(object sender, RoutedEventArgs e)
{
string createstringvalue = MyCustomMethod();
}
When the user clicks the button1 it generates a custom string.
I want to use this custom string as an argument in a custom method used by the button2 Click method.
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(createstringvalue);
if (commandExecuted)
{
MessageBox.Show("Selected file was loaded successfully");
}
}
Currently I get an ERROR:
The name createstringvalue does not exist in the current context
I apologize if my question is quite simple. I try to learn C# , so I try different concepts.
Please mark the question as duplicate, and I will close it, if it is already answered and post in the comments the duplicate SO question.
You can make a variable outside your click event as Loocid mentioned, or you can directly read the value in button2:
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(MyCustomMethod());
if (commandExecuted) //or: if (CustomMethod2(MyCustomMethod()))
{
MessageBox.Show("Selected file was loaded successfully");
}
}
At the top of the class string createstringvalue; Define the variable as
later
private void button1_Click(object sender, RoutedEventArgs e)
{
createstringvalue = MyCustomMethod();
}
I'm writing a Form in c# using Emgu, but, when trying to use the function CvtColor, I get the error "CV_RBG2GRAY" does not exist in the current context. I've looked everywhere, but I couldn't find any other occurences of this problem.
The problem occurs in this line:
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
CvInvoke.CvtColor(Processo.InImage, Processo.BWImage, CV_Rgb2Gray);
}
Could Someone please help me?
That constant is in Emgu.CV.CvEnum.ColorConversion namespace. Try this:
private void startToolStripMenuItem_Click(object sender, EventArgs e)
{
CvInvoke.CvtColor(Processo.InImage, Processo.BWImage, Emgu.CV.CvEnum.ColorConversion.Rgb2Gray);
}
I have 2 buttons, Connect and Send Command. In the first button I am creating the connection to my server and in the second sending a command. The codes are as below:
public void button1_Click(object sender, EventArgs e)
{
SshExec shell = new SshExec("hostname", "username", "password");
shell.Connect();
}
public void button2_Click(object sender, EventArgs e)
{
shell.RunCommand("ls");
}
Now the error is in "shell.RunCommand("ls");" I am getting "The name 'shell' does not contains in the current context". I am new to C# (mere 2 days) and do not have much idea, so please if anyone can help.
What I am expecting is no error and the command to be sent properly to the terminal when the second button is pressed.
At the end sorry for bad formatting of the code, please inform me if any additional information is required.
Right now, you are declaring the variable shell inside the button1_Click method. That means only that method can "see" the variable and use it.
In order for both of the button methods to use the variable, it must be declared outside/above at the class level.
Try this:
private SshExec shell;
public void button1_Click(object sender, EventArgs e)
{
shell = new SshExec("hostname", "username", "password");
shell.Connect();
}
public void button2_Click(object sender, EventArgs e)
{
shell.RunCommand("ls");
}
I'm still new at this so I will try to explain my problem the best I can. English is not my first language so I apologize if I use some terms incorrectly.
I have a 100 line code that is executed every time a button is pressed. My problem is, I have 20 buttons and they all contain the same code (they are only slightly different in means of grabbing info from different source). Is there any way to do this instead of copying the same code too many times.
Basically my code is this:
private void button1_Click(object sender, EventArgs e)
{
//file data source url
sourceUrl = ("www.myurl.com")
//Grab data
code
code
code
//Store data
code
code
code
//Write data
code
code
code
}
Every button has the same code except for the "sourceUrl" part. If I want to add more buttons I have to copy>paste the whole code and my application is starting to get HUGE. Is there any way to shrink the code by only having the code once, and then calling an action or method every time the button is pressed. So instead of having 100 line code multiple time, I'll have one line code for each button and one 100 line code on the top that will be the source for that one line code.
Thanks in advance
Use the Tag property of your buttons to store the source url string and then set, for every button, the same event handler
private void buttonCommonHandler_Click(object sender, EventArgs e)
{
Button b = sender as Button;
CommonMethod(b.Tag.ToString());
}
private void CommonMethod(string sourceUrl)
{
// Execute the common code here....
}
You could set the common handler and the Tag using the form designer window or you could do that dynamically mimicking the code prepared for you by the designer in the InitializeComponent call
button1.Click += buttonCommonHandler;
button1.Tag = "www.myurl.com";
button2.Click += buttonCommonHandler;
button2.Tag = "www.anotherurl.com";
That's what functions are for. Use this layout:
private void YourFunc(string sourceUrl)
{
//Grab data
code
//Store data
code
//Write data
code
}
Now your buttons' event handlers look like this:
private void button1_Click(object sender, EventArgs e)
{
YourFunc("www.myurl.com");
}
private void button2_Click(object sender, EventArgs e)
{
YourFunc("www.myurl2.com");
}
Sure there is a way. Just make the whole function a function that takes the url as a string parameter. And then call that function from your code behind.
private void button1_Click(object sender, EventArgs e)
{
//file data source url
ProcessData("www.myurl.com");
}
private void ProcessData(string sourceUrl)
{
//Grab data
code
code
code
//Store data
code
code
code
//Write data
code
code
code
}
Here we can use use CommandName property, by passing URL in every button's CommandName property and passing it as a parameter to your Common Method to fetch data , So you can create a single function and call it through you btn_Click event .
<asp:Button ID="button1" runat="server" Text="clickMe"
CommandName="put your URL here" OnCommand="button1_Click" />
<%--OnClick="button1_Click" />--%>
See here we can pass your 'URL' in CommandName property ,few things to keep in mind , Here we are using OnCommand event rather than OnClick event of button , so that we can use 'CommandName' property here .1. OnCommand MSDN
2. Commandname MSDN
// private void button1_Click(object sender, EventArgs e)
private void button1_Click(object sender, CommandEventArgs e)
{
string sourceUrl = Convert.tostring(e.CommandName)
// Call function to grab data pass URL as parameter.
GrabDate (sourceUrl ) ;
So now we can get the URL value from button CommandName property .
3 . EventArgs Class
4 .CommandEventArgsClass
now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.