I am new to C# and programming in general and trying to learn/practice a bit before starting classes in spring. Anyway I am trying to get the Tag of a checkbox control from Form A to Form B into a textbox on FormB (using string builder because there is other information from textboxes going to FormB Textbox as well) Now i can get this to work fine on Form B load but due to how my program flows i need to make this happen on a button click. Below is the current code that is working on FormB load. Sorry if i am missing something basic here.
Form B. I omitted some of my stringbuilder lines for space.
public CodeBlueForm(List<string> ids, string custNameCb, string custWtnCb, string custCbrCb, string custNotesCb)
{
InitializeComponent();
cbNameText.Text = custNameCb;
cbWtnText.Text = custWtnCb;
cbCbrText.Text = custCbrCb;
cbNotesText.Text = custNotesCb;
string checkBoxesLines = "Lights: ";
foreach (string id in ids)
checkBoxesLines += string.Format("{0}, ", id);
System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
strBuilder.AppendLine("Inet Notes: " + "\r" + ((checkBoxesLines) + cbNotesText.Text));
cbViewText.Text = strBuilder.ToString();
{
My current Form A
private void code_blue_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var checkBoxIds = GetCheckBoxIds();
tShootCb = tShootText.Text;
svcsOfferedCb = svcsOfferedText.Text;
otherNotesCb = otherNotesText.Text;
custModemCb = custModemText.Text;
//Opens CODE BLUE form and pushes text to it.
CodeBlueForm cbForm = new CodeBlueForm(checkBoxIds, custNameText.Text, custCbrText.Text, custBtnText.Text, custModemCb + "\r\n" + tShootCb + "\r\n" + svcsOfferedCb + "\r\n" + otherNotesCb);
cbForm.Show();
}
private List<string> GetCheckBoxIds()
{
List<string> checkBoxIds = new List<string>();
foreach (Control control in pnlCheckBoxes.Controls)
{
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
if (checkBox.Checked && checkBox.Tag is string)
checkBoxIds.Add((string)checkBox.Tag);
}
}
return checkBoxIds;
As i said this currently does work on Form B load but i need to figure out how to make it work in FormB button click. Any direction on this would be great.
Thanks
You can pass a handle of FormA to FormB upon creation. All items that are on your FormA can then be accessed in FormB using that handle.
and
In this example you have a checkbox called
myCheckBoxon FormA, a textbox calledmyTextBoxon FormB and a button on each form. Each button has a method calledbuttonClickregistered to the click event. The button on FormA creates and shows an instance of FormB and passes along a handle to itself (this). The button on FormB can then use the handle (calledparentin this example) to access all controls on FormA IFF you made them accessible by changing theModifiersproperty topublic. (This is not a nice way to do it, but for the sake of simplicity I’d recommend to do it in that way for now. Once you understand the dependencies you should move on the create public getter/setter methods to encapsulate exactly what should be changed instead of exposing the whole control.)