I wrote a programme in VB to calculate the efficency of a heat exchanger with the temperature and other variables as user imput in a windows form. In VB is very simple, you just type:
Tcin = Val(Form1.Tempcin.Text);
Thin = Val(Form1.Temphotin.Text);
and the values are stored in Tcin and Thin.
But I have to translate the whole code to C# and I'm facing a lot of problems just to get the values form the text boxes in the windows form (all numerical). I have to use them in several methods as well, not only in the main form. The same happens with RadioButtons and CheckBoxes.
How do I call the variables or the "check status" of the buttons and checkboxes in the namespace and the several methods?
Inside your Form1.cs class, you can call:
Checkbox:
bool checked = this.checkBox1.Checked;
Radiobutton:
bool checked = this.radioButton1.Checked;
Textbox:
string text = this.textBox1.Text; // For text
- or -
int number = this.Int32.Parse(textBox1.Text); // For numbers
// If the textbox contains letters, there will be an error thrown (an exception)
Update the control names accordingly. You may have named your textbox myTextBox instead of textBox1.
This topic has to do with controls in C#. A button, a textbox, a checkbox, a radiobutton, a progressbar, and even a form ... these are all controls. This should help you with terminology if you want to Google further.
Related
I've build an email app in c# winforms with a richtextbox.
Now I want to add a search option, which opens in a new Window to enter the search/replace text.
I know how to do this in one form, but how do i get access to the richtextbox in form1 from the second form?
In short, to have a form return values, one must give it public properties, and have them set by the form when closing.
A more in-depth explanation and examples can be found here
I have a Windows Phone 8 project that converts values (i.e.: Celsius to Fahrenheit). There are two TextBox UI elements, one of which is read-only. The user can change the first TextBox to input the value to be converted. He can also press a button to "swap" the two TextBoxes so that he can do the reverse conversion. When the user presses the button, the value from the second TextBox goes into the first TextBox (and vice versa). But it's not the user who changed the value, it's the code who did.
I asked around (on IRC) and researched the subject, but I am a beginner and couldn't understand most of what I have found.
I heard that a simple solution would be to use Data Bindings. I researched the subject, and from what I read, Data Bindings can't solve my problem (correct me if I'm wrong).
I also tried to create a subclass of TextBox, hoping that I could hook in some custom event to it and go further in that direction. But I did not understand how to link the custom TextBox to the UI (in XAML). The way I created the subclass is to just create a new class and add TextBox as the parent. I know there is a template in VS to create a new User Control, and I tried it, but I couldn't understand what I was doing (or what I was supposed to do).
So I have two questions: Am I looking at the problem from the right angle? If yes, how do I create a custom TextBox and link it to the UI? If not, how could I solve my problem?
If your question is how to distinguish if the text got changed by the user or by the code then its simple.
Assuming that when the user types something you'd like to perform method A but when the code changes the text you'd like to perform method B:
In both cases you will need to override the TextBox.TextChanged() event handler.
You will also need a flag variable to tell you if the swap button was pressed or not.
The event handler should be something like this:
{
if (swap_pushed)
{
Method_B();
swap_pushed = false;
}
else
{
Method_A();
}
}
And finally your event handler for swap Button.Click() should be like this:
{
swap_pushed = true;
}
Please I need help with this issue. I have a dynamic menu with buttons. Each button has name of form(VP,VZPAL,PAL,...).and when I click on some button, I want to activate form with the same name that the button has. I try do it with this code, but its escape with exception:
Value cant be null.
string Formname = "FISpanel.Form" + b.Name.ToUpper().ToString();
Form frm = (Form)Activator.CreateInstance(Type.GetType(Formname));
frm.Show();
Value of Formname is right(FISpanel.FormVP).Any ideas?
This is how I've achieved the same thing in the past. In my application I have 'links' that are set inside of .ini files, but these can be different for each user, so (like you), I needed a solution that would launch forms depending on what was clicked:
LinkLabel lnk = (LinkLabel)sender;
Type type = Type.GetType("Valhalla." + lnk.Tag.ToString());
Form thisFrm = (Form)Activator.CreateInstance(type);
// DISPLAY THE FORM //
thisFrm.ShowDialog();
Also, as #King King has mentioned. The dot is very important. Are your forms actually named: "FormVP" Or just "VP" (etc...) If the later then you will have to call them as: Form.VP and not FormVP.
I have a main form with some buttons, textboxes, labels, etc.
On a second form I would like to copy the text from the main forms textbox onto the second form.
Have tried:
var form = new MainScreen();
TextBox tb= form.Controls["textboxMain"] as TextBox;
textboxSecond.Text = tb.Text;
But it just causes an exception. The main screen textbox is initialised and contains text.
When I hover over form I can see all the controls are there.
What am I doing wrong?
Looking at the original code, there are two potential reasons for the NullReferenceException you are getting. First, tb is not defined in the code you provide so I am not sure what that is.
Secondly, TextBox textbox = form.Controls["textboxMain"] as TextBox can return null if the control is not found or is not a TextBox. Controls, by default, are marked with the private accessor, which leads me to suspect that form.Controls[...] will return null for private members.
While marking the controls as internal will potentially fix this issue, it's really not the best way to tackle this situation and will only lead to poor coding habits in the future. private accessors on controls are perfectly fine.
A better way to share the data between the forms would be with public properties. For example, let's say you have a TextBox on your main screen called usernameTextBox and want to expose it publicly to other forms:
public string Username
{
get { return usernameTextBox.Text; }
set { usernameTextBox.Text = value; }
}
Then all you would have to do in your code is:
var form = new MainForm();
myTextBox.Text = form.Username; // Get the username TextBox value
form.Username = myTextBox.Text; // Set the username TextBox value
The great part about this solution is that you have better control of how data is stored via properties. Your get and set actions can contain logic, set multiple values, perform validation, and various other functionality.
If you are using WPF I would recommend looking up the MVVM pattern as it allows you to do similar with object states.
PhoenixReborn is correct. The problem is that you are creating a new MainScreen, which means that new controls are created, so unless the text in your controls are initialized in the form constructor, they are going to be empty. Usually, the way to handle this is to pass the first form instance to the second form, like this:
SecondForm second = new SecondForm(this);
and in the second form:
public SecondForm (MainForm form)
{
// do something with form, like save it to a property or access it's controls
}
That way, the second form will have access to the first form's controls. You might consider making the properties you need to use public (in the designer properties pane). That way you can just do form.textboxMain.Text.
I have a Class named Testing and a Form called TitleScreen. In TitleScreen I have a textBox1 who's text I would like to be passed to a Class and then pass it back to my Form into a textBox2.
I know how to do only the basics in C# so if you try and make it simple as possible.
In your Class:
public class Class1
{
public static string SeparateName(string fullName)
{
string[] wordsInText = fullName.Split(' ');
return wordsInText[0];
}
}
In your Form:
private void button1_Click(object sender, System.EventArgs e)
{
textBox2.Text = Class1.SeparateName(textBox1.Text);
}
"I highly recommend that you read a book or tutorial that targets new users, otherwise there will be holes in your understanding of the language and the frameworks."
It sounds like you want to perform an operation on the textbox's value and then print the result in another textbox.
You can write a method (function) that accepts an argument of type String and perform the operation in that method. The method can then set the Text property of the textbox to the result.
If you're asking how to input code in a winforms project, you can double-click the background of the form to reach its code. (At least in Visual Studio)
If you don't know how to do the above suggestions, I highly recommend that you read a book or tutorial that targets new users, otherwise there will be holes in your understanding of the language and the frameworks.
I would suggest you want to look at the concept of data binding, whereby you bind the controls on your forms to the properties of the underlying objects (instances of your classes).
Binding removes the need to write code to cross-load the data from the class into the form and back again, instead you can then say "text box 1 is bound to this property of my class". Then, when you update the value of the textbox the data is automatically placed into the chosen property of your class instance. Typically you then have a save button that calls a save method on your class to persist the data to your data store (database or whatever).
It is perfectly reasonable to bind more than one control on your form to the same property on your underlying class, so in your example you can bind both textBox1 and textBox2 to the same property on your class. Then, once you've implemented databinding, when you change the value in textBox1, the value will automatically be reflected in textBox2, either on each keystroke or when the field is validated (typically when you move focus to another control).
This is the microsoft documentation on Winforms binding which covers everything you need: https://msdn.microsoft.com/en-us/library/ef2xyb33(v=vs.110).aspx