0

I want to hide a button when the DateTime.Now = a input date by user. The textbox100 is in the Form2 and is already public, but I know something else is missing, because I get the error: "The name 'textBox100' does not exist in the current context."

Thank you.

public void Form1_Load(object sender, EventArgs e)
{
    var dateTimeStr = textBox100.Text;
    var user_time = DateTime.Parse(dateTimeStr);
    var time_now = DateTime.Now;

    if (time_now >= user_time)
    {
        button1.Visible = false;
    }
}
MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • This isn't JavaScript: why are you using `var` in every line? It makes your code rather unreadable. – Daniel Aug 13 '12 at 21:28
  • Because the type on the right side is obvious, isn't it? – Tim Schmelter Aug 13 '12 at 21:37
  • @TimSchmelter Well, yes, but in C# (As opposed to JavaScript, where programmers are adapted to reading code like this) I'm used to reading the type of the variable, the identifier, and then the initializing value. Having to figure out the type for myself isn't difficult, but it does slow me down unnecessarily. It also makes the code more confusing and less maintainable. – Daniel Aug 13 '12 at 21:40
  • Dont worry about that guys, im very new to C#. – Joao Carlos Rafael Aug 13 '12 at 21:42
  • @Daniel: But you don't want to discuss subjective things, do you? http://stackoverflow.com/questions/236878/what-to-use-var-or-object-name-type – Tim Schmelter Aug 13 '12 at 21:47
  • @TimSchmelter I don't, the intention was never to get into a debate; but as a new C# developer, he shouldn't be getting into bad habits. And I think that this is truly a bad habit, I've never met a C# programmer who condoned using only `var`. – Daniel Aug 13 '12 at 21:49
  • I appreciate you trying to correct me, but i asked a question and you didn't even answered me. Instead of answering, you asked me why im using var. – Joao Carlos Rafael Aug 13 '12 at 21:58
  • 1
    @JoaoCarlosRafael: Actually [he has answered](http://stackoverflow.com/a/11942808/284240) your question ;) – Tim Schmelter Aug 13 '12 at 22:08
  • @JoaoCarlosRafael Comments allow us to discuss problems that aren't directly related to the question being asked, I was just trying to help you and offer you advice so that you wouldn't fall into a bad habit. Also, I did answer you, look down :). – Daniel Aug 13 '12 at 22:09

3 Answers3

2

You need to improve your communication between the forms. See the accepted answer in this question.

Adapted to your code:

using ( var form = new Form2() )
{
    var dateTimeStr = form.textBox100.Text;
    var user_time = DateTime.Parse(dateTimeStr);
    var time_now = DateTime.Now;

    if (time_now >= user_time)
    {
        button1.Visible = false;
    }
}

If you need to wait before taking the value of the TextBox, that is, wait for the user to type in the input, then you can write:

string dateTimeStr;
using ( var form = new Form2() )
{
    form.submitButton.OnMouseUp += (source, e) =>
    {
        dateTimeStr = form.textBox100.Text;
    };
} 

Assuming you have a submission button somewhere in your form.

Community
  • 1
  • 1
Daniel
  • 2,944
  • 3
  • 22
  • 40
0

even if it's public, it still belongs to the class Form2

var dateTimeStr = Form2.textBox100.Text;
unloco
  • 6,928
  • 2
  • 47
  • 58
0

You cant get the text of textbox100 if Form2 not instantiated an having a reference in Form1. Then use the line from UnLoCo. Of course its has to be public in Form2

elblanko
  • 26
  • 3