Updating controls in child form from parent form - winforms

I have below code in Form2
public void authorisedList()
{
using (myContext v = new myContext())
{
DateTime date = DateTime.Today.AddMonths(-12);
var myList = (from l in v.AuthorisedList
where l.FromDate >= date
select new
{
l.ID,
l.EmpName,
l.StartDate,
l.EndDate,
l.Days,
l.Approved,
l.Confirmed,
}).ToList();
reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource datasource = new ReportDataSource("MyReportsDatasource", myList);
reportViewer1.LocalReport.DataSources.Add(datasource);
string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
string reportPath = Path.Combine(exeFolder, #"rdlcReports\Authorised List.rdlc");
reportViewer1.LocalReport.ReportPath = reportPath;
reportViewer1.RefreshReport();
}
}
Then in Form1 which is a parent of Form2, i have below code in radiobutton
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Form2 au = new Form2(this);
au.authorisedList();
}
The problem is that when i check a radioButton control (radioButton1) in Form1, authorisedList() in Form2 seems to be executing but the reportViewer report does not update/change.
Am wondering why.

If your Form2 is already open , then you should get the object of the open form and then call its authorisedList() method. You can use Application.OpenForms property.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Form2 au = Application.OpenForms["Form2"] as Form2;
if(au != null)
au.authorisedList();
}

Related

Error in loop when trying to make my array of labels match a list from another class

I'm trying to create a loop that will change my array of labels to match a list from another class. I declared the list as
ADDITIONAL INFO: I am creating a payroll program. When the Pay button is clicked for an employee, the Status list with that employee index gets updated into "Paid". I want the label on the other form to reflect the word "Paid" as well
private List<string> Status = new List<string>(2);
public List<string> GetList()
{
return Status;
}
private void btnFind_Click(object sender, EventArgs e)
{
Status.Add("Unpaid");
Status.Add("Paid");
Status.Add("Unpaid");
}
And in the second form
public partial class Form2 : Form
{ Label[] Labels = new Label[3];
private void Form2_Load(object sender, EventArgs e)
{
Labels[0] = this.lblTony;
Labels[1] = this.lblSteve;
Labels[2] = this.lblPeter;
Form1 f1 = new Form1 ();
for (int i = 0; i !=3 ; i++)
{
List<string> Status = f1.GetList();
Labels[i].Text = Status[i];
}
}
}
'''
I tried to only put the relevant codes but I may be missing something? The error is IndexOutOfRangeException which I do not get because the are the same size. I tried changing the sizes but the error only changes to argumentoutofbounds
try this :
status.Add(lables[i].Text);
insted of
lables[i].Text = status[i];
I Dont Test it But I think its going to work!
go forward
There are a lot of things that are not great in the code you have shown but if you want to follow this approach then why not just:
class Form1
{
private List<string> Status = new List<string>();
public List<string> GetList()
{
return Status;
}
// I'm guessing as to how you add elements to Status
public Form1()
{
Status.Add("Item 1");
Status.Add("Item 2");
Status.Add("Item 3");
}
}
public partial class Form2 : Form
{
Label[] Labels = new Label[3];
private void Form2_Load(object sender, EventArgs e)
{
Labels[0] = this.lblTony;
Labels[1] = this.lblSteve;
Labels[2] = this.lblPeter;
Form1 f1 = new Form1();
List<string> Status = f1.GetList();
Debug.Assert(Labels.Length == Status.Count());
for (int i = 0; i < Status.Count(); ++i)
{
Labels[i].Text = Status[i];
}
}
}
It looks like your main problem is that you have allocated a space of 2 for Status when you construct the object but then run the loop for 3 when copying the values over. If you are using a List you rarely need to pre-allocate its size.
I would create your Label array using this kind of syntax so you don't have to specify a size:
Label[] Labels;
private void Form2_Load(object sender, EventArgs e)
{
Labels = new Label[] { this.lblTony, this.lblSteve, this.lblPeter };
}
Also, I suspect that you're getting the error because of this line in Form2:
Form1 f1 = new Form1 ();
...as this is creating an "invisible" instance of Form1 and is likely not the Form1 on your screen where presumably the "btnFind" button was clicked to add items to the List (is that a typo in your posted code?).
If that's the case, then you need a reference to the actual visible Form1. You can do use by either passing Form1 into Form2 via a constructor or method, via the Show() method, or by iteration over the Application.OpenForms collection and finding Form1 there.
I'm just trying to make the label array text become the status list
from form1.
Here's one way to pass Form1 into Form2 via the Show() method:
private void button1_Click(object sender, EventArgs e)
{
// ... in Form1 ...
Form2 f2 = new Form2();
f2.Show(this); // <-- pass Form1 via "this"
}
Now, over in Form2, you can cast this.Owner back to Form1 and use that to access the list:
private void button1_Click(object sender, EventArgs e)
{
// ... in Form2 ...
Form1 f1 = (Form1)this.Owner;
List<String> status = f1.GetList();
// ... do something with "status" ...
}

Prevent RepositoryItemSearchLookUpEdit when Popup is Open When CloseUpKey.Key is pressed

I use a RepositoryItemSearchLookUpEdit. its CloseUpKey property is set to space
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
I want to use this shortcut only for open popup and not for closing popup.
How can I achieve this?
UPDATE------------------------
First I create an RepositoryItemSearchLookUpEdit object
var result = new RepositoryItemSearchLookUpEdit();
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
result.KeyDown += repositoryItemLookUpEdit_KeyDown;
result.CloseUp += repositoryItemLookUpEdit_CloseUp;
result.QueryCloseUp += repositoryItemLookUpEdit_QueryCloseUp;
private void repositoryItemLookUpEdit_QueryCloseUp(object sender, System.ComponentModel.CancelEventArgs e)
{
var edit = sender as SearchLookUpEdit;
KeyEventArgs k = new KeyEventArgs(edit.Properties.CloseUpKey.Key);
AttachKeyPressEvent(k);
if (k.KeyCode == edit.Properties.CloseUpKey.Key)
e.Cancel = true;
}
And pass it to a grid column:
grdListView.Columns["yyy"].ColumnEdit = result
How can I achieve that with these events, without creating a descendant SearchLookUpEdit
UPDATED:
The problem is that CloseUp event (where you could get the necessary info about the closeup key) occurs after the QueryCloseUp event (where you could precent the closing up event). Also the KeyPress, KeyDown and KeyUp events seem also NOT to occur when the QueryCloseUp occurs, as a result they couldn't be overridden. So I tried this, I created a custom KeyEventHandler and triggered him during QueryCloseUp event in order to get the necessary info of what key was pressed and cancel the event if the close key event was the one. Here is my codeTry it to see if it suits you
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Here you can add your grid control as you have created
DataTable dt = new DataTable();
dt.Columns.Add("ID"); //use your own names and types
gridControl1.DataSource = dt;
var result = new RepositoryItemSearchLookUpEdit();
result.CloseUpKey = new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.Space);
result.QueryCloseUp += new CancelEventHandler(repositoryItemLookUpEdit_QueryCloseUp);
((gridControl1.MainView as GridView).Columns["ID"] as GridColumn).ColumnEdit = result;
}
private static readonly object myQueryCloseUp = new object();
public event KeyEventHandler MyQueryCloseUp
{
add { Events.AddHandler(myQueryCloseUp, value); }
remove { Events.RemoveHandler(myQueryCloseUp, value); }
}
protected virtual void AttachKeyPressEvent(KeyEventArgs e)
{
KeyEventHandler handler = (KeyEventHandler)Events[myQueryCloseUp];
if (handler != null)
handler(this, e);
}
//Here you add your own Handler implementation
public void repositoryItemLookUpEdit_QueryCloseUp(object sender, CancelEventArgs e)
{
KeyEventArgs k = new KeyEventArgs((sender as SearchLookUpEdit).Properties.CloseUpKey.Key);
AttachKeyPressEvent(k);
if (k.KeyCode == (sender as SearchLookUpEdit).Properties.CloseUpKey.Key)
e.Cancel = true;
}
}

Cannot set the value of a global variable

Im pretty new to programming, but im very eager to get more into this stuff, and in particular, c#. I have a made some code for an autotyper (spam bot if i may), only to be used as a goal for me to create. Essentially, what i want the program to do, is as following:
When i start my Form1, the global variable "_timerValue" is set to
1000
When i hit Start button, the text from the textbox on will be sent at
the interval of "_timerValue"
When i hit the Speed button, Form2 will show.
When i hit very fast, "_timerValue" is set to 5000 (testing purposes)
Form1 code:
public partial class Form1 : Form
{
static class TimerIntervalValue
{
Form2 f2 = new Form2();
TimerIntervalValue = f2._timerValue;
}
public Form1()
{
InitializeComponent();
f2._timerValue = "1000";
}
public void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send(textBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show(f2._timerValue);
timer1.Interval = Convert.ToInt32(f2._timerValue);
if (timer1.Enabled == false)
{
timer1.Enabled = true;
textBox1.Enabled = false;
button1.Text = ("Stop");
}
else if (timer1.Enabled == true)
{
timer1.Enabled = false;
textBox1.Enabled = true;
button1.Text = ("Start");
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
Form2 code:
public partial class Form2 : Form
{
public string TimerValue;
public string _timerValue
{
get { return TimerValue; }
set { TimerValue = value; }
}
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._timerValue = "5000";
}
}
I originally tried to create a Form2 instance just under "InitializeComponent();" in Form1, but that didnt seem to be accessible through the other funtions.
I just know its something very simple like im using the wrong class to create the Form2 instance or something like that ...
Anyway, thank you in advance
Just mark TimerValue and _timerValue as static. Then you don't need to use
Form2 f2 = new Form2(); or Form2 frm2 = new Form2();
anymore. In Form 1, just use Form2._timerValue instead of f2._timerValue. In Form 2, just change:
public void button1_Click(object sender, EventArgs e)
{
_timerValue = "5000";
}

Access Form1 controls from Form2

Have two forms-Form1 & Form2.Form1 has a button(btnNew),which opens Form2 on click, and is disabled.I need to enable the button again, only when Form2 is closed.User needs to use Form1 also simultaneously.This code is not enabling the button again. Where am I missing.
In Form1:
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
btnNew.Enabled = false;
}
public void EnableButton()
{
btnNew.Enabled = true;
}
In Form2:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
f1.EnableButton();
}
You code creates a new Form1 which will be different from the one already running in your application.
You could try adding a reference to Form1 in your Form2 and operate on it's controls that way.
Give form2 a property like:
public Form ParentForm {get; set;}
And assign it form1 in your button click:
Form2 f2 = new Form2()
f2.ParentForm = this;
f2.show();
Then in your closing you should be able to do something like:
this.ParentForm.EnableButton();
Subscribe to your Form2 closing event from within the class that is instantiating it (Form1).
private void btnNew_Click_1(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Closing += f2_Closing;
f2.Show();
btnNew.Enabled = false;
}
public void f2_Closing(object sender, FormClosingEventArgs e)
{
this.EnableButton();
}
public void EnableButton()
{
btnNew.Enabled = true;
}

How to create and use WebBrowser in background thread?

How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:
var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
private void wbThread()
{
CWebBrowser browser = new CWebBrowser();
var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}
CWebBrowser - custom class, wich delegate System.Windows.Forms.WebBrowser object Navigate method and wait until page completed loads. The problem is LoadCompleted event on System.Windows.Forms.WebBrowser object never raises. I found some solution here, but it does not work (can't find method Application.Run() on my WPF app).
public class CWebBrowser : ContentControl
{
public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
private readonly AutoResetEvent loadCompletedEvent;
private readonly AutoResetEvent navigatedEvent;
public enum EventType
{
navigated, loadCompleted
}
public CWebBrowser()
{
innerWebBrowser = new System.Windows.Forms.WebBrowser();
loadCompletedEvent = new AutoResetEvent(false);
navigatedEvent = new AutoResetEvent(false);
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = innerWebBrowser;
Content = host;
innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
}
void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
{
navigatedEvent.Set();
}
void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
return;
var doc = innerWebBrowser.Document;
loadCompletedEvent.Set();
}
public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
{
if (etype == EventType.loadCompleted)
loadCompletedEvent.Reset();
else if (etype == EventType.navigated)
navigatedEvent.Reset();
innerWebBrowser.Navigate(url);
if (etype == EventType.loadCompleted)
loadCompletedEvent.WaitOne();
else if (etype == EventType.navigated)
navigatedEvent.WaitOne();
System.Windows.Forms.HtmlDocument doc = null;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
delegate
{
doc = innerWebBrowser.Document;
}));
return doc;
}
}
Thansk for all advices and sorry for my bad english :o(
Why don't you use the default WebBrowser control like this?
public MainPage()
{
InitializeComponent();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}
void startNavigate()
{
WebBrowser wb = new WebBrowser();
wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
wb.Navigate(new Uri("http://www.google.com"));
}
void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// e.Content
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
// e.Content when the document finished loading.
}
Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.

Resources