I know this question has been asked many of times about how to create a search button. I am very new to C# programming and I am having a hard time creating a search and just haven't found what I am looking for from other posts. So I hope someone can help me.
I have created a Windows Form Application and I have a form setup using "Details" view from my DataSet and the data shows up correctly in the application when I scroll from record to record. My data is stored in a sdf file. I want to have people either enter in an "account number" or a persons "last name" and then be able to hit the search button. And after the search button the prearranged fields would update with the information. For the ability to either choose the "last name" or the "account number" I can have the items listed in a combo box if need be.
I have included a copy of the code some of the naming of the items have been changed as to not disclose my profession. Any help is greatly appreciated.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void custtableBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void custtableBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.custtableBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.custDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'custDataSet.custtable' table. You can move, or remove it, as needed.
this.custtableTableAdapter.Fill(this.custDataSet.custtable);
}
private void file_Name_12TextBox_TextChanged(object sender, EventArgs e)
{
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.custtableTableAdapter.FillBy(this.custDataSet.custtable);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
private void btnfind_Click(object sender, EventArgs e)
{
}
}
}
What is the custtableBindingNavigatorSaveItem_Click and custtableBindingNavigatorSaveItem_Click_1 ? Thet are the same, if they are two buttons doing the same thing then you can use the same method from both buttons (though why two buttons to do the same thing I do not know).
Anyway, you have some choices and it depends on the datasize, speed needed, whether the data should be offline or db locked and so on...
Set you queries/stored procedures (sprocs are safer) to allow for limiting the result. This lets the DB do all the hard work - it is what DB's are designed for.
Read everything into memory (your table fill) and select off the table - not to disimilar really with Linq queries. This means all the data is offline and you will need to decide when and how you write it back to the database (if at all).
PS: I am intrigued, what is your profession that your pseudonym here is not enough to hide behind? Imagination gone wild !
Related
i have been tasked to make a layout of 4 web browsers, that later could be used for security cameras.
the web browser part was easy, but i have been stuck at making it responsive, because when you run it and maximize the program, the resolution of the web browsers stay the same. and since this program will be running on a big flat screen it has to respond to the resolution.
i have looked all over the internet and have not found a solution. i have tried the anchoring but when i do this and i enlarge the program the browsers start over lapping each other. i have tried putting them in a flow grid and a table grid. other things i found was "this.AutoSize = true;" but i am kinda new to c# forms and do not understand this.
can anyone help?
the code and a few photo's of what happens
how it is now
what happens when enlarged
the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace webspace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("www.nos.nl");
webBrowser2.Navigate("www.google.com");
webBrowser3.Navigate("www.facebook.com");
webBrowser4.Navigate("www.google.com/maps");
this.AutoSize = true;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser2_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser3_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser4_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + "is loading...";
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void webBrowser3_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
Have a layout that is two Orientation=Horizontal SplitContainers, inside another SplitContainer that is Orientation=Vertical (or vice versa, two verticals one on either side of a horizontal)
When the form resizes, set all the SplitContainer's SplitterDistances to 50% of the width/height as appropriate.. Unless the user's customised them (in which case decide what to do, like have a proportional resize, thereby allowing the user to have some views bigger than others)
Make the web browser controls Dock=Fill their panels
Note that the WebBrowser control is quite old now, and should probably be replaced with a WebView2 (Chrome-based Edge)
I am trying to write a data input app using Visual Studio 2017 C# for my company.
We have 2 tables, SI and SIC, in a SQL DB.
I have created a form and put a bound datagridview, which is bound to the SIC table.
On form load, it shows all data nicely.
What we want to do is enter data into this datagridview and save.
However, when entering a value into column D, we want to check that it doesn`t exist in column D in the SI table.
I did have it setup with a unique constraint, however, when the record wasn`t unique, an error message appeared, and pressing OK would delete all the other data on the row. Not desirable as there is quite a lot of data to be entered.
I am very new to programming in general, so would appreciate any advice that could be given.
My form code looks like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Product_List
{
public partial class NewProduct_frm : Form
{
public NewProduct_frm()
{
InitializeComponent();
}
private void NewProduct_frm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bHTESTDataSet.SIC' table. You can move, or remove it, as needed.
this.TableAdapter.Fill(this.bHTESTDataSet.SIC);
}
private void Save_btn_Click(object sender, EventArgs e)
{
this.Validate();
this.BindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.bHTESTDataSet);
}
private void Reload_btn_Click(object sender, EventArgs e)
{
this.TableAdapter.Fill(this.bHTESTDataSet.SIC);
}
}
}
i actually work on a Customer-DataGrid but i stuck on the Sources because i dont use really often C#.
I have a DataGridView (dataGridView1), a internal Database (Database.mdf), a BindingSource (customerBindingSource) and customerTableAdapter
Now i trying to refresh the DataSource when i click a button.
Here is a simple snippet:
private void Kundenverwaltung_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'kundenAnsicht.customer' table. You can move, or remove it, as needed.
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
// I tried already some methods but i dont find a properly, functionally way
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
I hope you can understand my problem.
~ Dennis
You need to connect your DataGridView with "customerBindingSource":
`private void Kundenverwaltung_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = this.customerBindingSource;
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}`
It all.
I'm trying to develop a simple app for Windows Phone 8, and there are many requirements for the use of the Back Button. As I don't want the Back Button to simply GoBack in back stack, I'd like to pop up a message box to warn the user that this action will bring him back to main menu.
Problem is, this page has to be reloaded some times, and the following code stop working properly after 1 reload. The messagebox opens multiple times. And the more times I reload, the more MessageBox appears.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using BackButtonTests.Resources;
namespace BackButtonTests
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
NavigationService.Navigating += NavigationService_Navigating;
}
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
private void Restart_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml?reload=" + DateTime.Now.ToString(), UriKind.RelativeOrAbsolute));
//Use this fake reload query with unique value as a way to "deceive" the system, as windowsphone does not support NavigationService.Reload, and using simply the Uri of the same page will not properly load everything
}
private void Quit_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Quit");
}
}
}
This is just a test code I wrote, that shows exactly the problem I'm experiencing in my actual project. Of course there are 2 buttons written in xaml.
And the code won't work until you first reload the page, as it's not NavigatedTo when it's the front page (not a problem in my actual project).
Any clues of what I'm doing wrong?
NOTE: I'm not interested in changing the event handler (to OnBackKeyPress, for instance). I'm interested in understanding what's going on with the handler I chose (NavigationService.Navigating, NavigationMode.Back). Thanks
Updated following additional information that clarifies the questing
Changing your navigating event handler to will mean the event isn't fired on every page in the stack
void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
NavigationService.Navigating -= NavigationService_Navigating;
if (e.NavigationMode == NavigationMode.Back)
{
e.Cancel = true;
MessageBox.Show("Quit");
}
}
No longer neccessary
Override OnBackKeypress instead of navigating
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var DoYouWantToQuit = MessageBox.Show("Are you sure you want to Quit", "Quit", MessageBoxButtons.OkCancel);
if (DoYouWantToQuit != MessageBoxButton.Ok)
{
e.Cancel = true
}
base.OnBackKeyPress(e);
}
(This is a continuation of the discussion on this question)
I have code that looks in a specific folder in the C: drive. It can tell what is in that folder, and gets what the user selects. But the problem is switching to a new Data folder to get the code from.
All the code necessary to run the program is kept in the Data folder, the company I am interning with wants to be able to switch Data folders so they can show their software off better, and have it geared towards whoever they are showing it to.
So basically my program needs to switch data folders so the company can show their software better.
Ill post all my code, its not much, so you guys can look at all of it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string defaultPath = #"C:\Mavro\MavBridge\";
public Form1()
{
InitializeComponent();
dropdown();
}
private void button1_Click(object sender, EventArgs e)
{
//some sort of code to switch directory before close goes here
MessageBox.Show("Data folder has been changed.", "Done");
Application.Exit();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = comboBox1.SelectedItem.ToString();
defaultPath = path;
}
private void buttonTest_Click_1(object sender, EventArgs e)
{
}
public void dropdown()
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
}
}
To answer your second question, about stripping the Default Path from the combobox display
//Where you load your directories
string[] dispDirectories = Directory.GetDirectories(#"c:\", "*.*");
// so here we will iterate through all the directories found and remove the default path from it.
for (int i=0;i<dispDirectories.Count();i++)
dispDirectories[i]=dispDirectories[i].Remove(0, defaultPath.Length);
Then where you set your path change to this. Because we removed the default Path we now have to add it again.
string path = defaultPath+comboBox1.SelectedItem.ToString();
defaultPath = path;
Look at your button1_Click method. Change your message Box to
MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");
private void button1_Click(object sender, EventArgs e)
{
//some sort of code to switch directory before close goes here
MessageBox.Show("Data folder has been changed to "+defaultPath,"Done");
Application.Exit();
}
you will see that you have already changed the default Path
EDIT(#K'Leg Suggestion to make answer more clear): If you want to get the subdirectories, after you make your first selection you should call method dropdown() in comboBox1_SelectedIndexChanged
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = comboBox1.SelectedItem.ToString();
defaultPath = path;
dropdown();
}
A better would be receive default path as parameter in dropdown(), something on the following line
public void dropdown(string defaultPath)
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, "Data*");
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
and then call dropdown method in comboBox1_SelectedIndexChanged as:
dropdown(comboBox1.SelectedItem.ToString());
EDIT: (based on the comments on OP) the problem is the filter you are specifying for the GetDirecotries, for every path you pass to it, it looks for folder starting with Data and then any characters, for example Data.Apple, now when you set your path to Data.Apple, there it again looks for folder which should start with Data, you may pass the filter in the dropdown method on some condition
you may define the method dropdown as:
public void dropdown(string defaultPath, string filter)
{
string[] dispDirectories = Directory.GetDirectories(defaultPath, filter);
comboBox1.Items.Clear();
comboBox1.Items.AddRange(dispDirectories);
}
Then you can call the dropdown for the first time as :
public Form1()
{
InitializeComponent();
dropdown(#"C:\Mavro\MavBridge\","Data*");
}
and then in the SelectedIndexChanged as:
dropdown(comboBox1.SelectedItem.ToString(),"*"); // * means select all