Basically trying to learn a few things I havn't tried yet. I want to make a small program that has a Textbox and Button.
Whats typed into the textbox field will be added to the end of a url applied from the button.
http://website.com/stuff?things= +textboxValue
After you click the launch button, I want the url created with the entered text, to open in a second form.
I have everything working except for the text from form1 to carry over to form2. Just wondering how I can go about this.
Form 1
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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.LinkTarget = textBox1.Text;
form2.Show();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form3 newFrm = new Form3();
newFrm.Show();
}
private void Form1_Load_1(object sender, EventArgs e)
{
}
}
}
Form2
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 WindowsFormsApplication8
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string LinkTarget {
get;
set;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string myUrl = "http://website.com/stuff?things=" + LinkTarget;
}
}
}
Example of what I am going for.
Form one has text box and button.
when you click the button it will open http://www.stackoverflow.com/questions/ in the second form. the second form is basically a browser.
but, in form one if I type 41090977 before I hit the button, it will open http://www.stackoverflow.com/questions/41090977
You can create a property in form2, which you set before you open it:
//... other form2 code
public string LinkTarget {
get;
set;
}
You can set the value like this (in button1_Click):
Form2 form2 = new Form2();
form2.LinkTarget = myTextBox.Text;
form2.Show();
Within form2 you can use the value of the property to create your link. In your current code you have just defined the property. You have to use its content to create your link. This is done by attaching the property value to your string containing the URL with the + operator:
string myUrl = "http://website.com/stuff?things=" + LinkTarget;
Related
I am using C# windows forms and I'm having a problem. I have a form which I want to change the background of, however, I want to do so from a second form. The second for has a button which when pressed, the background of the first form changes. Here is my code
First Form:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Second Form:
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
}
}
The button is supposed to turn the first form transparent. This however, does not work. Am I missing something? Thank you!
You can set the Form 2's owner to Form1. Then access it's properties that way.
Form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog(this);
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
frm1.TransparencyKey = Color.Turquoise;
frm1.BackColor = Color.Turquoise;
}
You can do it using delegate and events or by implementing singleton in parent form.
I have a form with 10 radio buttons (Form2)for a user to check (all in same group). Then a button to go to the next form (Form3).
On Form3 I have a back button to go back to Form2 to change the radio button if needed.
When the back button is pressed, it goes to Form2 with all of the radio buttons, but it doesn't show the previously checked radio button.
Example code:
string SchoolName = "";
if (radioButton1.Checked)
{
SchoolName = radioButton1.Text;
}
if (radioButton2.Checked)
{
SchoolName = radioButton2.Text;
}
and then going back to the previous form using back button:
private void button3_Click(object sender, EventArgs e)
{
this.Close();
th = new Thread(opennewform);
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
private void opennewform(object obj)
{
Application.Run(new Form2());
}
recreating the form like new Form2() will add a initialized form with default values, and that will result in loose of your changes.
to solve you can:
play with show/hide instead of close/new
save object state, when re-opening form2 for example you can call a constructor like
new form2(check1State,check2State,selectedDropItem,txtName...);
In your opennewform() method you are instantiating a new copy of Form2 and not going back to the one you came from originally. That's why your original radio button selection is not being saved. You need to somehow return to the original Form2 instance instead of creating a new instance.
For example, you can hide the Form2 when user closes it and re-Show it when user needs it again.
You are not going back to the same instance of the form, you are creating a new form. See my two form project below
Form 1
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
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
string results = form2.GetData();
}
}
}
Form 2
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 Form2 : Form
{
Form1 form1;
public Form2(Form1 nform1)
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form2_FormClosing);
form1 = nform1;
form1.Hide();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//stops form from closing
e.Cancel = true;
this.Hide();
}
public string GetData()
{
return "The quick brown fox jumped over the lazy dog";
}
}
}
I am in need of a bit of help searched around the internet for about 2 days
to try and figure out how i can call an AxShockwaveFlash Variable from a second form this is for a game trainer something simple just getting into C# so i thought id start with one of these however i love everything i make to have a nice look and as many options as i can add so for my first C# Trainer i added a MenuStrip that opens a second form(VariableMods.cs) it has a few radio buttons a text box 3 toggle switches and 2 buttons one for setting a variable to whatever is typed in the text box while checking which radio button is checked and a button to close the variable menu which is another thing i need a bit of help with
-How do i Close the Second Form but still keep my Toggles On?
-How do i Call A Variable From (Form1.cs) to Form2(VariableMods.cs)
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 OCULAS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Show the Variable ModMenu
private void openVariableModsToolStripMenuItem_Click(object sender, EventArgs e)
{
VariableMods VarMenu = new VariableMods(this);
VarMenu.Show();
}
//Show About Program Box
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
InfoBox1 AboutBoxx = new InfoBox1();
AboutBoxx.Show();
}
//Close the Program
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
That is my Form1 Code Have Not Put anything into it about the ShockwaveFlash yet
And
here is Form2(VariableMods)
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 OCULAS
{
public partial class VariableMods : Form
{
Form1 originalForm; //Using "OriginalForm" To Call "Form1"
public VariableMods(Form1 IncomingForm)
{
originalForm = IncomingForm; //originalForm(Form1) is the form to be called
InitializeComponent();
BoxHeadVariables utf = new BoxHeadVariables();
}
private void ambiance_Button_21_Click(object sender, EventArgs e)
{
this.Hide();
}
private void ambiance_Button_11_Click(Form1 incomingForm, object sender, EventArgs e)
{
if (ambiance_RadioButton1.Checked == true)
{
MessageBox.Show("Damage Mod Is Active");
}
if (ambiance_RadioButton2.Checked == true)
{
MessageBox.Show("Speed Mod Is Active");
}
if (ambiance_RadioButton3.Checked == true)
{
MessageBox.Show("Range Mod Is Active");
}
}
}
}
Ok, here is the complete problem. I've created an simple application in C# which displays the current date & time & it has an "About" button through this button I've made it to open Form2 which displays my name. Everything works fine but whenever I open Form2 & close it(i.e by clicking on the "X" button on Form2 window frame) & again if I click on "About" button Form1 freezes & hangs up.
Here is the code Form1 code:
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 Date
{
public partial class Form1 : Form
{
Form3 SecondForm = new Form3();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label2.Text = DateTime.Now.ToString(" hh:mm:ss tt");
label4.Text = DateTime.Now.ToString(" dd-MM-yyyy ");
}
private void about_btn_Click(object sender, EventArgs e)
{
SecondForm.Show();
}
}
}
Here is the Form2 code:
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 Date
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
You need to create your SecondForm each time when you want to show it like this
namespace Date
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.label2.Text = DateTime.Now.ToString(" hh:mm:ss tt");
label4.Text = DateTime.Now.ToString(" dd-MM-yyyy ");
}
private void about_btn_Click(object sender, EventArgs e)
{
Form3 secondForm = new Form3();
secondForm.Show();
}
}
}
Problem in the original code is that when form closes, it tries to destroy controls, data on it, and when you try to call Show again, it can't display it as it already destroyed.
All I am trying to do is to change the state of checkbox on form2, and keep the state after pression OK.
I have form1 which is my main form and it only has one Strip menu. The code for form1 is as follow:
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog();
}
}
}
The Form2 only has one checkbox and one OK button.
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 test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void OK_Click(object sender, EventArgs e)
{
if (cbxForm2.Checked == true)
{
cbxForm2.Checked = true;
}
}
}
}
How can I change my code so when I go back on the menu the state of the combo box is as I left it?
You are creating a new Form2 every time:
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
// the 'new' keyword means you are creating an entirely new instance
Form2 dialog = new Form2();
dialog.ShowDialog();
}
This new instance has no idea what any previous instances looked like, so you need to store the state of the CheckBox and assign the value when you open Form2.
public partial class Form1 : Form
{
// backing field to store the state
bool checkBoxChecked;
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
// assign the state
dialog.CheckBoxChecked = this.checkBoxChecked;
dialog.ShowDialog();
// save the state
this.checkBoxChecked = dialog.CheckBoxChecked;
}
}
You also need to add a property on Form2 so you can retrieve the state:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool CheckBoxChecked
{
get { return cbxForm2.Checked; }
set { cbxForm2.Checked = value; }
}
}