I have a countdown and it outputs to a label on the main form, I want to popout the countdown from the main form to a child form because the child form will be able to stay on top of all other windows (for game) I can't get it to updated the countdown on the child's label. I only get the second when i hit the popout button.
This is on the main form
private void tmrMain_Tick(object sender, EventArgs e)
{
TimerSeconds = TimerSeconds - 1;
if (TimerSeconds == 0)
{
tmrMain.Stop();
if (chbxPlaySound.Checked)
{
System.Media.SystemSounds.Exclamation.Play();
}
if (chbxRepeat.Checked)
{
btnStartTimer.PerformClick();
}
}
string dis_seconds = Convert.ToString(TimerSeconds);
//
// lblTimer is the label that shows the countdown seconds
//
lblTimer.Text = dis_seconds;
}
This is my pop out button on the main form
private void btnPopOut_Click(object sender, EventArgs e)
{
PopTimer ShowTimer = new PopTimer(TimerSeconds);
ShowTimer.Show(this);
}
This is my child form
public partial class PopTimer : Form
{
public PopTimer(int countTimer)
{
InitializeComponent();
//string dis_seconds = Convert.ToString(TimerSeconds);
lblPopTimer.Text = Convert.ToString(countTimer); ;
}
}
Your PopTimer form needs an accessible method, something like this:
public void SetCountdown(int value) {
lblPopTimer.Text = value.ToString();
}
Then your PopTimer needs to be accessible, so move the declaration:
private PopTimer showTimer = null;
private void btnPopOut_Click(object sender, EventArgs e)
{
showTimer = new PopTimer(timerSeconds);
showTimer.Show(this);
}
Then in your tick event:
if (showTimer != null) {
showTimer.SetCountdown(timerSeconds);
}
Related
I have this piece of code, where it runs a function (MouseTracker) after clicking on a label
this MouseTracker, should capture a mouse position x and y after pressing the OK in dialog result.
namespace AdvancedStashHelper
{
public partial class Settings : Form
{
public int xTemp;
public int yTemp;
public Settings()
{
InitializeComponent();
}
private void MouseTracker()
{
while (true)
{
var result = MessageBox.Show("Move your cursor to position and press ENTER");
if (result == DialogResult.OK)
{
xTemp = MousePosition.X;
yTemp = MousePosition.Y;
}
break;
}
}
private void orbTransLabel_Click(object sender, EventArgs e)
{
Thread thread = new(MouseTracker);
thread.Start();
orbTransXPos.Text = xTemp.ToString();
}
private void orbAltLabel_Click(object sender, EventArgs e)
{
Thread thread = new(MouseTracker);
thread.Start();
}
}
}
The problem here it's, first time when you click on the label, nothing happens, but after you click the second time, the value is updated with previous mouse position.
This should function like this:
click on label
run the dialog that says to press enter to capture mouse position
update the values on form after pressing ok
do this for all labels with different mouse position values
thanks all for your info the code was simply as it is
namespace AdvancedStashHelper
{
public partial class Settings : Form
{
public int xTemp;
public int yTemp;
public Settings()
{
InitializeComponent();
}
private void MouseTracker()
{
var result = MessageBox.Show("Move your cursor to position and press ENTER");
if (result == DialogResult.OK)
{
xTemp = MousePosition.X;
yTemp = MousePosition.Y;
}
}
private void orbTransLabel_Click(object sender, EventArgs e)
{
MouseTracker();
orbTransXPos.Text = xTemp.ToString();
orbTransYPos.Text = yTemp.ToString();
}
private void orbAltLabel_Click(object sender, EventArgs e)
{
MouseTracker();
orbAltXPos.Text = xTemp.ToString();
orbAltYPos.Text = yTemp.ToString();
}
}
}
I have 3 forms. Names: MainScreen, LoadingForm, MoviesInfo.
When I press the button on the MainScreen, it is doing some works and the LoadingForm is loading inside a panel on the MainScreen.I want to do when works done, show the MoviesInfo Form on the same panel on the MainScreen or panel on the LoadingForm. How can I do that?
I add the forms on the panel like that.
public static void AddFormToPanel(Form frm, Panel panel)
{
frm.TopLevel = false;
panel.Controls.Add(frm);
frm.Show();
frm.Dock = DockStyle.Fill;
frm.BringToFront();
}
//Loading Form
public partial class LoadingForm : Form
{
public Action Worker { get; set; }
public LoadingForm(Action worker)
{
InitializeComponent();
if (worker == null)
{
throw new ArgumentNullException();
}
Worker = worker;
}
private void btnCancel_Click_1(object sender, EventArgs e)
{
this.Close();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Task.Factory.StartNew(Worker)
.ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
}
}
MainScreen Form
private void MovieOnButtonClick(object sender, EventArgs eventArgs)
{
using (loading = new LoadingForm(getMovieData))
{
loading.ShowDialog(this);
}
AddFormToPanel(moviesInfo, panelMain);
}
I don't want this line loading.ShowDialog(this); I want to add loading form inside the panel.
Move the action and the control to the MainForm:
private async void MovieOnButtonClick(object sender, EventArgs eventArgs)
{
var loadingForm = new LoadingForm(); // create a dummy loadingForm
AddFormToPanel(loadingForm, panelMain);
var work = Task.Factory.StartNew(Worker); // Worker = GetMovies or so
await work;
AddFormToPanel(moviesInfo, panelMain);
loadingForm.Dispose();
}
Remove all logic and events from the LoadingForm.
And it is better to use UserControl than Forms to place in that Panel.
I have a List of Objects. What I want to do:
Build a Dialog Box which shows a Radio Button for each element in the given List and returning the selected element/value by clicking on OK-Button.
Thanks in Advance.
Here is a quick example of creating your own form and getting a value from it.
Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frmTest frmTest = new frmTest();
DialogResult dr = frmTest.ShowDialog();
if(dr == System.Windows.Forms.DialogResult.OK)
{
string value = frmTest.GetValue();
MessageBox.Show(value);
}
}
}
Form1 View:
public partial class frmTest : Form
{
private string _value { get; set; }
public frmTest()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
this._value = radioButton.Text; // Assign the radio button text as value Ex: AAA
}
public string GetValue()
{
return this._value;
}
}
You have to make sure that all radio buttons are using radioButton_CheckedChanged for the CheckedChanged event.
Form2 View:
Output:
build your own form and add a public variable "a string for example" called "Result"
public partial class YourDialog:Form
{
public string Result = "";
public YourDialog()
{// add all the controls you need with the necessary handlers
//add the OK button with an "On Click handler"
}
private void OK_Button_Click(object sender, EventArgs e)
{
//set the Result value according to your controls
this.hide();// will explain in the main form
}
}
// in your main form
private string GetUserResult()
{
YourDialog NewDialog = new YourDialog();
NewDialog.ShowDialog();//that's why you only have to hide it and not close it before getting the result
string Result = NewDialog.Result;
NewDialog.Close();
return Result;
}
OOps! I came back just to see there are already 2 answers! How ever, I want to post my version, which can build controls according to list of strings:
//dialog form
public partial class frmDialogcs : Form
{
public string selectedString;
//keep default constructor or not is fine
public frmDialogcs()
{
InitializeComponent();
}
public frmDialogcs(IList<string> lst)
{
InitializeComponent();
for (int i = 0; i < lst.Count; i++)
{
RadioButton rdb = new RadioButton();
rdb.Text = lst[i];
rdb.Size = new Size(100, 30);
this.Controls.Add(rdb);
rdb.Location = new Point(20, 20 + 35 * i);
rdb.CheckedChanged += (s, ee) =>
{
var r = s as RadioButton;
if (r.Checked)
this.selectedString = r.Text;
};
}
}
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
//in main form
private void button1_Click(object sender, EventArgs e)
{
var lst = new List<string>() { "a", "b", "c" };
frmDialogcs dlg = new frmDialogcs(lst);
if (dlg.ShowDialog() == DialogResult.OK)
{
string selected = dlg.selectedString;
MessageBox.Show(selected);
}
}
I have a timer function within my main app which acts as a timed session. However, I want to change the timer interval and an ability to turn it on and off via the sub form, but cannot use the timer functions elements within another form to change the settings. Below is my code snippet. Any tips or example will be appreciated.
Attempt :
Main Form
private void Booyaa_Load(object sender, EventArgs e)
{
BooyaaTimer.Interval = 45 * 60 * 1000); // 45 mins
BooyaaTimer.Tick += new EventHandler(BooyaaTimer_Tick);
BooyaaTimer.Start();
if (!Properties.Settings.Default.SettingShutdown)
{
MessageBox.Show("Not properly shut down");
GetPass pass = new GetPass();
DialogResult result = pass.ShowDialog();
if (result == DialogResult.OK) {
Properties.Settings.Default.SettingShutdown = true;
Properties.Settings.Default.Save();
}
else
{
Close();
}
}
private void BooyaaTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("TIME IS UP");
GetPass pass = new GetPass();
DialogResult result = pass.ShowDialog();
if (result == DialogResult.OK)
{
Show();
Properties.Settings.Default.SettingShutdown = true;
Properties.Settings.Default.Save();
}
else
{
BooyaaTimer.Start();
Properties.Settings.Default.SettingShutdown = false;
Properties.Settings.Default.Save();
Hide();
}
}
Timer controls form
public object BooyaaTimer { get; private set; }
private void btn_confirm_Click(object sender, EventArgs e)
{
BooyaaTimer.Interval = Int32.Parse(textBox1.Text); // gives error on interval
}
I'm going to make a guess. The Timer is created in the main form
public partial class Form1 : Form {
Timer BooyaaTimer = new Timer(); // Or this is created in Designer
void SomeFunctionThatCreatesTheOtherForm() {
TimerControlsForm form2 = new TimerControlsForm();
// Pass the timer to form2
form2.BooyaTimer = BooyaTimer;
form2.ShowDialog();
}
}
And the other form
public partial class TimerControlsForm : Form {
// This has to be a Timer object
public Timer BooyaTimer {get; set;}
private void btn_confirm_Click(object sender, EventArgs e) {
BooyaaTimer.Interval = Int32.Parse(textBox1.Text);
}
}
I have a Mdiparent form containing a button and some child forms.
How is it possible to change backcolor of all textboxes in all child forms when clicking the button in parent form?
i know answer is already given.. but i would go with event and delegates..
multicast delegate is best choice is here
so here is my solution.
namespace winMultiCastDelegate
{
public partial class Form1 : Form
{
public delegate void ChangeBackColorDelegate(Color backgroundColor);
//just avoid null check instanciate it with fake delegate.
public event ChangeBackColorDelegate ChangeBackColor = delegate { };
public Form1()
{
InitializeComponent();
//instanciate child form for N time.. just to simulate
for (int i = 0; i < 3; i++)
{
var childForm = new ChildForm();
//subscribe parent event
this.ChangeBackColor += childForm.ChangeColor;
//show every form
childForm.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
ChangeBackColor.Invoke(Color.Black);
}
}
/// <summary>
/// child form class having text box inside
/// </summary>
public class ChildForm : Form
{
private TextBox textBox;
public ChildForm()
{
textBox = new TextBox();
textBox.Width = 200;
this.Controls.Add(textBox);
}
public void ChangeColor(Color color)
{
textBox.BackColor = color;
}
}
}
This the ChilForm;
public ChilForm()
{
InitializeComponent();
}
public void ChangeTextboxColor()
{
textBox1.BackColor = Color.Yellow;
}
And this is Parent;
ChilForm frm = new ChilForm();
public Parent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Shows the child
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//Changes color
frm.ChangeTextboxColor();
}