I need to build an array to contain objects. However, it does not work and I could not find out the error myself. Please help
# include "CRegistration.h" //My object class
SKIP MANY LINES
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
array<CRegistration^> ^CregArray = gcnew array<CRegistration^>(100);
record_number = 0;
}
private: System::Void MyAdd_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ myForm2 = gcnew Form2();
if (myForm2->ShowDialog()==System::Windows::Forms::DialogResult::OK) {
record_number = record_number + 1;
CRegistration^ Creg = gcnew CRegistration(System::Convert::ToString(record_number),myForm2->TempReg[0],myForm2->TempReg[1],myForm2->TempReg[2],myForm2->TempReg[3]);
CregArray[record_number-1] = Creg;
}
}
};
change line
array<CRegistration^> ^CregArray = gcnew array<CRegistration^>(100);
to
CregArray = gcnew array<CRegistration^>(100);
add field declaration:
Form1(void)
{
..
}
array<CRegistration^> ^CregArray;
private: System::Void MyAdd_Click(System::Object^ sender, System::EventArgs^ e) {
..
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've been strugling with this error for a while now.
I'm trying to implement a log in to an ATM system and I'm trying to connect it to a database as well. But when I try to run it, shows up 144 errors that are all the same:
'MyForm2': is not a member of 'jiji'
'MyForm2': undeclared identifier
Syntax error: missing ';' before identifier 'f2'
'f2':undeclared identifier
I honestly don't know why is this happening. I've tried to include MyForm2.h in the main code but nothing changes.
Here I leve the piece of code that is nearly the same for all my 7 forms ( except the 2nd that is the one that is giving the error I think)
MyForm.h:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
try {
String^ User = textBox1->Text;
String^ Pwd = textBox2->Text;
int AN = Int32::Parse(textBox3->Text);
String^ constr = "Server=127.0.0.1;Uid=root;Pwd=;Database=visualcppdb";
MySqlConnection^ con = gcnew MySqlConnection(constr);
MySqlDataAdapter^ da = gcnew MySqlDataAdapter("Select AccountNum from Userinfo WHERE AccountNum = '" + AN + "'", con);
DataTable^ dt = gcnew DataTable();
da->Fill(dt);
if (dt->Rows->Count >= 1) {
MySqlCommand^ cmd = gcnew MySqlCommand("insert into Db values (" + AN + ") ", con);
con->Open();
MySqlDataReader^ dr = cmd->ExecuteReader();
con->Close();
try {
this->Hide();
jiji::MyForm2 f2;
f2.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
else {
MessageBox::Show("User does not exist, try again");
}
}
catch (Exception^ e) {
MessageBox::Show(e->Message);
}
}
By the way AN is the Account number.
The MyForm2.h code is the same over and over again except for the ending.
MyForm2.h:
//Extract money
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm3 f3;
f3.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Insert Money
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm4 f4;
f4.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Transfer
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm5 f5;
f5.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Check Amount
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm6 f6;
f6.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Reload phone
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
try {
this->Hide();
jiji::MyForm7 f7;
f7.ShowDialog();
this->Show();
}
finally {
this->Close();
}
}
//Exit
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
try {
String^ constr = "Server=127.0.0.1;Uid=root;Pwd=;Database=visualcppdb";
MySqlConnection^ con = gcnew MySqlConnection(constr);
MySqlCommand^ cmd = gcnew MySqlCommand("TRUNCATE Db ", con);
con->Open();
MySqlDataReader^ dr = cmd->ExecuteReader();
con->Close();
}
catch (Exception^ e) {
MessageBox::Show(e->Message);
}
finally {
this->Close();
}
}
Perhaps you aren't including MyForm2.h in a context MyForm.h can access?
Maybe look into how to declare a namespace and how to include headers, e.g. Creating a C++ namespace in header and source (cpp)
What happens if you inline the MyForm2 class definition into MyForm so you guarantee it is accessible?
I have prepared more than one UserControl for a windows program in XAML. Each user control works as a separate page. But I do page transitions in navigate class. In ".xaml.cs" when calling User control
Navigate.navigate (navigate_grid, new DeviceLayout ());
I'm using the line of code. But every time I create a new user control, the background functions don't work. How do I flip one instead of invoking a new user control each time?
class Navigate
{
public static void navigate(Grid grd, UserControl uc)
{
if (grd.Children.Count > 0)
{
grd.Children.Clear();
grd.Children.Add(uc);
}
else { grd.Children.Add(uc); }
}
}
Example navigate:
public SettingsView()
{
InitializeComponent();
Navigate.navigate(navigate_grid, new SystemLayout());
}
private void system_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new SystemLayout());
previous_page.Text = "";
current_page.Text = "SİSTEM";
next_page.Text = "UYGULAMA";
}
private void application_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ApplicationLayout());
previous_page.Text = "SİSTEM";
current_page.Text = "UYGULAMA";
next_page.Text = "BAĞLANTI";
}
private void connection_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ConnectionLayout());
previous_page.Text = "UYGULAMA";
current_page.Text = "BAĞLANTI";
next_page.Text = "ÜRÜNLER";
}
private void product_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ProductsLayout());
previous_page.Text = "BAĞLANTI";
current_page.Text = "ÜRÜNLER";
next_page.Text = "CİHAZLAR";
}
private void device_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new DeviceLayout());
previous_page.Text = "ÜRÜNLER";
current_page.Text = "CİHAZLAR";
next_page.Text = "YAZICILAR";
}
private void printer_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new PrinterLayout ());
previous_page.Text = "CİHAZLAR";
current_page.Text = "YAZICILAR";
next_page.Text = "KULLANICILAR";
}
private void users_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new UsersLayout());
previous_page.Text = "YAZICILAR";
current_page.Text = "KULLANICILAR";
next_page.Text = "BAKIM";
}
private void maintenance_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new MaintenanceLayout());
previous_page.Text = "KULLANICILAR";
current_page.Text = "BAKIM";
next_page.Text = "HAKKINDA";
}
private void info_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new InfoLayout());
previous_page.Text = "BAKIM";
current_page.Text = "HAKKINDA";
next_page.Text = "";
}
}
Not sure if I get what you mean.
When you change the UserControl with the use of any of those functions eg info_button_click you can't access this funtion anymore.
That would be the case, because your XAML and .cs file are one class, containing those funcitons. If you change the UserControl (XAML) you will also change the .cs file. Therefore you can't access those functions anymore.
You could probably get the behaviour you want if you bind the commands to a viewmodel, which you could then pass through the navigation as well?
Sry, I'm still not sure what exactly it is you're doing.
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";
}
I want to create multiple forms and when I click the back button it will back to form1. I tried this C++/CLI - how to open a new form and back but when I click the button in the form2 to go back to form1 it gets an error. " NullReferenceException was Unhandled"
Form1
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
/* Form1::Hide();
Form2^ form2 = gcnew Form2();
form2->ShowDialog();*/
Form2 ^ frm2 = gcnew Form2();
frm2->Show();
this->Hide();
}
Form2
Form2(System::Windows::Forms::Form ^ frm1)
{
otherform = frm1;
InitializeComponent();
}
private: System::Windows::Forms::Form ^ otherform;
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e
{
this->Hide();
otherform->Show();
}
You have created the second constructor of Form2 but you are not using it inside button1_Click.
Instead of this:
Form2 ^ frm2 = gcnew Form2();
Do this:
Form2 ^ frm2 = gcnew Form2(this);
I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}