Drag data from data base to a listbox - c#

I want some data to be displayed in a listbox when the form is loaded. But after it is loaded nothing changes.
I added DataSource and used foreach to get the items.
public partial class MainForm : Form
{
public CDiary diary;
public MainForm()
{
InitializeComponent();
diary = new CDiary("mongodb://localhost:27017");
}
private void MainForm_Load(object sender, EventArgs e)
{
listOfEvents.DataSource = diary.Events;
foreach(CEvent ev in diary.Events){
listOfEvents.Items.Add(ev.Date);
}
}
}
Thanks.

Related

How to save and open the content in the one of subforms separately?

There are two forms, a MainForm and a GraphicsForm.
In MainForm, there are "New" and "Save", "Open" buttons. When clicking the "New", a GraphicsForm created (When the "New" is clicked multiple times, multiple GraphicsForms are created).
The question is, when created multiple GraphicsForms, and the user only wants to save the content in one of them or open a content file to one of them, How to implement this?
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private ContentDoc _contentDoc = new ContentDoc();
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm(_contentDoc);
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // here
}
private void Open_Click(object sender, EventArgs e)
{
... // here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//ContentDoc is a class to manage all the graphics drawn by the user in the form.
private ContentDoc _contentDoc = new ContentDoc();
public GraphicsForm(ContentDoc contentDoc)
{
InitializeComponent();
_contentDoc = contentDoc;
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
The parent form has an ActiveMdiChild property, so you can use the to access the currently-selected GraphicsForm instance:
var activeGraphicsForm = ActiveMdiChild as GraphicsForm;
There are other variations you might use, e.g. pattern matching, depending on the specific details and your preference.
You can then put your saving logic in a public method in GraphicsForm and call it from the parent form. Alternatively, you can put your saving logic in the parent form and expose the data to be saved via one or more public properties in GraphicsForm.

how to use textbox content from a form to a usercontrol?

i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}

How to call a comboBox from another form (class) to be used inside a button

I want to call a some combo-box items so i can make an if /else statements and output a form.The combo-box items are out side of my class(Form) how can i access them i tried this(below) but the error says does not exist in the current context.I also changed it
the method from private to public
public void buttonFinish_Click(object sender, EventArgs e)
{
if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
{
}
}
Send ComboBox from form1 to form2 using constructor. Here is example:
Form1 Class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(comboBox1, comboBox2);
f2.Show();
}
}
Form2 Class:
public partial class Form2 : Form
{
ComboBox comboBoxD;
ComboBox comboBoxType;
public Form2(ComboBox cb, ComboBox cbType)
{
InitializeComponent();
comboBoxD = cb;
comboBoxType = cbType;
}
private void Form2_Load(object sender, EventArgs e)
{
}
protected void buttonFinish_Click(object sender, EventArgs e)
{
if(comboBoxD.Text == "Alphabet" && comboBoxType.Text == "Numbers")
{
}
}
}
UPDATE:
Here is another approach for accessing controls present in another form.
Default Modifiers of every control is private. For controls you want to access from another form you have change Modifiers property as Public.
Form1 Class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
}
Form2 Class:
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 f)
{
InitializeComponent();
f1 = f;
}
protected void buttonFinish_Click(object sender, EventArgs e)
{
if(f1.comboBoxD.Text == "Alphabet" && f1.comboBoxType.Text == "Numbers")
{
}
}
}
This is because your ComboBox is only available in the codebehind file of your Form.
One solution would be to store a reference to your combobox as a property in your codebehind.
like this:
public ComboBox myCmbBox { get; private set; }
and access it in the codebehind of form2.
You can write a public method in your ComboBox's class and then call it from where you have instance of that form.
like this:
in your main form:
using (var modal = new MyModal())
{
modal.ShowDialog();
modal.getSomething();
}
in your modal:
public string getSomething()
{
return yourComboBox.Text;
}

C# Pass selection from listbox in form1 to textbox in form2

I've got two forms and a custom class. I have populated a listbox in form1 using my custom class which holds several data types. I want to pass each of those values in the class located in the listbox to individual text boxes in form2. I'm having trouble figuring out how to access the individual values in each listbox instance of my class and then split them among the text boxes in form2. I thought I was on the right track by creating a property on form2 for my first textbox. I only have the one property set up right now because I wasn't sure it would work and was only testing. In form1 I was trying to set it up so I could access my class values from the selected item.
Form 1
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
DialogResult result = editProperties.ShowDialog();
object employeeSelect = lstBoxEmployees.SelectedValue;
editProperties.TextFirstName = Convert.ToString(employeeSelect);
}
form 2
public partial class frmProperties : Form
{
public string TextFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
public frmProperties()
{
InitializeComponent();
}
}
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}
public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}
Form 2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}

View list on another form

Can someone tell me what i'm doing wrong here?
I've got a list in a database class that I want to view in a listbox on my form yet it's not showing anything.
I call the form from a button click of my first form which is where I enter the data and it works if I put a listbox on that form, but I'm wanting to open another form which will only show the data if that makes sense?
here's my code for the form that I want to view what's in the list:
public partial class Summary : Form
{
public Summary()
{
InitializeComponent();
}
private Database viewlist = new Database();
private void Summary_Load(object sender, EventArgs e)
{
}
private void sum()
{
List<String> listofPicks = viewlist.listPickups();
listBox1.Items.AddRange(listofPicks.ToArray());
}
private void button1_Click(object sender, EventArgs e)
{
sum();
}
}
Also may i make it clear that this code works if it's all done on the same form
Try this (kind of), load the list when the form loads, not on a button click:
public partial class Summary : Form
{
private Database _viewlist = new Database();
public Summary()
{
InitializeComponent();
}
private void Summary_Load(object sender, EventArgs e)
{
LoadList();
}
private void LoadList()
{
listBox1.Items.Clear();
listBox1.Items.AddRange(_viewlist.Pickups.ToArray());
}
}
public class Database
{
public List<string> Pickups
{
get { return new List<string> {"alfa", "beta"}; }
}
}

Categories

Resources