Form is not rendering in Visual Studio 2019 - c#

I've been making a simple decimal to binary/octal/hex converter using a form in Visual Studio 2019, but something strange is happening.
I've created a form in Form1.cs[Design] by just dragging and dropping a couple of text fields , 3 buttons and a couple of labels. It looked fine. Then I clicked on the button and went to Form1.cs to added some programming-logic . When I clicked back to Form1.cs [Design] this is what I saw:
No buttons or textfields visible. If I try to drag and drop a button again this is what I get:
So the buttons I added are there, they're just not rendering?
When I try to Ctrl+F5, there are no errors, it builds but the form looks blank as well.
Here's the code from Form1.cs ,
using System; using System.Windows.Forms;
namespace DecimalConverter {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
long number = long.Parse(textBox1.Text);
string Base = Convert.ToString(number, 2);
textBox2.Text = Base;
}
private void button2_Click(object sender, EventArgs e)
{
long number = long.Parse(textBox1.Text);
string Base = Convert.ToString(number, 8);
textBox2.Text = Base;
}
private void button3_Click(object sender, EventArgs e)
{
long number = long.Parse(textBox1.Text);
string Base = Convert.ToString(number, 16);
textBox2.Text = Base;
}
private void Form1_Load(object sender, EventArgs e)
{
}
} }
The only non-autogenerated code is the one attached to the buttons.
The Form1_Load method is auto-generated and I think the problem is in it? I don't completely understand its function though.
Any help would be appreciated.

Some controls have a same names. Check it

Related

Winform copy button text to textbox using universal method

So this is a fairly straightforward thing, and I am just curious if there is a better way to do it to save lines of code. For class we are making a teletype machine. Basically there is a textbox, and a series of buttons A-Z and 0-9. When you click the button it adds the corresponding letter/number to the textbox. When you click send, it adds the contents of the textbox to a label and resets the textbox. Everything works and it only took a few minutes to build. However there is a mess of redundant lines and I was curious if there is a way to clean up the code with a method.
This is my current code.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + "A";
}
As you can see, it is very simplistic and straight forward. Click A, and "A" gets added to the textbox. However the Text property of the button is also just "A" and I want to know if there is a way to just copy the text property of that button and add it to the textbox string.
Something like this, except with a universal approach where instead of having to specify btn_A it just inherits which button to copy based on the button clicked. That way I can use the same line of code on every button.
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = box_UserInput.Text + btn_A.Text;
}
You can use this which is more universal as the Control class contains the Text property. Also, using the best practice $"".
private void btn_A_Click(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
You can also assign the same event to each button. Create an event, say addControlTextOnClick and assign the same event to each button.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addControlTextOnClick(object sender, EventArgs e)
{
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";
}
}
You can even shorten this more using this C# construct:
private void addControlTextOnClick(object sender, EventArgs e) =>
box_UserInput.Text = $"{box_UserInput.Text}{((Control)sender).Text}";

Double click on DataGridView cell to open a form

I have a form called ListaDeAlunos with a DataGridView on it.
When I double click on any cell, I want to open a form called Alunos for the selected row of the DataGridView on the form ListaDeAlunos, so I can edit the record. I almost got it to work, but the form Alunos does not show the right record when it opens.
Here's what I did so far:
On the source form I created a class called Variables
and in that class I created a public static string called RecordName
class Variables
{
public static string RecordName;
}
On the source form I created a CellDoubleClick event:
private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Form Alunos = new Alunos();
Alunos.MdiParent = this.MdiParent;
Alunos.Show();
Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();
}
On the second form (the one that will open in the DoubleClick event), I have the following code on the Form_Load event:
private void Alunos_Load(object sender, EventArgs e)
{
this.tbl_alunosBindingSource.Filter = string.Format("Nome LIKE '{0}%'", Variables.RecordName);
}
Any ideas on how to fix this? It's almost working!
PROBLEM SOLVED !!!
All I had to do was to put the last line of code on top. Simple.
private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{ Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();
Form Alunos = new Alunos();
Alunos.MdiParent = this.MdiParent;
Alunos.Show();
}

System.FormatException occurred in mscorlib.dll when converting it int32

I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?

How to display multiple images in a single picturebox in c# windows form?

I'm a beginner in developing c# windows form in visual studio and i'm a little bit confused with its programming constructs compared to VB.NET that i'm used to program. I want to know how to display multiple images in a single picturebox because in vb.net you will just import the image then load it from the resources not like in c# that I think the coding is different. Any help would be appreciated.
I would create an ImageList with all the Images you want to show in your PictureBox, add the three Buttons to your Form and change the Picture on Click.
public partial class Form1 : Form
{
private ImageList imagelst;
public Form1()
{
InitializeComponent();
imagelst = new ImageList();
}
private void Form1_Load(object sender, EventArgs e)
{
//pictures from your Harddrive
Image i = new Bitmap("rock.jpg");
imagelst.Images.Add("rock", i);
i = new Bitmap("scissors.jpg");
imagelst.Images.Add("scissors", i);
i = new Bitmap("paper.jpg");
imagelst.Images.Add("paper", i);
}
private void btnRock_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["rock"];
}
private void btnScissors_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["scissors"];
}
private void btnPaper_Click(object sender, EventArgs e)
{
pictureBox1.Image = imagelst.Images["paper"];
}
}
I hope I got what you want to do. If not excuse my bad english and slow-wittedness, please.

saving form attributes

I wrote .net code and want anyone to help me and tell if this is right or not :). I made a form with two checkboxes and two picture boxes and a button and want to save values of the checkbox and the picture box when I close the form and reload these values again after rerun.
The code I wrote:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//this.BackColor = Properties.Settings.Default.UserBackColor;
Properties.Settings.Default.Reload();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
pictureBox1.Image = Image.FromFile("C:\\red.jpg");
Properties.Settings.Default.Upgrade();
}
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
Application.Exit();
}
The use of Upgrade() is incorrect here. It is used to update settings after an application upgrade.
Remove that line and everything should work fine and dandy.
There's a nice short article on CodeProject that explains the use of application settings: http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

Categories