i write some code in c# for example ;
if(Condition)
{
private void ribbonPanel1_Click(object sender, EventArgs e)
{
Do something ;
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
Do Something ;
}
private void ribbonPanel1_Click(object sender, EventArgs e)
{
Do something ;
}
}
Is this allowed ? can we restrict multiple Methods of control under any condition ? Not only if can we use any Loop?
do this instead
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (condition)
{
Do something ;
}
}
You cannot restrict a specific method. But what you can do is call the method in a specific condition.
public bool Condition;
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (Condition) ThingsYouWantToDo();
}
private void ThingsYouWantToD()
{
//Stuff
}
private void ribbonPanel1_Click(object sender, EventArgs e)
{
if (Condition)
DoFirstStuff();
else if (Condition2)
DoSecondStuff();
else
DoThirdStuff();
}
But wait... are you maybe speaking about conditional compiler directives? Like:
private static void Main()
{
#if DEBUG
Console.WriteLine("You are running debug version!");
#endif
Console.WriteLine("Program is starting!");
}
Related
I would like to call btnSubmit if certain conditions in axTws1_tickPrice are true. How do i do this?
private void btnSubmit_Click(object sender, EventArgs e)
{
//code here
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
Call butSubmit (how do i do this)
}
}
You're better off having a common method that both of your control handlers call, rather than trying to call one handler from another. That way your code is more extensible and testable, and you don't have to worry about the event arguments or senders.
For example:
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
Multiple options.
Option 1 :
Preferred approach, move common logic to another method.
private void btnSubmit_Click(object sender, EventArgs e)
{
CommonLogic();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
CommonLogic();
}
}
private void CommonLogic()
{
// code for common logic
}
Option 2:
Executing PerformClick() method which generates a Click event for a button.
btnSubmit.PerformClick();
Option 3:
Invoke the event method like any other normal method.
btnSubmit_Click(sender, new EventArgs());
just call it with current parameters.
if (Condition)
{
butSubmit(sender, null)
}
Unbelievable, but
btnSubmit_Click(null,null);
Or other arguments if needed.
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
button1_Click(sender, EventArgs.Empty);
}
}
button1_Click is similar to a normal method which accepts two inputs of type object and EventArgs So you can call them by giving the same parameters. if you are not going to use these arguments inside the method then you can call them by passing null,null don't use null if you want to use e or sender inside the method. in such situation call them as like i suggested above.
Thanks Steve and Hari - this worked well
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
private void tabPage3_Click(object sender, EventArgs e)
{
((Control)this.tabPage3).Enabled = false;
}
private void tabPage4_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
((Control)this.tabPage4).Visible = false;
this.tabPage4.Hide();
}
}
private void tabPage1_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabPage1.Hide();
}
}
private void WelcomePage_Load(object sender, EventArgs e)
{
I've done this some time ago. The problem is that there is no property Visible and Enabled is doing not the things you would like to do.
So here is how i'm doing it:
// Put this over the constructor
private TabPage tabPage4ToShowForNotStudents = this.tabPage4;
private TabPage tabPage1ToShowForNotStudents = this.tabPage1;
Then you have to subscribe the Load-Method of your Form:
void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role != "student")
{
yourTabControl.TabPages.Add(this.tabPage4ToShowForNotStudents);
yourTabControl.TabPages.Add(this.tabPage1ToShowForNotStudents);
}
}
Now it will add the TabPage to your TabControl if the Role is not student. If it is it will not be added.
Be sure to not have them added in the designer otherwise it will not work.
Hope this is useful :)
thanks guys.i have sorted out the problem.
private void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabControl1.TabPages.Remove(tabPage5);
}
else
{
tabControl1.TabPages.Remove(tabPage4);
}
}
I have this little piece of code inside a project:
private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click();
}
private void button2_Click(object sender, EventArgs e)
{
x = 0;
}
Now, I want to call the button2_Click method, but I cannot because I have to pass arguments.
The problem is, I don't know what arguments to pass when calling the method.
What should I pass?
use like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
button2_Click(this.button2,EventArgs.Empty);
}
private void button2_Click(object sender, EventArgs e)
{
x = 0;
}
just treat it as normal parameter. but it's not recommand like it. the best Practice is Extract the body of button2_Click and call the Extract method.the button2_Click should alway be trigger by user Interface.for example :
private void textBox1_TextChanged(object sender, EventArgs e)
{
method();
}
private void button2_Click(object sender, EventArgs e)
{
method();
}
public void method(){
//x=0 or other
....
}
Nevermind, I just used:
button2_Click(sender, e)
and it worked.
Let's say I have the following code:
private void btnArr1(object sender, EventArgs e)
{
wr("test string1","st1");
}
private void btnArr2(object sender, EventArgs e)
{
wr("test string2","st2");
}
private void btnArr3(object sender, EventArgs e)
{
wr("test string3","st3");
}
private void wr(String str, String st){
switch(st){
case "st1":{
st1.Add(str);
break;
}
case "st2":{
st2.Add(str);
break;
}
case "st3":{
st3.Add(str);
break;
}
}
}
Is it somehow possible to minimize that code?
For example Change "st1.Add(str)" to something like "st".Add(str).
The only difference between those buttons is the list name where the information is stored. Everything else is equal.
You need to use a Dictionary<String, List<String>>.
In your constructor (or other initializationary area), add a new List<string>() for each key that you need.
You can then write
dict[someKey].Add(something);
Well if these are all in the same class, you can just use:
private void btnArr1(object sender, EventArgs e)
{
st1.Add("test string1");
}
private void btnArr2(object sender, EventArgs e)
{
st2.Add("test string2");
}
private void btnArr3(object sender, EventArgs e)
{
st3.Add("test string3");
}
If that doesn't work for you, please give details as to why it wouldn't work.
Hard to say without knowing the type of st1, st2, etc. but something like this?
private void btnArr1(object sender, EventArgs e)
{
wr("test string1",st1);
}
private void btnArr2(object sender, EventArgs e)
{
wr("test string2",st2);
}
private void btnArr3(object sender, EventArgs e)
{
wr("test string3",st3);
}
private void wr(String str, List<string> st){
st.Add(str);
}
I need to know if it's possible to call the Click of a button from another one.
private void myAction_Click(object sender, EventArgs e)
{
// int x;
// ...
}
private void Go_Click(object sender, EventArgs e)
{
// call the myAction_Click button
}
Thanks.
You want:
private void Go_Click(object sender, EventArgs e)
{
myAction_Click(sender, e);
}
But a better design is to pull the code out:
private void myAction_Click(object sender, EventArgs e)
{
DoSomething();
}
private void Go_Click(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// Your code here
}
The button has a PerformClick method.
http://msdn.microsoft.com/en-us/library/hkkb40tf(v=vs.90).aspx