Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, December 13, 2008 2:12 PM
Hi,
I have a button which uses open file dialog and I take txt file with it. And also I have a next button.
I want that if button not clicked or txt file not taken my program gives messagebox and says you have to click button and select the txt file.
Actually I want to know how I have to write the condition for it.
Like
if (What I have to write here?)
Thanks
Atakan
All replies (13)
Saturday, December 13, 2008 8:06 PM âś…Answered
You would do something like this:
public
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool button1_clicked = false;
private void button1_Click(object sender, EventArgs e)
{
button1_clicked = true;
}
private void button2_Click(object sender, EventArgs e)
{
if (button1_clicked)
{
MessageBox.Show("Thank you for clicking button1");
}
else
{
MessageBox.Show("Please click button1 first.");
}
}
}
However, I would suggest a different way of approaching the problem. If you don't want the user to click button2 before clicking button1, then a better way to handle this would be through UI cues. In other words, disable the button. Set button2 to be disabled using the designer, and then enable it when the user clicks Button1. It would look like this:
public
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you for clicking button1");
}
}
The user will understand, when they see that Button2 is grayed out, that they shouldn't click on it. When Button1 is clicked, and Button2 becomes available, it will invite the user to click the button. It's always better to simply disallow the user certain actions by indicating to them that the intended use of a UI element is to not use it, than to leave the element available and scold the user when they do. It also makes for cleaner code.
David Morton - http://blog.davemorton.net/
Saturday, December 13, 2008 2:28 PM
Not too sure what you are asking, or where the Next button fits it. There is no such button on the standard dialog. The dialog can return a DialogResult value that tells you what the user did.
void MethodA() |
{ |
OpenFileDialog openFileDialog = new OpenFileDialog(); |
DialogResult userResult = openFileDialog.ShowDialog(); |
} |
Rudedog =8^DMark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 2:35 PM
I think I couldn't explain well... Let me explain again.
I must get some values from txt file for my calculation after and then go to next step. I did it. To get the values I have button1 which use openfilediolog.
And I have another button2 go to next step.
I want that if my first button not clicked it gives me message telling me you didn't use opendialog file.
for example if (button1notclicked)
{
messegebox.show("you didn't click the button1");
return;
}
what should I write for red coloured?
Thanks
Atakan
Saturday, December 13, 2008 3:28 PM
This might get you started.
Button startOpenFileDialog = new Button(); |
Button nextStep = new Button(); |
void MethodA() |
{ |
OpenFileDialog openFileDialog = new OpenFileDialog(); |
DialogResult userResult = openFileDialog.ShowDialog(); |
if (userResult = DialogResult.OK) |
{ |
// get the file stuff; |
nextStep.Enabled = true; |
} |
} |
void Form1_Load(object sender, EventArgs e) |
{ |
nextStep.Enabled = false; |
} |
Rudedog =|^DMark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 3:42 PM
Hi Rudedog2,
Yes it is something like that. can't I do it only for button? I mean if button1 not clicked give a messagebox.
Thanks.
Saturday, December 13, 2008 5:22 PM
AtakanSever said:
Hi Rudedog2,
Yes it is something like that. can't I do it only for button? I mean if button1 not clicked give a messagebox.
Thanks.
My code will disable the button untl the user successfully selects and opens a file. No message needed because the button is disabled and will not work. To display a message with the button always enabled you need a means to store the state, which means create a bool field.
bool readyForNextStep = false;
void nextStep_Click(object sender, EventArgs e) |
{ |
if (!readyForNextStep) |
{ |
MessageBox.Show("Is this homework?"); |
} |
return; |
} |
So when do you set the field readyForNextStep = true?
Mark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 5:22 PM
AtakanSever said:
Hi Rudedog2,
Yes it is something like that. can't I do it only for button? I mean if button1 not clicked give a messagebox.
Thanks.
Have a nice day.Mark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 5:26 PM
Hi rudedog2,
As I'm beginner I do not understand everything.
That is why I have asked you only for button.
Anyway I will try to what you said.
Thanks
Atakan
Saturday, December 13, 2008 5:36 PM
AtakanSever said:
Hi rudedog2,
As I'm beginner I do not understand everything.
That is why I have asked you only for button.
Anyway I will try to what you said.
Thanks
Atakan
It was readily apparent that you had not tried anything yet. I apologize for my assumption that this is homework, but when a problem MUST be solved in specific way it usually is homework.
"...only for a button..."? That is what I posted. I called it "nextStep". The trick is that you must create an event handler for the button. Try double clicking on it in the Form Designer.
=8^DMark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 6:50 PM
No I have tried your first code but now I have 20 pages code in my program and when tried your and clicked next openfiledialog opened and then it stoped like I want. But I didn't want that it opened. I tried to stop it but couldn't do it. then I asked about only button as I thought it will be easier for me as I don't want to change anything with my code because I did it in 3 days.
Also it is kind of homework. My project of one of my lesson.
Like I said you I'm beginner with c# as I was using fortran before. So it is really different. So it might be normal that some code I can not understand it as soon as I look at it.
Thanks anyway.
Atakan
Saturday, December 13, 2008 7:10 PM
AtakanSever said:
No I have tried your first code but now I have 20 pages code in my program and when tried your and clicked next openfiledialog opened and then it stoped like I want. But I didn't want that it opened. I tried to stop it but couldn't do it. then I asked about only button as I thought it will be easier for me as I don't want to change anything with my code because I did it in 3 days.
Also it is kind of homework. My project of one of my lesson.
Like I said you I'm beginner with c# as I was using fortran before. So it is really different. So it might be normal that some code I can not understand it as soon as I look at it.
Thanks anyway.
Atakan
Never be afraid to re-write "working" code, especially if it is the initial effort to get something working. Great code is rarely simply written out on the first shot. It tends to evolve into great code.
The sample I initially posted disables the "Next" button until the user successfully goes through the OpenFileDialog and completes the operation properly.
Started off with FORmula TRANslator, huh? I feel your pain. That is procedure driven language, not object oriented in the least. All I can say is stay in the batter's box and keep swinging until you can wrap your head around the concept of objects. I started off in 1971 with BASIC on a PDP-8 by DEC and an ASR-33 teletype. Can you say paper tape?
Look at objects as subroutines with multiple entry points, but it goes far beyond that because objects themselves can become entry points and even replace one another at runtime to provide different behaviors.
Rudedog =8^DMark the best replies as answers. "Fooling computers since 1971."
Saturday, December 13, 2008 7:45 PM
This conversation will go out off subject.
I just needed help about something and ask you question. Why I'm asking question? Because I don't know it well. So it is difficult to use something that you never see before. Or even difficult to understand it by first looking.
That is why I just asked if there is something easier and that is all. maybe it is easy for you but for me it was confusing. Like I said you
Button startOpenFileDialog = new Button(); |
Button nextStep = new Button(); |
it is the first time I see it. How can I directly understand what it is?
Anyway I don't want to talk about it more.
I ask you in these way now. So maybe if you know the answer you can help me.
Suppose I have a button1 and I don't use openfiledialog in it.
And I have button2. (I mean I have only these 2 button and nothing else in my program as an object and code)
Now I want that when I click button 2 it checks that if button1 clicked before. Then let goes forward if button1 clicked and stop the program if button1 not clicked before.
Thanks
Atakan
Saturday, December 13, 2008 9:03 PM
Thanks David M Morto,
That was all I asked for. with a little explanation like you did I understood everything.
Thanks again.
Atakan