Problem changing values in textbox - c#

Simplifying
I have a text box and a button
The button just create an messagebox with the text from the textbox.
But i change the value of the textbox, the new value apears (Ex: Type 123) but the message box does not show the value.
If i try to use the value in the programming (get the value by textbox1.text) the variable has nothing ( textbox1.text = "") but i can still see what i typed in the form.
Anyone has any clue?

Your button's click event handler should look something like this
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show(textBox.Text);
}
I suspect you already have code similar to this and that at some point the textbox is cleared or otherwise set to String.Emppty but without seeing actual code it is difficult to help you

When/where did you check the value of textBox1.Text? If you're checking it in the constructor, Form1_Load, or anything else that occurs before you'll have typed text, you will get an empty value.
To properly check the value of textBox1.Text, you should set what's called a breakpoint on the line that calls MessageBox.Show(textBox1.Text). To do this, click in the grey area of the source editor (it's on the far left) on the line containing MessageBox.Show(..). A red circle will appear and your code should be highlighted. When you run your application and click on your button, your application should pause and Visual Studio will highlight that line and from here you can hover over "textBox1.Text" in the MessageBox.Show() line and it should show you the current value.
If your application is as simple as a form, a textbox, and your button1_Clicked event handling code, this should work no problem. If it is not this simple, then you need to look for anything that sets the value of the textBox in your code and make sure it isn't passing any blank values by using breakpoints.
To solve this properly, though, we really need more information.

Thanks Eric and Crippledsmurf. As both of you said, its hard to help without the code.
The problem I found is that when calling the form, I send some objects by reference, so I can track them down and I found that when (don't ask me why it happens that way, I'm still working on it) the construtor is called he make an new component, so the component in the interface no longer represents the one pointed by the variable "textbox1" (Yes Crash893, I haven't mispelled the name).
I found that I was making some mess with the references, and probably that was causing the problem. I fixed the problem by changing the actions performed by references for delegates and events, but I couldn't track down the exactly source of the problem.
Thanks, again, everyone for the insights.

Related

Object reference not set to an instance of an object, using radio button

Code in C#
Binary.frm_binaryQuesiton1.radioCheck.Checked = true;
So here's the problem, I'm creating an application which allow user to check any radio button they want and move to the next question, but once they go back, the selection will disappear, are there any way of storing which radio button did user click, and it will autoclick once the form is loaded? (like what I was trying to do in the code)
the code you show is correct. Except it needs to be
Binary.frm_binaryQuesiton1.radioCheck.Checked = <saved value>;
And you need to store the saved value in a global somewhere
this error occurs when your object contains null refernce .. your object doesnot pointing any thing ,,please elaborate your question or post your code to get appropriate answer !!

Real Time Output Of Processed Inputs

Being a beginner in C# I am having problem in a specific implementation. I need to compute two data in real time by which I mean ki the output shows as soon as the inputs are provided with no click of button necessary.
- For example I have a Text Box where if a type a number 5 gets added to it and the output shows in a Label. The label automatically updates when more numbers are typed in real time.
How do I achieve this?
Thanks !
Before explaining the specifics, it's important to point out that from the code's perspective there isn't much difference between clicking a button or responding to the TextBox's event. Here's what I mean.
In addition to calling code procedurally from within your own methods like this:
void MyMethod(argument)
{
...Other Code...
DoSomething(argument);
...Other Code...
}
.Net also allows you to attach method calls to Events. Events are just references to delegates(I'll leave it up to you to research delegates), but they allow you to asynchronously execute your code based on external interaction.
In your question you say that you want to perform a calculation without making the user click a button. Before going into how you'll accomplish that, lets think about what you'd do if there were a button. Chances are you'll drag & drop a button onto the designer surface, then double click it. Behind the scenes you'll suddenly have a method that looks something like this:
void button_ButonClick(object sender, ClickEventArgs args)
{
DoSomething();
}
So you'll go and populate the new method's body with your calculation logic. Under the hood you've actually just had the designer hook that new method up to the Button's click event. So in your case, whether you're adding the calculation logic to a button click or the TextBox's TextChanged event, you're actually doing almost the same thing.
Just for reference, here's the MSDN documentation for TextBox's TextChanged event .
OK I will give it a shot :)
Assume two textboxes and a label on a form.
Each textbox has a text_changed event handler, i.e. if you type something in either text box, the event handler code is called and there you can access the text of each textbox and transform the text into two numbers.
Then you compute the 2 numbers as per your rules and the result is displayed in the label.
This is a very simplified explanation! There must be validation of the inputs in the textboxes to ensure the data format is correct.
Ask more questions if this is not clear enough.

AcceptsReturn on TextBox not functioning correctly

I have a simple search field on a form that is set as multiline (which I understand is the only way to change a text box's height) and has the flag AcceptsReturn set to false.
However, when I press enter within that control, instead of it activating the default button as it should, it puts in a return character.
Now, I've also attempted using the KeyPress event to check if the Enter key has been pressed to activate the search-button click function in the hope that it would override this return behaviour - but it hasn't. Now it just runs the search AND inserts a return character.
I'm running Visual Studio 2010 (although this problem seemed to be present in 2008 too before I converted it) and C# .NET 2.0. Any solutions?
I see that an answer has already been posted, which mentions the AcceptButton property, but I figure I would state more clearly why that's necessary: quoth MSDN, on AcceptsReturn, "If there is no default button for the form, the ENTER key will always create a new line of text in the control, regardless of the value of this property." (I just tried it out on a dummy form - by "default button", they did in fact mean the form's AcceptButton property. With one set, the value of AcceptsReturn made a difference; without one, it had no effect.)
As for KeyPress, while that is obviously not the best way in this case, I have had to use tricks like that in the past - did you remember to set e.Handled to true in the case that you handled the event yourself?
The form has a property called AcceptButton. Is that pointing to the button you are calling the default button?
I just wrote a little test and it seems to work for me.

C# WinForm text change/ exit prompt

Just out of being bored, I decided to start building my own text editor. I have been having trouble with my coding, so my teacher had suggested building smaller programs that I wanted to write to help get me more familiar with the language, and since I couldn't think of anything, I ended up making this text editor.
I've been trying to root through the code on my own as much as possible, but I was wondering how to make the text that appears at the top of the form (beside the icon) reflect the current filename (or "new" or something if there is no file loaded) as well as having the * if the file has been edited.
I would also like to know how to code my exit button to check if the text has been edited before closing, and ask the user to save if it has, as well as having this show up if the user uses the "X" button in the corner, which currently flat out exits the program no matter what.
To change the title (text besides the icon):
Form1.Text = "This is a new title";
where Form1 is the name of you form object
To check if text is saved:
Hold a boolean variable that indicates whether the user saved the text or not.
Use the Form_Closing method to check if this variable is set to true, and do as you wish
More on Form_Closing here
Many questions :)
Let me answer a few of them:
In your own code, you should probably set a "dirty bit". In other words, declare a boolean variable that says whether or not the text changed. "Changed" is something you, the programmer, needs to define. It can mean many different things - you get to decide.
Each Winform "control" has a set of "properties", most of which you can change programmatically (on-the-fly). Your "form" has a "text" property that changes the title. Label, Button and other controls also have their own "text" property you can change at will.
Each Winform control also has a set of "events" you can override. The "Close" event is the easiest way to manage program shutdown - including if somebody pressed the "X" button. This is also a good place to check your "dirty bit", and save the file accordingly.

C# Tooltip not appearing on "Show"

To fullfill a requirement I have to show a tooltip manually for 30 seconds. According to msdn I just have to use the "Show" method.
toolTip.Show(QuestionHelpText, btnHelp, 30000);
But I only get the standard tooltip behavior, meaning that the message appears half a second after my click (only because the mouse pointer is still over the button). I tried some variations like
toolTip.Show(QuestionHelpText, btnHelp);
but still, nothing happens.
Does anybody have an idea why that is?
Thanks
I know a simple workaround
Put a lable (let's name it labelHelp) with empty text near your button
The following code should work
private void btnHelp_Click(object sender, EventArgs e)
{
toolTip.Show(QuestionHelpText, labelHelp, 3000);
}
Where is "toolTip" declared?
MSDN doesn't indicate (on the ToolTip.Show Method documentation) that the Show method is a blocking call, so if you're declaring toolTip in a method and then pretty much straight afterwards exiting the method then toolTip will have fallen out of scope, causing it to not render or disappear.
I think you need to set initialDelay property of tool tip

Categories

Resources