Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I'm a newbie to C# OOP.
I have an application that connects to a GsmModem. The app has two forms i.e: Main-form(Main-Menu) and Form2(Messages).
I'm able to open the port in Main-Menu and connect to it. I want to be able to access it from Form2, which I will use to send SMSs

How can I achieve this
Your Help is highly appreciated

What I have tried:

//OPENING PORT IN MAIN-MENU

public static string PortNumber = ComPortHolder.GetportInfo();
 
public GsmCommMain comm1 = new GsmCommMain(PortNumber, 9600, 150);


 private void MainMenu_Load(object sender, EventArgs e)
        {
            ComPort();
        }

public void ComPort()
{
 if (!(comm1.IsOpen()))
    {
     comm1.Open();
     ConTo.Text = $"{PortNumber} Connected";
     ConTo.ForeColor = Color.FromArgb(5, 142, 237);

     SignalQualityInfo Info = comm1.GetSignalQuality();
     Double A = Convert.ToDouble(Info.SignalStrength.ToString());
     Double B = (A / 30) * 100;

     SigSt.Text = Convert.ToString(Math.Round(B, 0,MidpointRounding.ToEven)) + "%"; 
                        
    }
    else
    {
      return;
    }
}



//ACCESSING PORT IN FORM2(MESSAGES)

public static MainMenu MM = new MainMenu();

private void BtnSnd2_Single_Click(object sender, EventArgs e)
        {
            try
            {
                txtMessage1.Text = ClientName.Text + NewLine + TxtMsgBx.Text;
                string TimeStamp = (DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString());
                if (txtMessage1.Text != string.Empty && TxtPhnNum.Text != string.Empty)
                {
                   
                    //MM.comm1.Open();
                    SmsSubmitPdu pdu;
                    byte dcs = (byte)DataCodingScheme.GeneralCoding.Alpha7BitDefault;
                    pdu = new SmsSubmitPdu(txtMessage1.Text, Convert.ToString(TxtPhnNum.Text), dcs)
                    {
                        RequestStatusReport = true
                    };

                    
                    MM.comm1.SendMessage(pdu);

                    

                    DC.Message = txtMessage1.Text;
                    DC.PhoneNumber = TxtPhnNum.Text;
                    DC.TimeSent = TimeStamp.ToString();
                    DC.InsertSMS();

                    DataGridView2.DataSource = null;
                    DF.DataGd_Data();
                    DataGridView2.DataSource = DF.DT2;
                    MM.comm1.Close();

                    int A = DF.DT2.Rows.Count;
                    //RowCnt.Text = "" + A;
                }



            }
            catch
            {
                MessageBox.Show("Invalid/ No COMPORT found make sure the device is connected/installed correctly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted
Updated 1-Jul-22 3:04am
v2

1 solution

When you write this in Form2:
public static MainMenu MM = new MainMenu();
it does what it says: creates a new instance of the form which - like all other class instances - has nothing to do with any existing instances.

In addition, doing it this way breaks OOPs: it "locks" the two forms together so you can't change one without considering the effects on the other.
"Child" forms should not know or care about "Parent" forms: the parent needs to know some info about the child because it is explicitly displaying it, but the reverse should not be the case.

Instead, pass the serial port instance to the Form2 when you create it: Transferring information between two forms, Part 1: Parent to Child[^] and pass information abck up if you need to like this: Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
Comments
CPallini 1-Jul-22 9:26am    
5.
0071974 1-Jul-22 18:10pm    
Ok guys ive tried this:

private void BtnMsg_Click(object sender, EventArgs e)
{
Messages frm2 = new Messages(comm1);
frm2.Show();
}
and in frm2:

GsmCommMain Comm1;
public Messages(GsmCommMain comm1)
{
InitializeComponent();
Comm1 = comm1;
}
but the problem is when I send an SMS the port closes in MainMenu
the SMS is sent though;
0071974 1-Jul-22 18:49pm    
Fixed it;
Thanks @ OriginalGriff
OriginalGriff 2-Jul-22 0:55am    
Excellent!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900