Public function not working properly - c#

I have the following two pieces of code, please take a look at it, I pointed where it is going wrong.
I removed the functions where I call the second window, they do not make sense here.
First, main form, this form calls the second form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace STP_Design
{
public partial class STP2Main : Form
{
public STP2Main()
{
InitializeComponent();
tabControl1.SelectedTab = tabPageDeviceManager;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MenuForm MDIMF = new MenuForm();
MDIMF.MdiParent = this;
MDIMF.StartPosition = FormStartPosition.Manual;
MDIMF.Location = new Point(3, 50);
MDIMF.Show();
tabControl1.Visible = false;
}
public void set()
{
tabControl1.Visible = true; // This statement executes, but does not result in anything I'd expect. The debugger tells me the visibility is false.
tabControl1.BringToFront();
}
}
}
and second form, which I close and should update the first form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace STP_Design
{
public partial class MenuForm : Form
{
public MenuForm()
{
InitializeComponent();
this.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
STP2Main stp = new STP2Main();
stp.set();
this.Close();
}
}
}

You're calling the set method on a new version of the main form, rather than on the version you presumably already have displayed to the user.
What you need to do is get the current main form from the MdiParent property of the menu form, and call the method on that instead.
// In menu form
private void button1_Click(object sender, EventArgs e)
{
var mainForm = this.MdiParent as STP2Main;
if (mainForm != null)
mainForm.set();
this.Close();
}

Related

ShowDialog not opening form Visual Studio

when i try to open a form on visual studio, it doesn't work. i've tried many solutions but it still doesn't work, could the problem be in a different code? here is the code that i am using:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VBR_Application
{
public partial class addNewGT : Form
{
public addNewGT()
{
InitializeComponent();
}
private void addOnline_Click(object sender, EventArgs e)
{
var newForm = new AddOnline();
newForm.Show();
}
}
}
Did you mean?
private void addOnline_Click(object sender, EventArgs e)
{
var newForm = new Form();
newForm.Show();
}
If you want show a built in dialog you can use this one
MessageBox.Show(this, "Title", "Message");
If you want a custom dialog you can also use this one
_FormDialog dialog = new _FormDialog(this);
dialog.ShowDialog();
_FormDialog is the widows form you have created.

LostFocus txtBox.text changed by a button

what I'm trying to do is to change the text of the "last" lost focussed TxtBox by using a button. I know about the LostFocus method, but what are the actual paramaters that we have to give to that fuction?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.ActiveControl = textBox2;
}
private Control _focusedControl;
private void button1_Click(object sender, EventArgs e)
{
SubmitData();
}
private void SubmitData()
{
if (_focusedControl != null)
{
_focusedControl.Text = "hi";
}
}
private void TextBox2_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("Got focus.");
_focusedControl = (Control)sender;
}
}
}
So, what is happening is, no messagebox is popping and the _focusedCOntrol varable doesn't contain the last focus.

Opacity CheckBox(VisualStudio) don't work

There's no code problem, the debug doesn't have any problem but when I test, when I checked the checkbox the opacity doesn't change. Nothing happen. I'm using VisualStudio 2013 Express. Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TP3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int carac = textBox1.Text.Length;
label2.Text = carac.ToString();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Form Form1;
Form1 = new Form1();
if(checkBox1.Checked == true)
{
Form1.Opacity = 1;
}
}
}
}
The code doesn't seem to do anything right. You are creating a new instance of Form in the checkBox1_CheckedChanged method (why do you create a new form?), you set the Opacity property on the new form, but you do not show the form any way. You need to call Show() / ShowDialog() on the Form1 to show it.
If you wish to change the opacity of the current form you can do it this way:
this.Opacity = 1;
And a call like without this would work as well:
Opacity = 1;

Text between two forms with color in C#

I have 2 forms with a main window and a second window. The main window (Form1) shall get text from the second window (Form2)
My second window (Form2) can write text from form 2 to form 1. In the class I can choose color for my text but my problem is that when I push the button who shall send the text it just comes the text without the color I choose so its just black text in Form1 when I send example yellow.
I'm not a C# expert since I'm pretty new at this. I'm sure its a pretty simple problem to fix but for me its not so easy.
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace tester
{
public partial class Form1 : Form
{
public string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 NewForm2 = new Form2(this);
NewForm2.Show();
}
internal void populate()
{
richTextBox1.Text = text;
}
}
Form2.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Tester
{
public partial class Form2 : Form
{
Form1 texting;
public Form2(Form1 iForm)
{
texting = iForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
texting.text = richTextBox1.Text;
texting.populate();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( MyColorDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.ForeColor = MyColorDialog.Color;
}
}
}
Just as you are making public string text in your Form1, you could make a public Color rictTextBoxColor property. Then setting it as well, and referring to it in your populate method

How to make axMozillaBrowser control wait berofe it navigates another web-page

I am creating a simple web-browser reloader program. In my program I am using these controls
axMozillaBrowser,
Button,
progressBar,
TextBox
I am trying to load a web page "5" times. Below is my code that works when i use webBrowser control (an instance of internet explorer)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
while (progressBar1.Value !=5 )
{
webBrowser1.Navigate(textBox1.Text);
while(webBrowser1.ReadyState != webBrowserRedyState.Complete)
{
if (webBrowser1.ReadyState == webBrowserRedyState.Complete)
{
progressBar1.Value =+ 1;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Maximum = 5;
textBox1.Text = "www.google.com";
}
}
}
This is the code that is not working when I use axMozillaBrowser control (an instance of Mozilla Browser). This doesn't load the web page and it's waiting cursor is just blinking.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
while (progressBar1.Value !=5 )
{
axMozillaBrowser1.Navigate(textBox1.Text);
while(axMozillaBrowser1.ReadyState != MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
if (axMozillaBrowser1.ReadyState == MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
progressBar1.Value =+ 1;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Maximum = 5;
textBox1.Text = "www.google.com";
}
}
}
Does your browser control have Navigated (or something like that) event?
As example, you can use it into simple logic cycle, where your control navigates to first URL in collection, then, when navigation completed, navigate to second URL and next.
upd: sorry, I read it again and can't understand now.
upd2: but I suggest you to find Navigated and Navigating events and NavigationState property.

Categories