saving form attributes - c#

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

Related

Form is not rendering in Visual Studio 2019

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

C# open a Form and close it

I want to show a Form called TTT so I tried this:
public static TTT ttt_local = new TTT();
private void button1_Click(object sender, EventArgs e)
{
ttt_local.Show();
}
Then I want to close the Form from inside so ttt_local closes itself when a button in ttt_local is pressed. That works but if I want to reopen ttt_local I get an ObjectDisposedException. Can someone help me please?
You should not need to let a form close itself, however you can set its visibility or simply hide it (the same also applies for showing a form):
Consumer-code:
var ttt = new TTT();
ttt.Show();
TTT-class:
public class TTT : Form
{
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
}
}
Now call ttt.Show() again within your consumer-code, not the form-class itself.
Alternativly you may set the visibility of the form using Form.Visibility.
You have two options.
Use instance variable instead of class variable and let the things work as it is now.
Don't dispose the form, just live with Show/Hide options.
.
Option 1:
public TTT ttt_local = new TTT();
private void button1_Click(object sender, EventArgs e)
{
if(ttt_local == null) ttt_local = new TTT();
ttt_local.Show();
}
Option 2:
Don't close the form, just play with hide/show or even set the Visible property.
What is the reason of using a global variable? You should only put the variable definition inside the event function:
private void button1_Click(object sender, EventArgs e)
{
TTT ttt_local = new TTT();
ttt_local.Show();
}
Whenever the event is triggered, the variable creates and then it disposes with closing the form.
You can show it and then hide it like that:
ttt_local.Show();
ttt_local.Hide();
or close:
ttt_local.Close();
Regards.

Windows form opening other forms

What I want
I am creating an application which has two functionalities. Both functionalities have their own form (called FactuurForm and VerhuurForm). I have another Form called Home, which has, among others, two buttons. Depending on which button is clicked, I wish to open one of the two forms, and complete close the Home-form.
What I have
Currently, I have the following code:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Home home = new Home();
home.ShowDialog();
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
home.Close();
factuur.ShowDialog();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
home.Close();
verhuur.ShowDialog();
}
}
}
kiesFactuur and kiesVerhuur are booleans which in my Home class, initialized as false. As soon as I click on of the buttons, the corresponding boolean will flip to true, triggering the if-statements to close the home-form and open the new form.
My question
Altough my current codes works, it seems a bit much for such a simple functionality. I feel like I wouldn't need the booleans and this go all be done easier. So is there an easier/better way to do this?
I've also thought about creating multiple Main functions. Clicking a button would activate the corresponding new Main function and terminate the current Main. Is this even possible and if so, is it a good solution?
I don't exactly understand the need to completely close the home form. I'd just place 2 eventhandlers for each of the buttons and call the following code on them. The first form will be hidden and closed when you close your subform.
private void ShowSubDialog(Form form)
{
this.Hide(); //makes your main form invisible before showing the subform
form.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
ShowSubDialog(new FactuurForm());
Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
ShowSubDialog(new VerhuurForm());
Dispose();
}
private void Factuur_Click(object sender, EventArgs e) {
LoadForm(new FactuurForm());
}
private void Verhuur_Click(object sender, EventArgs e) {
LoadForm(new VerhuurForm());
}
private void LoadForm(Form f) {
this.Hide();
f.ShowDialog();
this.Show();
}
Add this to your Home form, remove everything after home.ShowDialog() from Main, and make Facturr_Click and Verhurr_Click handle their respective button's click events. This will allow Home to hide/show automatically.
You should replace your code like this :
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
factuur.Show();
this.Hide();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
verhuur .Show();
this.Hide();
}
In the VerhuurForm and FactuurForm you may ovveride the event of closure like this :
public VerhuurForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(VerhuurForm_FormClosed);
}
void FormClosedEventHandler(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
To be sure that your application is closed if you close the form because the Home still active but hidden.

How to show a windows form as a Dialog into VSTO word addin?

I have a VSTO Project (WinWord addin) using c#.
The Project has only one window, it must be showed when click on some button, the problem is that it only happens when i'm debuging, if i try to use it after run the installation, it doesn't show the window. Here is my ribbon code:
public partial class MyRibbon
{
private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnPublicar_Click(object sender, RibbonControlEventArgs e)
{
MyForm form = new MyForm();
//form.TopLevel = true;
form.ShowDialog();
//form.Show();
}
private void gallery1_Click(object sender, RibbonControlEventArgs e)
{
}
private void editBox1_TextChanged(object sender, RibbonControlEventArgs e)
{
}
}
The commented code are some tries. Any help tks.
After very deep debug, found my addin were throwing an non handeled exception (some XML files needed by addin are lost. They weren't where they should), the extrange thing is that Word doesn't show the problem, simply doesn´t open the form.

Making two boxes equal each other

I am trying to make a text box (UPC_txtBox4) self populate to equal the same value of UPC_txtBox2. The two text boxes are on separate forms but I feel there should be a way to link the two.
If form1 is responsible for navigating to form2, then you can pass the value on the query string from form1 using a URL similar to the following:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
Response.Redirect(Request.ApplicationPath + "/Form2.aspx?upc=" + UPC_txtBox2.Text, false);
}
}
then in form2 code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Assuming this field is an asp.net textbox and not an HTML input
UPC_txtBox4.Text = Request.QueryString["upc"];
}
}
Alternatively, you could store the value in session state, assuming that you are using sessions.
CORRECTION: Seeing as you are using WebForms, not WinForms as I had assumed, the below is irrelevant. I'll leave it just incase it helps someone else.
You should just create a method on the form that needs to be updated, then pass a reference when of that form to the newly created form.
This won't work if either form is a dialog (as far as I know).
So:
Form that has the textbox that will be directly edited.
private Form formToUpdate;
public void OpenForm(Form _formToUpdate)
{
formToUpdate = _formToUpdate;
txtBlah.TextChanged += new EventHandler(OnTextChanged);
this.Show();
}
private void OnTextChanged(object sender, EventArgs e)
{
formToUpdate.UpdateText(txtBlah.Text);
}
Form that is to be dynamically updated:
delegate void StringParameterDelegate (string value);
public void UpdateText(string textToUpdate)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(UpdateText), new object[]{textToUpdate});
return;
}
// Must be on the UI thread if we've got this far
txtblah2.Text = textToUpdate;
}
Note: this is untested (although it should work), and largely pseudo code, you'll need to tailor it to your solution obviously.

Categories